From 3ed2627f7c05e8eaa4559371f9d6d2cb71aa4275 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期日, 12 九月 2021 14:11:08 +0800
Subject: [PATCH] 优化刷新缓存功能
---
java110-db/src/main/java/com/java110/db/Java110MybatisInterceptor.java | 273 ++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 254 insertions(+), 19 deletions(-)
diff --git a/java110-db/src/main/java/com/java110/db/Java110MybatisInterceptor.java b/java110-db/src/main/java/com/java110/db/Java110MybatisInterceptor.java
old mode 100644
new mode 100755
index 9714206..ffca6b9
--- a/java110-db/src/main/java/com/java110/db/Java110MybatisInterceptor.java
+++ b/java110-db/src/main/java/com/java110/db/Java110MybatisInterceptor.java
@@ -1,30 +1,47 @@
package com.java110.db;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.java110.core.client.RestTemplate;
+import com.java110.core.factory.Java110TransactionalFactory;
+import com.java110.db.dao.IQueryServiceDAO;
+import com.java110.dto.order.OrderItemDto;
+import com.java110.utils.constant.ServiceConstant;
+import com.java110.utils.factory.ApplicationContextFactory;
+import com.java110.utils.util.DateUtil;
+import com.java110.utils.util.StringUtil;
import org.apache.ibatis.executor.Executor;
-import org.apache.ibatis.mapping.*;
+import org.apache.ibatis.mapping.BoundSql;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.mapping.ParameterMapping;
+import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.http.*;
+import java.sql.Timestamp;
import java.text.DateFormat;
-import java.util.Date;
-import java.util.List;
-import java.util.Locale;
-import java.util.Properties;
+import java.util.*;
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class,
Object.class})
})
-
public class Java110MybatisInterceptor implements Interceptor {
private static Logger logger = LoggerFactory.getLogger(Java110MybatisInterceptor.class);
+ IQueryServiceDAO queryServiceDAOImpl;
+ RestTemplate restTemplate;
+
@Override
public Object intercept(Invocation invocation) throws Throwable {
+ if (StringUtil.isEmpty(Java110TransactionalFactory.getOId())) { //鏈紑鍚簨鍔�
+ return invocation.proceed();
+ }
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
Object parameter = null;
@@ -34,18 +51,19 @@
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
Configuration configuration = mappedStatement.getConfiguration();
+ Map<String, Object> sqlValue = new HashMap<>();
//鑾峰彇sql璇彞
- String sql = showSql(configuration, boundSql);
-
+ String sql = showSql(configuration, boundSql, sqlValue, sqlCommandType);
+ restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
switch (sqlCommandType) {
case INSERT:
- dealInsertSql(mappedStatement, parameter,sql);
+ dealInsertSql(mappedStatement, parameter, sql, sqlValue);
break;
case UPDATE:
- dealUpdateSql(mappedStatement, parameter,sql);
+ dealUpdateSql(mappedStatement, parameter, sql, sqlValue);
break;
case DELETE:
- dealDeleteSql(mappedStatement, parameter,sql);
+ dealDeleteSql(mappedStatement, parameter, sql, sqlValue);
break;
}
return invocation.proceed();
@@ -57,7 +75,50 @@
* @param mappedStatement
* @param parameter
*/
- private void dealDeleteSql(MappedStatement mappedStatement, Object parameter,String sql) {
+ private void dealDeleteSql(MappedStatement mappedStatement, Object parameter, String sql, Map<String, Object> sqlValue) {
+
+ String tmpTable = sql.substring(sql.indexOf("into") + 4, sql.indexOf("(")).trim();
+ if(tmpTable.indexOf(" ") > 0){
+ tmpTable = tmpTable.substring(0,tmpTable.indexOf(" "));
+ }
+ String tmpWhere = sql.substring(sql.indexOf("where"));
+ //鎻掑叆鎿嶄綔鏃朵箣鍓嶇殑 娌℃湁鏁版嵁 鎵�浠� preValue 涓虹┖瀵硅薄
+ JSONArray preValues = new JSONArray();
+
+ String execSql = "select * from " + tmpTable + " " + tmpWhere;
+
+ queryServiceDAOImpl = ApplicationContextFactory.getBean("queryServiceDAOImpl", IQueryServiceDAO.class);
+ List<Map<String, Object>> deleteDatas = queryServiceDAOImpl.executeSql(execSql, null);
+
+ if (deleteDatas != null && deleteDatas.size() > 0) {
+ for (Map<String, Object> map : deleteDatas) {
+ dealReturnMap(map);
+ preValues.add(map);
+ }
+ }
+
+ JSONArray afterValues = new JSONArray();
+
+ JSONObject logText = new JSONObject();
+ logText.put("preValue", preValues);
+ logText.put("afterValue", afterValues);
+
+ OrderItemDto orderItemDto = new OrderItemDto();
+ orderItemDto.setbId("-1");
+ orderItemDto.setAction("DEL");
+ orderItemDto.setActionObj(tmpTable.trim());
+ orderItemDto.setLogText(logText.toJSONString());
+ orderItemDto.setServiceName(ApplicationContextFactory.getApplicationName());
+ orderItemDto.setoId(Java110TransactionalFactory.getOId());
+
+ String url = ServiceConstant.SERVICE_ORDER_URL + "/order/oIdApi/createOrderItem";
+ HttpHeaders httpHeaders = new HttpHeaders();
+ HttpEntity httpEntity = new HttpEntity(orderItemDto.toString(), httpHeaders);
+ ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
+
+ if (responseEntity.getStatusCode() != HttpStatus.OK) {
+ throw new IllegalArgumentException("娉ㄥ唽浜嬪姟鍥炴粴鏃ュ織澶辫触" + responseEntity);
+ }
}
/**
@@ -66,7 +127,79 @@
* @param mappedStatement
* @param parameter
*/
- private void dealUpdateSql(MappedStatement mappedStatement, Object parameter,String sql) {
+ private void dealUpdateSql(MappedStatement mappedStatement, Object parameter, String sql, Map<String, Object> sqlValue) {
+ //RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
+
+ String tmpTable = sql.substring(sql.indexOf("update") + 6, sql.indexOf("set")).trim();
+
+ if(tmpTable.indexOf(" ") > 0){
+ tmpTable = tmpTable.substring(0,tmpTable.indexOf(" "));
+ }
+ String tmpWhere = sql.substring(sql.indexOf("where"));
+
+ //鎻掑叆鎿嶄綔鏃朵箣鍓嶇殑 娌℃湁鏁版嵁 鎵�浠� preValue 涓虹┖瀵硅薄
+ JSONArray preValues = new JSONArray();
+ JSONArray afterValues = new JSONArray();
+ JSONObject afterVaule = null;
+ String execSql = "select * from " + tmpTable + " " + tmpWhere;
+ queryServiceDAOImpl = ApplicationContextFactory.getBean("queryServiceDAOImpl", IQueryServiceDAO.class);
+ List<Map<String, Object>> deleteDatas = queryServiceDAOImpl.executeSql(execSql, null);
+
+ if (deleteDatas != null && deleteDatas.size() > 0) {
+ for (Map<String, Object> map : deleteDatas) {
+ dealReturnMap(map);
+ preValues.add(map);
+ afterVaule = new JSONObject();
+ afterVaule.putAll(map);
+ afterVaule.putAll(sqlValue);
+ afterValues.add(afterVaule);
+ }
+ }
+
+ JSONObject logText = new JSONObject();
+ logText.put("preValue", preValues);
+ logText.put("afterValue", afterValues);
+
+ OrderItemDto orderItemDto = new OrderItemDto();
+ orderItemDto.setbId("-1");
+ orderItemDto.setAction("MOD");
+ orderItemDto.setActionObj(tmpTable.trim());
+ orderItemDto.setLogText(logText.toJSONString());
+ orderItemDto.setServiceName(ApplicationContextFactory.getApplicationName());
+ orderItemDto.setoId(Java110TransactionalFactory.getOId());
+
+ String url = ServiceConstant.SERVICE_ORDER_URL + "/order/oIdApi/createOrderItem";
+ HttpHeaders httpHeaders = new HttpHeaders();
+ HttpEntity httpEntity = new HttpEntity(orderItemDto.toString(), httpHeaders);
+ ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
+
+ if (responseEntity.getStatusCode() != HttpStatus.OK) {
+ throw new IllegalArgumentException("娉ㄥ唽浜嬪姟鍥炴粴鏃ュ織澶辫触" + responseEntity);
+ }
+ }
+
+ private void dealReturnMap(Map<String, Object> map) {
+ for (String key : map.keySet()) {
+ Object value = map.get(key);
+ if (value instanceof String) {
+ map.put(key, "'" + map.get(key) + "'");
+ } else if (value instanceof Date) {
+ String tmpValue = DateUtil.getFormatTimeString((Date) value, DateUtil.DATE_FORMATE_STRING_A);
+ map.put(key, "'" + tmpValue + "'");
+ } else if (value instanceof Timestamp) {
+ Date date = new Date(((Timestamp) value).getTime());
+ String tmpValue = DateUtil.getFormatTimeString(date, DateUtil.DATE_FORMATE_STRING_A);
+ map.put(key, "'" + tmpValue + "'");
+ } else if (value instanceof Double) {
+ map.put(key, "'" + map.get(key) + "'");
+ } else {
+ if (value != null) {
+ map.put(key, "'" + value.toString() + "'");
+ } else {
+ map.put(key, "''");
+ }
+ }
+ }
}
/**
@@ -75,12 +208,46 @@
* @param mappedStatement
* @param parameter
*/
- private void dealInsertSql(MappedStatement mappedStatement, Object parameter,String sql) {
- String deleteSql = "delete from ";
+ private void dealInsertSql(MappedStatement mappedStatement, Object parameter, String sql, Map<String, Object> sqlValue) {
+ // RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
+ //鎻掑叆鎿嶄綔鏃朵箣鍓嶇殑 娌℃湁鏁版嵁 鎵�浠� preValue 涓虹┖瀵硅薄
+ JSONArray preValues = new JSONArray();
+
+ JSONArray afterValues = new JSONArray();
+
+ String tmpTable = sql.substring(sql.toLowerCase().indexOf("into") + 4, sql.indexOf("(")).trim();
+
+ if(tmpTable.indexOf(" ") > 0){
+ tmpTable = tmpTable.substring(0,tmpTable.indexOf(" "));
+ }
+
+ afterValues.add(sqlValue);
+
+ JSONObject logText = new JSONObject();
+ logText.put("preValue", preValues);
+ logText.put("afterValue", afterValues);
+
+ OrderItemDto orderItemDto = new OrderItemDto();
+ orderItemDto.setbId("-1");
+ orderItemDto.setAction("ADD");
+ orderItemDto.setActionObj(tmpTable.trim());
+ orderItemDto.setLogText(logText.toJSONString());
+ orderItemDto.setServiceName(ApplicationContextFactory.getApplicationName());
+ orderItemDto.setoId(Java110TransactionalFactory.getOId());
+
+ String url = ServiceConstant.SERVICE_ORDER_URL + "/order/oIdApi/createOrderItem";
+ HttpHeaders httpHeaders = new HttpHeaders();
+ HttpEntity httpEntity = new HttpEntity(orderItemDto.toString(), httpHeaders);
+ ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
+
+ if (responseEntity.getStatusCode() != HttpStatus.OK) {
+ throw new IllegalArgumentException("娉ㄥ唽浜嬪姟鍥炴粴鏃ュ織澶辫触" + responseEntity);
+ }
}
+
@Override
public Object plugin(Object target) {
@@ -94,15 +261,18 @@
}
- public String showSql(Configuration configuration, BoundSql boundSql) {
+ public String showSql(Configuration configuration, BoundSql boundSql, Map<String, Object> sqlValue, SqlCommandType sqlCommandType) {
Object parameterObject = boundSql.getParameterObject();
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
- String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
+ String sql = boundSql.getSql().replaceAll("[\\s]+", " ").toLowerCase();
+ String orgSql = sql;// 鍘熷sql
+
+ List<String> values = new ArrayList<>();
if (parameterMappings.size() > 0 && parameterObject != null) {
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));
-
+ values.add(getParameterValue(parameterObject));
} else {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
for (ParameterMapping parameterMapping : parameterMappings) {
@@ -110,13 +280,69 @@
if (metaObject.hasGetter(propertyName)) {
Object obj = metaObject.getValue(propertyName);
sql = sql.replaceFirst("\\?", getParameterValue(obj));
+ values.add(getParameterValue(obj));
} else if (boundSql.hasAdditionalParameter(propertyName)) {
Object obj = boundSql.getAdditionalParameter(propertyName);
sql = sql.replaceFirst("\\?", getParameterValue(obj));
+ values.add(getParameterValue(obj));
}
}
}
}
+
+ if (sqlCommandType == SqlCommandType.INSERT) {
+ String tmpKey = orgSql.substring(orgSql.indexOf("(") + 1, orgSql.indexOf(")"));
+ String[] tmpKeys = tmpKey.split(",");
+
+// if (values.size() < tmpKeys.length) {
+// throw new IllegalArgumentException("sql 閿欒 key 鍜寁alue 涓暟涓嶇瓑" + sql);
+// }
+ for (int keyIndex = 0; keyIndex < tmpKeys.length; keyIndex++) {
+ String key = tmpKeys[keyIndex].trim();
+ String value = "";
+ if (values.size() - 1 < keyIndex) {
+ continue;
+ }
+ value = values.get(keyIndex);
+ if ("''".equals(value)) {
+ continue;
+ }
+ sqlValue.put(key, value);
+ }
+ } else if (sqlCommandType == SqlCommandType.UPDATE) {
+ String tmpKey = orgSql.substring(sql.indexOf("set") + 3, orgSql.indexOf("where"));
+ String[] tmpKeys = tmpKey.split(",");
+// if (values.size() < tmpKeys.length) {
+// throw new IllegalArgumentException("sql 閿欒 key 鍜寁alue 涓暟涓嶇瓑" + sql);
+// }
+ for (int keyIndex = 0; keyIndex < tmpKeys.length; keyIndex++) {
+ String tmpSetKey = tmpKeys[keyIndex];
+ String[] keyValues = tmpSetKey.split("=");
+ String key = "";
+ if (keyValues.length != 2) {
+ throw new IllegalArgumentException("update 璇彞鍙兘鏈夐棶棰橈紝娌℃湁 set 涓嚭閿� " + sql);
+ }
+ if (keyValues[0].contains(".")) {
+ key = keyValues[0].substring(keyValues[0].indexOf(".") + 1).trim();
+ } else {
+ key = keyValues[0].trim();
+ }
+ String value = "";
+ if (values.size() - 1 < keyIndex) {
+ continue;
+ }
+ value = values.get(keyIndex);
+ if ("''".equals(value)) {
+ continue;
+ }
+ sqlValue.put(key, value);
+ }
+
+ } else if (sqlCommandType == SqlCommandType.DELETE) {
+
+ }
+
+
return sql;
}
@@ -132,10 +358,19 @@
if (obj != null) {
value = obj.toString();
} else {
- value = "";
+ value = "''";
}
}
return value;
}
+
+
+ public static void main(String[] args) {
+ String tmpTable = "product_label t";
+ if(tmpTable.indexOf(" ") > 0){
+ tmpTable = tmpTable.substring(0,tmpTable.indexOf(" "));
+ }
+ System.out.printf(tmpTable);
+ }
}
--
Gitblit v1.8.0