MyBatis 怎样通过拦截器修改 SQL

打印 上一主题 下一主题

主题 1027|帖子 1027|积分 3081


如果我们想实现多租户,或者在某些 SQL 背面自动拼接查询条件。在开发过程中大部分场景可能都是一个查询写一个 SQL 行止理,我们如果想修改最终 SQL 可以通过修改各个 mapper.xml 中的 SQL 来处理。
但实际过程中我们可能穿插着 ORM 和 SQL 的混合使用,隐藏在代码中不轻易被发现,还有如果项目中有许多许多的 SQL 我们不可能一个一个的去修改办理。
这个时候我们就需要通过 mybatis 拦截 SQL 并且最终修改 SQL。

详细利用如下:
1. 实现Interceptor接口

实现Interceptor接口,并写相关逻辑
  1. package cn.source.framework.interceptor.impl;
  2. import cn.source.common.core.domain.BaseEntity;
  3. import cn.source.common.core.domain.entity.SysUser;
  4. import cn.source.common.utils.DateUtils;
  5. import cn.source.common.utils.SecurityUtils;
  6. import org.apache.ibatis.executor.Executor;
  7. import org.apache.ibatis.mapping.MappedStatement;
  8. import org.apache.ibatis.mapping.SqlCommandType;
  9. import org.apache.ibatis.plugin.*;
  10. import java.util.Properties;
  11. /**
  12. * @Description: 自定义拦截器,注入创建人,创建日期,修改人,修改日期,企业id
  13. */
  14. @Intercepts({
  15.    @Signature(
  16.        type = Executor.class,
  17.        method = "update",
  18.        args = {MappedStatement.class, Object.class}),
  19. })
  20. public class HandleBaseInfoInterceptor implements Interceptor {
  21.      @Override
  22.      public Object intercept(Invocation invocation) throws Throwable {
  23.        Object[] args = invocation.getArgs();
  24.        MappedStatement mappedStatement = (MappedStatement) args[0];
  25.        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
  26.        // 用户类不处理,会导致获取不到用户信息出错
  27.        if (args[1] instanceof SysUser) {
  28.          return invocation.proceed();
  29.        }
  30.        if (args[1] instanceof BaseEntity) {
  31.          BaseEntity baseEntity = (BaseEntity) args[1];
  32.          if (sqlCommandType == SqlCommandType.UPDATE) {
  33.            baseEntity.setUpdateTime(DateUtils.getNowDate());
  34.            baseEntity.setUpdateBy(SecurityUtils.getUsername());
  35.          } else if (sqlCommandType == SqlCommandType.INSERT) {
  36.            baseEntity.setCreateTime(DateUtils.getNowDate());
  37.            baseEntity.setCreateBy(SecurityUtils.getUsername());
  38.            // System.out.println("赋值企业ID:" + baseEntity.getCompanyId());
  39.            baseEntity.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
  40.          }
  41.        }
  42.        return invocation.proceed();
  43.      }
  44.      @Override
  45.      public Object plugin(Object target) {
  46.        return Plugin.wrap(target, this);
  47.      }
  48.    
  49.      @Override
  50.      public void setProperties(Properties properties) {
  51.      }
  52.         
  53. }
复制代码
2. 注册配置文件

将插件注册到 mybatis 的配置文件 mybatis-config.xml
  1. <plugins>
  2.     <!-- 自定义拦截器,注入企业id-->
  3.         <plugin interceptor="cn.source.framework.interceptor.impl.HandleSelectInterceptor">   </plugin>
  4.         <!-- 自定义拦截器,注入创建人,创建日期,修改人,修改日期,企业id-->
  5.         <plugin interceptor="cn.source.framework.interceptor.impl.HandleBaseInfoInterceptor">  </plugin>
  6. </plugins>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

商道如狼道

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表