• MyBatis 如何通过拦截器修改 SQL


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

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

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

    在这里插入图片描述

    具体操作如下:

    1. 实现Interceptor接口

    实现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
     */
    @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

    <plugins>
        
    	<plugin interceptor="cn.source.framework.interceptor.impl.HandleSelectInterceptor">   plugin>
    	
    	<plugin interceptor="cn.source.framework.interceptor.impl.HandleBaseInfoInterceptor">  plugin>
    plugins>
    
  • 相关阅读:
    【优化servlet】
    11.9 知识总结(三板斧、全局配置文件、静态文件的配置、request对象等)
    什么是tomcat?tomcat是干什么用的?下面带你们认识tomcat!通俗易懂!
    结合 Django 和 Vue.js 打造现代 Web 应用
    Mysql命令增加、修改、删除表字段
    一、react简介
    水牛社软件适合网络新手吗?说说我的看法
    spring bean实例注入到map 集合中
    美团动态线程池实践思路,开源了
    文心智能体平台介绍和应用:制作你的智能体(运维小帮手)
  • 原文地址:https://blog.csdn.net/qq_47183158/article/details/140891022