cmg
2024-06-22 cb38789006b411b29fba27a6a60493dc78946e17
提交 | 用户 | 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();
65         //新增
66         if(sqlCommandType == SqlCommandType.INSERT){
fb2c9f 67             sql = SqlUtils.insertSql(sql, VariableAesKey.aesKeysTable);
c64e12 68         }else if(sqlCommandType == SqlCommandType.UPDATE){
fb2c9f 69             sql = SqlUtils.updateSql(sql, VariableAesKey.aesKeysTable);
c64e12 70         }else if(sqlCommandType == SqlCommandType.SELECT){
C 71             if(VariableAesKey.isRun == 1){
fb2c9f 72                 sql = SqlUtils.selectSql(sql, VariableAesKey.aesKeysTable);
c64e12 73             }
C 74         }else if(sqlCommandType == SqlCommandType.DELETE){
fb2c9f 75             sql = SqlUtils.deleteSql(sql, VariableAesKey.aesKeysTable);
c64e12 76         }
346eb2 77
fb2c9f 78         //通过反射修改sql语句
C 79         Field field = boundSql.getClass().getDeclaredField("sql");
80         field.setAccessible(true);
81         field.set(boundSql, sql);
72586e 82
c64e12 83         return invocation.proceed();
C 84
85     }
86
87     @Override
88     public Object plugin(Object target) {
89         if (target instanceof StatementHandler) {
90             return Plugin.wrap(target, this);
91         } else {
92             return target;
93         }
94
95     }
96
97     @Override
98     public void setProperties(Properties properties) {
99
100     }
101 }