拦截器介绍

mybatis提供了@Intercepts注解允许开发者对mybatis的执行器Executor进行拦截。

Executor接口方法主要有update、query、commit、rollback等等。

主要思路为:

  1. 进入拦截器方法中
  2. 获取拦截器方法参数
  3. 获取解析参数及SQL
  4. 自定义生成自己的SQL语句
  5. 将自定义SQL设置进参数中
  6. 由mybatis处理后续问题

拦截器代码

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.Plugin;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Properties;
@Component
@Intercepts({@org.apache.ibatis.plugin.Signature(type = Executor.class, method = "query",
args = {
MappedStatement.class,
Object.class,
RowBounds.class,
ResultHandler.class,
CacheKey.class,
BoundSql.class})})
public class MybatisInterceptorConfig implements Interceptor {
/*自定义SQL*/
private String resetSql(String sql) { }
@Override
public Object intercept(Invocation invocation) throws Throwable {
resetSql(invocation);
return invocation.proceed();
}
@Override
public Object plugin(Object o) {
return Plugin.wrap(o, this);
}
@Override
public void setProperties(Properties properties) { }
private void resetSql(Invocation invocation) {
final Object[] args = invocation.getArgs();
BoundSql boundSql = (BoundSql) args[5];
if(StringUtils.isNotEmpty(boundSql.getSql())) {
modify(boundSql,"sql",resetSql(boundSql.getSql()));
}
}
private static void modify(Object object, String fieldName, Object newFieldValue){
try {
Field field = object.getClass().getDeclaredField(fieldName);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
if(!field.isAccessible()) {
field.setAccessible(true);
}
field.set(object, newFieldValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}

最新文章

  1. TemplateMethod(模块方法模式)
  2. COJ968 WZJ的数据结构(负三十二)
  3. HTML5音频,视频播放
  4. 将Ajax 中数组转换成字符串 封装成类
  5. 如何正确看待Linq的DistinctBy扩展和ForEach扩展
  6. 解决表格里面使用text-overflow后依旧不能隐藏超出的文本
  7. JavaEE Tutorials (15) - 对Java持久化API应用使用二级缓存
  8. jQuery实现按键盘方向键翻页
  9. JavaScript开发者必备的10个sublime的插件
  10. com.mysql.jdbc.Driver和com.mysql.cj.jdbc.Driver的区别
  11. !!!css如何让img图片居中?css的display属性实现图片居中(代码实例)
  12. js判断页面在pc端打开还是移动端打开
  13. 【Android】Android设计准则
  14. 如何卸载VMware虚拟机?
  15. topcoder srm 320 div1
  16. Ext.require 的作用(转)
  17. hdu 4122 Alice's mooncake shop (线段树)
  18. webpack配置实践
  19. java面试笔试题收集
  20. FD.io 社区中国行暨未来网络技术沙龙 南京站 参会小结

热门文章

  1. 【Python3爬虫】一次应对JS反调试的记录
  2. C#设计模式学习笔记:(10)外观模式
  3. 多线程共享变量和 AsyncLocal
  4. NetCore3.0实现自定义IOC容器注入
  5. RabbitMQ安装(发生系统错误5。拒绝访问。发生系统错误1067。进程意外终止。)
  6. Webdriver启动Firefox浏览器后,页面显示空白
  7. 折腾vue--使用vscode创建vue项目(二)
  8. 虚拟机安装_1_wincc_matriton
  9. NAS之NFS/CIFS
  10. 【python基础语法】第3天作业练习题