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