zhouxiang
2022-04-25 8ab2ad5580212b91df848e4c127f2a682485fde3
提交 | 用户 | age
c64e12 1 package com.hx.mybatis.aes.springbean;
C 2
3 import org.apache.ibatis.executor.statement.StatementHandler;
4 import org.apache.ibatis.mapping.BoundSql;
5 import org.apache.ibatis.mapping.MappedStatement;
6 import org.apache.ibatis.mapping.ParameterMapping;
7 import org.apache.ibatis.mapping.SqlCommandType;
8 import org.apache.ibatis.plugin.*;
9 import org.apache.ibatis.reflection.DefaultReflectorFactory;
10 import org.apache.ibatis.reflection.MetaObject;
11 import org.apache.ibatis.reflection.SystemMetaObject;
12 import org.apache.ibatis.session.Configuration;
8ab2ad 13 import org.slf4j.Logger;
Z 14 import org.slf4j.LoggerFactory;
c64e12 15 import org.springframework.stereotype.Component;
C 16
17 import java.lang.reflect.Field;
18 import java.sql.Connection;
19 import java.util.List;
20 import java.util.Properties;
21
22
23 @Component
24 @Intercepts({
25         @Signature(
26                 type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class
27         })
28 })
29 public class MySqlInterceptor implements Interceptor {
8ab2ad 30
Z 31     private static Logger logger = LoggerFactory.getLogger(MySqlInterceptor.class.getName());
c64e12 32     @Override
C 33     public Object intercept(Invocation invocation) throws Throwable {
34
35         // 方法一
36         StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
37         MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
38         //先拦截到RoutingStatementHandler,里面有个StatementHandler类型的delegate变量,其实现类是BaseStatementHandler,然后就到BaseStatementHandler的成员变量mappedStatement
39         MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
40         //id为执行的mapper方法的全路径名,如com.uv.dao.UserMapper.insertUser
41         //String id = mappedStatement.getId();
42         //sql语句类型 select、delete、insert、update
43         SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
44         BoundSql boundSql = statementHandler.getBoundSql();
45
46         // 获取节点的配置
47         Configuration configuration = mappedStatement.getConfiguration();
48         // 获取参数
49         Object parameterObject = boundSql.getParameterObject();
50         // MetaObject主要是封装了originalObject对象,提供了get和set的方法用于获取和设置originalObject的属性值,主要支持对JavaBean、Collection、Map三种类型对象的操作
51         // MetaObject metaObject1 = configuration.newMetaObject(parameterObject);
52         //获取sql中问号的基本信息
53         List<ParameterMapping> parameterMappings = boundSql
54                 .getParameterMappings();
55
56         /*for (ParameterMapping parameterMapping : parameterMappings) {
57             String propertyName = parameterMapping.getProperty();
58             System.out.println("propertyName:"+ propertyName);
59             System.out.println("parameterObject:"+ parameterObject);
60         }*/
61
62         //这里可以进行sql修改
63         //获取到原始sql语句
64         String sql = boundSql.getSql();
8ab2ad 65         String sql2 = null;
c64e12 66         //新增
C 67         if(sqlCommandType == SqlCommandType.INSERT){
8ab2ad 68             sql2 = SqlUtils.insertSql(sql, VariableAesKey.aesKeysTable);
c64e12 69         }else if(sqlCommandType == SqlCommandType.UPDATE){
8ab2ad 70             sql2 = SqlUtils.updateSql(sql, VariableAesKey.aesKeysTable);
c64e12 71         }else if(sqlCommandType == SqlCommandType.SELECT){
C 72             if(VariableAesKey.isRun == 1){
8ab2ad 73                 sql2 = SqlUtils.selectSql(sql, VariableAesKey.aesKeysTable);
Z 74             }else{
75                 sql2  = sql;
c64e12 76             }
C 77         }else if(sqlCommandType == SqlCommandType.DELETE){
8ab2ad 78             sql2 = SqlUtils.deleteSql(sql, VariableAesKey.aesKeysTable);
Z 79         }else{
80             sql2  = sql;
c64e12 81         }
346eb2 82
8ab2ad 83         if(!"err".equals(sql2)){
72586e 84             //通过反射修改sql语句
Z 85             Field field = boundSql.getClass().getDeclaredField("sql");
86             field.setAccessible(true);
8ab2ad 87             field.set(boundSql, sql2);
72586e 88         }
Z 89
c64e12 90         return invocation.proceed();
C 91
92     }
93
94     @Override
95     public Object plugin(Object target) {
96         if (target instanceof StatementHandler) {
97             return Plugin.wrap(target, this);
98         } else {
99             return target;
100         }
101
102     }
103
104     @Override
105     public void setProperties(Properties properties) {
106
107     }
108 }