MyBatis 如何通过拦截器修改 SQL

目录

    • [1. 实现Interceptor接口](#1. 实现Interceptor接口)
    • [2. 注册配置文件](#2. 注册配置文件)

假如我们想实现多租户,或者在某些 SQL 后面自动拼接查询条件。在开发过程中大部分场景可能都是一个查询写一个 SQL 去处理,我们如果想修改最终 SQL 可以通过修改各个 mapper.xml 中的 SQL 来处理。

但实际过程中我们可能穿插着 ORM 和 SQL 的混合使用,隐藏在代码中不容易被发现,还有假如项目中有很多很多的 SQL 我们不可能一个一个的去修改解决。

这个时候我们就需要通过 mybatis 拦截 SQL 并且最终修改 SQL。

具体操作如下:

1. 实现Interceptor接口

实现Interceptor接口,并写相关逻辑

java 复制代码
package cn.source.framework.interceptor.impl;

import cn.source.common.core.domain.BaseEntity;
import cn.source.common.core.domain.entity.SysUser;
import cn.source.common.utils.DateUtils;
import cn.source.common.utils.SecurityUtils;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;

import java.util.Properties;

/**
 * @Description: 自定义拦截器,注入创建人,创建日期,修改人,修改日期,企业id
 */
@Intercepts({
   @Signature(
       type = Executor.class,
       method = "update",
       args = {MappedStatement.class, Object.class}),
})
public class HandleBaseInfoInterceptor implements Interceptor {

     @Override
     public Object intercept(Invocation invocation) throws Throwable {
       Object[] args = invocation.getArgs();
       MappedStatement mappedStatement = (MappedStatement) args[0];
       SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
       // 用户类不处理,会导致获取不到用户信息出错
       if (args[1] instanceof SysUser) {
         return invocation.proceed();
       }
       if (args[1] instanceof BaseEntity) {
         BaseEntity baseEntity = (BaseEntity) args[1];
         if (sqlCommandType == SqlCommandType.UPDATE) {
           baseEntity.setUpdateTime(DateUtils.getNowDate());
           baseEntity.setUpdateBy(SecurityUtils.getUsername());
         } else if (sqlCommandType == SqlCommandType.INSERT) {
           baseEntity.setCreateTime(DateUtils.getNowDate());
           baseEntity.setCreateBy(SecurityUtils.getUsername());
           // System.out.println("赋值企业ID:" + baseEntity.getCompanyId());
           baseEntity.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
         }
       }
       return invocation.proceed();
     }

     @Override
     public Object plugin(Object target) {
       return Plugin.wrap(target, this);
     }
    
     @Override
     public void setProperties(Properties properties) {
     }
        
}

2. 注册配置文件

将插件注册到 mybatis 的配置文件 mybatis-config.xml

xml 复制代码
<plugins>
    <!-- 自定义拦截器,注入企业id-->
	<plugin interceptor="cn.source.framework.interceptor.impl.HandleSelectInterceptor">   </plugin>
	<!-- 自定义拦截器,注入创建人,创建日期,修改人,修改日期,企业id-->
	<plugin interceptor="cn.source.framework.interceptor.impl.HandleBaseInfoInterceptor">  </plugin>
</plugins>
相关推荐
失心疯_202326 分钟前
006.MySQL_查询数据
数据库·sql·mysql·关系型数据库·sqlyog·mysql教程·查询语句
环能jvav大师34 分钟前
基于R语言的统计分析基础:使用SQL语句操作数据集
开发语言·数据库·sql·数据分析·r语言·sqlite
骆晨学长1 小时前
基于springboot的智慧社区微信小程序
java·数据库·spring boot·后端·微信小程序·小程序
@月落1 小时前
alibaba获得店铺的所有商品 API接口
java·大数据·数据库·人工智能·学习
楠枬1 小时前
MySQL数据的增删改查(一)
数据库·mysql
goTsHgo1 小时前
从底层原理上解释 clickhouse 保证完全的幂等性
数据库·clickhouse
hayhead2 小时前
高频 SQL 50 题(基础版)| 626. 换座位
sql·力扣
阿华的代码王国3 小时前
MySQL ------- 索引(B树B+树)
数据库·mysql
Hello.Reader3 小时前
StarRocks实时分析数据库的基础与应用
大数据·数据库
执键行天涯3 小时前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis