MyBatis 如何通过拦截器修改 SQL

ops/2024/9/23 9:23:56/

目录

    • 1. 实现Interceptor接口
    • 2. 注册配置文件

假如我们想实现多租户,或者在某些 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 {@Overridepublic 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();}@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {}}

2. 注册配置文件

将插件注册到 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>

http://www.ppmy.cn/ops/89194.html

相关文章

WebKit简介及工作流程

引言 随着互联网的飞速发展&#xff0c;浏览器作为用户访问网络世界的门户&#xff0c;其性能和稳定性日益成为关注的焦点。在众多浏览器引擎中&#xff0c;WebKit以其卓越的渲染性能和跨平台特性&#xff0c;赢得了广泛赞誉。作为前端技术专家&#xff0c;深入了解WebKit的架…

C语言 | Leetcode C语言题解之第318题最大单词长度乘积

题目&#xff1a; 题解&#xff1a; int maxProduct(char ** words, int wordsSize){int masks[wordsSize];memset(masks, 0, sizeof(masks));for(int i 0; i < wordsSize; i) {int len strlen(words[i]);for(int j 0; j < len; j) {masks[i] | 1 << (words[i]…

38 器件移动、旋转、镜像、对齐、等间距操作介绍39 器件、网络、过孔锁定与解锁操作40 相同模块复用操作41 测量、查询功能介绍

38 器件移动、旋转、镜像、对齐、等间距操作介绍&&39 器件、网络、过孔锁定与解锁操作&&40 相同模块复用操作&& 41 测量、查询功能介绍 第一部分 38 器件移动、旋转、镜像、对齐、等间距操作介绍第二部分 39 器件、网络、过孔锁定与解锁操作第三部分 4…

校园选课助手【6】-使用验证码验证抢课接口

需求分析&#xff1a;抢课开放时&#xff0c;大量用户同时访问抢课接口&#xff0c;防止有人利用程序恶意刷接口进行抢课。 1.导入验证码依赖 <!--验证码依赖--><dependency><groupId>com.github.whvcse</groupId><artifactId>easy-captcha<…

基于springboot+vue+uniapp的美术馆预约平台小程序

开发语言&#xff1a;Java框架&#xff1a;springbootuniappJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#…

设计模式 策略模式(Strategy Pattern) C++表达

设计模式 策略模式&#xff08;Strategy Pattern&#xff09; C表达 flyfish 策略模式&#xff08;Strategy Pattern&#xff09;是一种行为设计模式&#xff0c;它的核心思想是将一系列相关的算法或行为封装到独立的策略类中&#xff0c;并使得这些策略可以相互替换。主要用…

文件系统 FTP Ubuntu 安装入门介绍

文件服务系列 文件存储服务系统&#xff08;File Storage Service System&#xff09;-00-文件服务器是什么&#xff1f;为什么需要&#xff1f; 文件存储服务系统&#xff08;File Storage Service System&#xff09;-01-常见的文件协议介绍 文件系统 FTP Ubuntu 安装入门…

QTableWidget的使用(可多行输入)

效果展示 源码 mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow>class QTableWidget; class MainWindow : public QWidget {Q_OBJECTpublic:MainWindow(QWidget *parent nullptr);~MainWindow();private:void insertTableRow(const QSt…