//package org.springblade.common.interceptor;
|
//
|
//import org.apache.ibatis.binding.MapperMethod;
|
//import org.apache.ibatis.executor.Executor;
|
//import org.apache.ibatis.executor.statement.StatementHandler;
|
//import org.apache.ibatis.mapping.BoundSql;
|
//import org.apache.ibatis.mapping.MappedStatement;
|
//import org.apache.ibatis.plugin.*;
|
//import org.apache.logging.log4j.util.Strings;
|
//import org.springblade.common.utils.SQLParseUtils;
|
//import org.springblade.es.service.ElasticsearchDocumentService;
|
//import org.springblade.es.vo.EsParam;
|
//import org.springblade.modules.article.entity.Article;
|
//import org.springframework.beans.factory.annotation.Autowired;
|
//import java.util.Properties;
|
//
|
//@Intercepts({
|
// @Signature(type = StatementHandler.class, method = "update", args = {MappedStatement.class, Object.class})
|
//})
|
//public class DataSyncInterceptor implements Interceptor {
|
//
|
// @Autowired
|
// private ElasticsearchDocumentService elasticsearchDocumentService;
|
//
|
// /**
|
// * 拦截器在sql执行成功后同步到es,
|
// * 如果同步失败抛出异常,保证数据一致性
|
// *
|
// * @param invocation
|
// * @return
|
// * @throws Throwable
|
// */
|
// @Override
|
// public Object intercept(Invocation invocation) throws Throwable {
|
// Object res = invocation.proceed();
|
//
|
// Object[] args = invocation.getArgs();
|
// if (args.length >= 2) {
|
// MappedStatement mappedStatement = (MappedStatement) args[0];
|
// //参数
|
// Object parameter = invocation.getArgs()[1];
|
// BoundSql boundSql = mappedStatement.getBoundSql(parameter);
|
// //sql
|
// String sql = boundSql.getSql();
|
// sql = sql.replaceAll("\n", "");
|
// //获取表名
|
// String tableName = SQLParseUtils.getTableName(sql);
|
// String sqlType = SQLParseUtils.parseSQLType(sql);
|
// if (!Strings.isBlank(tableName)) {
|
// if (tableName.equals("jczz_article") ||
|
// tableName.equals("jczz_house") ||
|
// tableName.equals("jczz_household") ||
|
// tableName.equals("jczz_place"))
|
// syncDataAfterUpdate(sql,tableName,sqlType,args[1]);
|
// }
|
// }
|
// return res;
|
// }
|
//
|
//
|
// /**
|
// * 数据同步
|
// * @param tableName
|
// * @param sqlType
|
// * @param parameter
|
// */
|
// private void syncDataAfterUpdate(String sql,String tableName,String sqlType,Object parameter) {
|
// EsParam esParam = new EsParam();
|
// esParam.setIndexName("test");
|
// esParam.setTableName(tableName);
|
// // 判断操作类型
|
// if (sqlType.equals("INSERT")){
|
// //insert 可用直接拦截到实体类
|
// if (tableName.equals("jczz_article")) {
|
// Article entity = (Article) parameter;
|
// elasticsearchDocumentService.addArticle(esParam,entity);
|
// }
|
// }
|
// if(sqlType.equals("UPDATE")){
|
// //update 方法需要特殊处理
|
// if (tableName.equals("jczz_article")) {
|
// Article entity = (Article) ((MapperMethod.ParamMap) parameter).get("param1");
|
// elasticsearchDocumentService.update(esParam,entity);
|
// }
|
// }
|
// // 删除处理
|
// if(sqlType.equals("DELETE")){
|
// esParam.setTableId("test");
|
// elasticsearchDocumentService.removeByQuery(esParam);
|
// }
|
// }
|
//
|
//
|
// @Override
|
// public Object plugin(Object target) {
|
// if (target instanceof Executor || target instanceof StatementHandler) {
|
// return Plugin.wrap(target, this);
|
// }
|
// return target;
|
// }
|
//
|
// @Override
|
// public void setProperties(Properties properties) {
|
// // 可以接收配置属性
|
// }
|
//}
|