MyBatis如何通过拦截器修改SQL

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

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

这个时候我们就需要通过mybatis拦截SQL并且最终修改SQL。具体操作如下:

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

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
 * @author: sourcebyte.vip
 * @date: 2023年10月18日 14:33
 */
@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) {
 }
}

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

<plugins>
  <!-- 自定义拦截器,注入企业id-->
  <plugin interceptor="cn.source.framework.interceptor.impl.HandleSelectInterceptor">   </plugin>
  <!-- 自定义拦截器,注入创建人,创建日期,修改人,修改日期,企业id-->
  <plugin interceptor="cn.source.framework.interceptor.impl.HandleBaseInfoInterceptor">  </plugin>
</plugins>

 

如若转载,请注明出处:开源字节   https://sourcebyte.vip/article/347.html

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开源字节

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值