From eea88d44d68728dd5306a1443697242636339a1f Mon Sep 17 00:00:00 2001 From: liquanyu Date: Mon, 5 Mar 2018 14:09:04 +0800 Subject: [PATCH] mysql --- .../web/inteceptor/MysqlDataInterceptor.java | 198 +++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 platform-operate-api/src/main/java/com/lyms/platform/operate/web/inteceptor/MysqlDataInterceptor.java diff --git a/platform-operate-api/src/main/java/com/lyms/platform/operate/web/inteceptor/MysqlDataInterceptor.java b/platform-operate-api/src/main/java/com/lyms/platform/operate/web/inteceptor/MysqlDataInterceptor.java new file mode 100644 index 0000000..336d1b6 --- /dev/null +++ b/platform-operate-api/src/main/java/com/lyms/platform/operate/web/inteceptor/MysqlDataInterceptor.java @@ -0,0 +1,198 @@ +package com.lyms.platform.operate.web.inteceptor; + + +import com.lyms.platform.common.utils.ExceptionUtils; +import com.lyms.platform.operate.web.utils.SendMysqlSyncDatUtil; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.executor.resultset.ResultSetHandler; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.ParameterMapping; +import org.apache.ibatis.plugin.*; +import org.apache.ibatis.reflection.MetaObject; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.type.TypeHandlerRegistry; + +import java.sql.Statement; +import java.text.DateFormat; +import java.util.*; + +/** + * 同步线上mysql的操作到区域 + * Created by Administrator on 2017-04-24. + */ +@Intercepts({ + @Signature(type = Executor.class, method = "update", args = { MappedStatement.class,Object.class }), + @Signature(type= ResultSetHandler.class, method = "handleResultSets", args = {Statement.class}) + }) +public class MysqlDataInterceptor implements Interceptor { + + //同步的表 + private static Set uses = new HashSet<>(); + static { + uses.add("baby_patient_extend_ear"); + uses.add("baby_patient_extend_ear_birth"); + uses.add("baby_patient_extend_ear_family"); + uses.add("baby_patient_extend_ear_follow_up"); + uses.add("baby_patient_extend_ear_hearing_diagnose"); + uses.add("baby_patient_extend_ear_mother"); + uses.add("baby_patient_extend_ear_screen"); + } + + private String sql; + + private Properties properties; + + private String sqlCommandType; + + private String sqlId; + + public Object intercept(Invocation invocation) throws Throwable { + Object[] args = invocation.getArgs(); + Object returnValue = null; + try + { + if (args != null && args.length == 2) + { + MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; + Object parameter = null; + if (invocation.getArgs().length > 1) { + parameter = invocation.getArgs()[1]; + } + sqlId = mappedStatement.getId(); + BoundSql boundSql = mappedStatement.getBoundSql(parameter); + Configuration configuration = mappedStatement.getConfiguration(); + sqlCommandType = mappedStatement.getSqlCommandType().name(); + if ("update".equals(sqlCommandType.toLowerCase()) || "delete".equals(sqlCommandType.toLowerCase())) + { + sql = getSql(configuration, boundSql, sqlId); + System.out.println("sql = "+sql); + if (isSyncTable(sql)) + { + + System.out.println("==============delete or update sync sql = " + sql); + //发送要同步的sql + sql = null; + } + } + else if ("insert".equals(sqlCommandType.toLowerCase()) ) + { + String tempSql = getSql(configuration, boundSql, sqlId); + if (isSyncTable(tempSql)) + { + sql = tempSql; + } + } + returnValue = invocation.proceed(); + } + else + { + returnValue = invocation.proceed(); + if (StringUtils.isNotEmpty(sql) && sqlCommandType != null && "insert".equals(sqlCommandType.toLowerCase())) + { + if (returnValue != null && returnValue instanceof ArrayList) + { + List list = (ArrayList)returnValue; + if (CollectionUtils.isNotEmpty(list)) + { + sql = sql.replaceFirst("\\(","(ID,"); + sql = sql.substring(0,sql.lastIndexOf("(")+1)+list.get(0)+","+sql.substring(sql.lastIndexOf("(")+1,sql.length()); + System.out.println("============add sync sql = "+sql); + //发送要同步的sql + sql = null; + } + } + } + } + }catch (Exception e) + { + ExceptionUtils.catchException(e," Mybatis Sql Interceptor exception"); + } + return returnValue; + } + + + /** + * 判断sql中的表是否要同步的 + * @param sql + * @return + */ + private boolean isSyncTable(String sql) + { + boolean use = false; + if (StringUtils.isEmpty(sql)) + { + return use; + } + for(String key : uses) + { + if (sql.toLowerCase().contains(key)) + { + use = true; + break; + } + } + + return use; + } + + public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId) { + String sql = showSql(configuration, boundSql); + + return sql; + } + + private static String getParameterValue(Object obj,String javaType) { + String value = null; + if (obj instanceof String) { + value = "'" + obj.toString() + "'"; + } else if (obj instanceof Date) { + DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA); + value = "'" + formatter.format(new Date()) + "'"; + } else { + if (obj != null) { + value = obj.toString(); + } else + { + value = "null"; + } + } + return value; + } + + public static String showSql(Configuration configuration, BoundSql boundSql) { + Object parameterObject = boundSql.getParameterObject(); + List parameterMappings = boundSql.getParameterMappings(); + String sql = boundSql.getSql().replaceAll("[\\s]+", " "); + if (parameterMappings.size() > 0 && parameterObject != null) { + TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); + if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { + sql = sql.replaceFirst("\\?", getParameterValue(parameterObject,parameterObject.getClass().getName())); + + } else { + MetaObject metaObject = configuration.newMetaObject(parameterObject); + for (ParameterMapping parameterMapping : parameterMappings) { + String propertyName = parameterMapping.getProperty(); + if (metaObject.hasGetter(propertyName)) { + Object obj = metaObject.getValue(propertyName); + sql = sql.replaceFirst("\\?", getParameterValue(obj,parameterMapping.getJavaType().getName())); + } else if (boundSql.hasAdditionalParameter(propertyName)) { + Object obj = boundSql.getAdditionalParameter(propertyName); + sql = sql.replaceFirst("\\?", getParameterValue(obj,parameterMapping.getJavaType().getName())); + } + } + } + } + return sql; + } + + public Object plugin(Object target) { + return Plugin.wrap(target, this); + } + + public void setProperties(Properties properties0) { + this.properties = properties0; + } +} \ No newline at end of file -- 1.8.3.1