拦截器类:
package com.jiahe.sgjcfx.handler;
import cn.hutool.core.util.ReflectUtil;
import com.jiahe.sgjcfx.annotation.DifferentHandler;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import java.lang.reflect.Field;
import java.util.Properties;
@Intercepts({
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
// @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
public class HandlerDiffInterceptor implements Interceptor {
private static final Integer MAPPED_STATEMENT_INDEX = 0;
private static final Integer PARAM_OBJ_INDEX = 1;
private static final Integer BOUND_SQL_INDEX = 5;
private static final String DIFF_ONE = "11";
private static final String DIFF_ONE_SON = "('22','33','44')";
private static final String DIFF_TWO = "55";
private static final String DIFF_TWO_SON = "('66','77','88')";
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement) args[MAPPED_STATEMENT_INDEX];
// 根据全限定名判断mapper类上有没有注解
String msId = ms.getId();
msId = msId.substring(0, msId.lastIndexOf("."));
Class<?> mapperClass = Class.forName(msId);
boolean present = mapperClass.isAnnotationPresent(DifferentQXHandler.class);
if (present) {
Object parameter = args[PARAM_OBJ_INDEX];
BoundSql boundSql = args.length == 4 ? ms.getBoundSql(parameter) : (BoundSql) args[BOUND_SQL_INDEX];
Object obj = boundSql.getParameterObject();
String sql = boundSql.getSql();
if (sql.trim().toUpperCase().startsWith("SELECT")) {
Field qxdm = obj.getClass().getDeclaredField("qxdm");
qxdm.setAccessible(true);
String value = (String) qxdm.get(obj);
if (DIFF_ONE.equals(value)) {
ReflectUtil.setFieldValue(obj, "qxdm", DIFF_ONE_SON);
} else if (DIFF_TWO.equals(value)) {
ReflectUtil.setFieldValue(obj, "qxdm", DIFF_TWO_SON);
}
sql = sql.replace("CODE =", "CODE in");
ReflectUtil.setFieldValue(boundSql, "sql", sql);
}
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Interceptor.super.plugin(target);
}
@Override
public void setProperties(Properties properties) {
Interceptor.super.setProperties(properties);
}
}
注解类:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DifferentHandler {
}
配置类:
@Bean
public String myInterceptor(SqlSessionFactory sqlSessionFactory) {
//实例化插件
HandlerDiffInterceptor sqlInterceptor = new HandlerDiffInterceptor();
//创建属性值
Properties properties = new Properties();
properties.setProperty("prop1","value1");
//将属性值设置到插件中
sqlInterceptor.setProperties(properties);
//将插件添加到SqlSessionFactory工厂
sqlSessionFactory.getConfiguration().addInterceptor(sqlInterceptor);
return "interceptor";
}
1759

被折叠的 条评论
为什么被折叠?



