Commit eea88d44d68728dd5306a1443697242636339a1f
1 parent
8783b5ad1a
Exists in
master
and in
6 other branches
mysql
Showing 1 changed file with 198 additions and 0 deletions
platform-operate-api/src/main/java/com/lyms/platform/operate/web/inteceptor/MysqlDataInterceptor.java
View file @
eea88d4
| 1 | +package com.lyms.platform.operate.web.inteceptor; | |
| 2 | + | |
| 3 | + | |
| 4 | +import com.lyms.platform.common.utils.ExceptionUtils; | |
| 5 | +import com.lyms.platform.operate.web.utils.SendMysqlSyncDatUtil; | |
| 6 | +import org.apache.commons.collections.CollectionUtils; | |
| 7 | +import org.apache.commons.lang.StringUtils; | |
| 8 | +import org.apache.ibatis.executor.Executor; | |
| 9 | +import org.apache.ibatis.executor.resultset.ResultSetHandler; | |
| 10 | +import org.apache.ibatis.mapping.BoundSql; | |
| 11 | +import org.apache.ibatis.mapping.MappedStatement; | |
| 12 | +import org.apache.ibatis.mapping.ParameterMapping; | |
| 13 | +import org.apache.ibatis.plugin.*; | |
| 14 | +import org.apache.ibatis.reflection.MetaObject; | |
| 15 | +import org.apache.ibatis.session.Configuration; | |
| 16 | +import org.apache.ibatis.type.TypeHandlerRegistry; | |
| 17 | + | |
| 18 | +import java.sql.Statement; | |
| 19 | +import java.text.DateFormat; | |
| 20 | +import java.util.*; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * 同步线上mysql的操作到区域 | |
| 24 | + * Created by Administrator on 2017-04-24. | |
| 25 | + */ | |
| 26 | +@Intercepts({ | |
| 27 | + @Signature(type = Executor.class, method = "update", args = { MappedStatement.class,Object.class }), | |
| 28 | + @Signature(type= ResultSetHandler.class, method = "handleResultSets", args = {Statement.class}) | |
| 29 | + }) | |
| 30 | +public class MysqlDataInterceptor implements Interceptor { | |
| 31 | + | |
| 32 | + //同步的表 | |
| 33 | + private static Set<String> uses = new HashSet<>(); | |
| 34 | + static { | |
| 35 | + uses.add("baby_patient_extend_ear"); | |
| 36 | + uses.add("baby_patient_extend_ear_birth"); | |
| 37 | + uses.add("baby_patient_extend_ear_family"); | |
| 38 | + uses.add("baby_patient_extend_ear_follow_up"); | |
| 39 | + uses.add("baby_patient_extend_ear_hearing_diagnose"); | |
| 40 | + uses.add("baby_patient_extend_ear_mother"); | |
| 41 | + uses.add("baby_patient_extend_ear_screen"); | |
| 42 | + } | |
| 43 | + | |
| 44 | + private String sql; | |
| 45 | + | |
| 46 | + private Properties properties; | |
| 47 | + | |
| 48 | + private String sqlCommandType; | |
| 49 | + | |
| 50 | + private String sqlId; | |
| 51 | + | |
| 52 | + public Object intercept(Invocation invocation) throws Throwable { | |
| 53 | + Object[] args = invocation.getArgs(); | |
| 54 | + Object returnValue = null; | |
| 55 | + try | |
| 56 | + { | |
| 57 | + if (args != null && args.length == 2) | |
| 58 | + { | |
| 59 | + MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; | |
| 60 | + Object parameter = null; | |
| 61 | + if (invocation.getArgs().length > 1) { | |
| 62 | + parameter = invocation.getArgs()[1]; | |
| 63 | + } | |
| 64 | + sqlId = mappedStatement.getId(); | |
| 65 | + BoundSql boundSql = mappedStatement.getBoundSql(parameter); | |
| 66 | + Configuration configuration = mappedStatement.getConfiguration(); | |
| 67 | + sqlCommandType = mappedStatement.getSqlCommandType().name(); | |
| 68 | + if ("update".equals(sqlCommandType.toLowerCase()) || "delete".equals(sqlCommandType.toLowerCase())) | |
| 69 | + { | |
| 70 | + sql = getSql(configuration, boundSql, sqlId); | |
| 71 | + System.out.println("sql = "+sql); | |
| 72 | + if (isSyncTable(sql)) | |
| 73 | + { | |
| 74 | + | |
| 75 | + System.out.println("==============delete or update sync sql = " + sql); | |
| 76 | + //发送要同步的sql | |
| 77 | + sql = null; | |
| 78 | + } | |
| 79 | + } | |
| 80 | + else if ("insert".equals(sqlCommandType.toLowerCase()) ) | |
| 81 | + { | |
| 82 | + String tempSql = getSql(configuration, boundSql, sqlId); | |
| 83 | + if (isSyncTable(tempSql)) | |
| 84 | + { | |
| 85 | + sql = tempSql; | |
| 86 | + } | |
| 87 | + } | |
| 88 | + returnValue = invocation.proceed(); | |
| 89 | + } | |
| 90 | + else | |
| 91 | + { | |
| 92 | + returnValue = invocation.proceed(); | |
| 93 | + if (StringUtils.isNotEmpty(sql) && sqlCommandType != null && "insert".equals(sqlCommandType.toLowerCase())) | |
| 94 | + { | |
| 95 | + if (returnValue != null && returnValue instanceof ArrayList) | |
| 96 | + { | |
| 97 | + List<Integer> list = (ArrayList)returnValue; | |
| 98 | + if (CollectionUtils.isNotEmpty(list)) | |
| 99 | + { | |
| 100 | + sql = sql.replaceFirst("\\(","(ID,"); | |
| 101 | + sql = sql.substring(0,sql.lastIndexOf("(")+1)+list.get(0)+","+sql.substring(sql.lastIndexOf("(")+1,sql.length()); | |
| 102 | + System.out.println("============add sync sql = "+sql); | |
| 103 | + //发送要同步的sql | |
| 104 | + sql = null; | |
| 105 | + } | |
| 106 | + } | |
| 107 | + } | |
| 108 | + } | |
| 109 | + }catch (Exception e) | |
| 110 | + { | |
| 111 | + ExceptionUtils.catchException(e," Mybatis Sql Interceptor exception"); | |
| 112 | + } | |
| 113 | + return returnValue; | |
| 114 | + } | |
| 115 | + | |
| 116 | + | |
| 117 | + /** | |
| 118 | + * 判断sql中的表是否要同步的 | |
| 119 | + * @param sql | |
| 120 | + * @return | |
| 121 | + */ | |
| 122 | + private boolean isSyncTable(String sql) | |
| 123 | + { | |
| 124 | + boolean use = false; | |
| 125 | + if (StringUtils.isEmpty(sql)) | |
| 126 | + { | |
| 127 | + return use; | |
| 128 | + } | |
| 129 | + for(String key : uses) | |
| 130 | + { | |
| 131 | + if (sql.toLowerCase().contains(key)) | |
| 132 | + { | |
| 133 | + use = true; | |
| 134 | + break; | |
| 135 | + } | |
| 136 | + } | |
| 137 | + | |
| 138 | + return use; | |
| 139 | + } | |
| 140 | + | |
| 141 | + public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId) { | |
| 142 | + String sql = showSql(configuration, boundSql); | |
| 143 | + | |
| 144 | + return sql; | |
| 145 | + } | |
| 146 | + | |
| 147 | + private static String getParameterValue(Object obj,String javaType) { | |
| 148 | + String value = null; | |
| 149 | + if (obj instanceof String) { | |
| 150 | + value = "'" + obj.toString() + "'"; | |
| 151 | + } else if (obj instanceof Date) { | |
| 152 | + DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA); | |
| 153 | + value = "'" + formatter.format(new Date()) + "'"; | |
| 154 | + } else { | |
| 155 | + if (obj != null) { | |
| 156 | + value = obj.toString(); | |
| 157 | + } else | |
| 158 | + { | |
| 159 | + value = "null"; | |
| 160 | + } | |
| 161 | + } | |
| 162 | + return value; | |
| 163 | + } | |
| 164 | + | |
| 165 | + public static String showSql(Configuration configuration, BoundSql boundSql) { | |
| 166 | + Object parameterObject = boundSql.getParameterObject(); | |
| 167 | + List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); | |
| 168 | + String sql = boundSql.getSql().replaceAll("[\\s]+", " "); | |
| 169 | + if (parameterMappings.size() > 0 && parameterObject != null) { | |
| 170 | + TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); | |
| 171 | + if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { | |
| 172 | + sql = sql.replaceFirst("\\?", getParameterValue(parameterObject,parameterObject.getClass().getName())); | |
| 173 | + | |
| 174 | + } else { | |
| 175 | + MetaObject metaObject = configuration.newMetaObject(parameterObject); | |
| 176 | + for (ParameterMapping parameterMapping : parameterMappings) { | |
| 177 | + String propertyName = parameterMapping.getProperty(); | |
| 178 | + if (metaObject.hasGetter(propertyName)) { | |
| 179 | + Object obj = metaObject.getValue(propertyName); | |
| 180 | + sql = sql.replaceFirst("\\?", getParameterValue(obj,parameterMapping.getJavaType().getName())); | |
| 181 | + } else if (boundSql.hasAdditionalParameter(propertyName)) { | |
| 182 | + Object obj = boundSql.getAdditionalParameter(propertyName); | |
| 183 | + sql = sql.replaceFirst("\\?", getParameterValue(obj,parameterMapping.getJavaType().getName())); | |
| 184 | + } | |
| 185 | + } | |
| 186 | + } | |
| 187 | + } | |
| 188 | + return sql; | |
| 189 | + } | |
| 190 | + | |
| 191 | + public Object plugin(Object target) { | |
| 192 | + return Plugin.wrap(target, this); | |
| 193 | + } | |
| 194 | + | |
| 195 | + public void setProperties(Properties properties0) { | |
| 196 | + this.properties = properties0; | |
| 197 | + } | |
| 198 | +} |