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