如何自定義MyBatis攔截器更改表名
自定義MyBatis攔截器可以在方法執(zhí)行前后插入自己的邏輯,這非常有利于擴(kuò)展和定制 MyBatis 的功能。本篇文章實現(xiàn)自定義一個攔截器去改變要插入或者查詢的數(shù)據(jù)源。
@Intercepts
@Intercepts是Mybatis的一個注解,它的主要作用是標(biāo)識一個類為攔截器。該注解通過一個@Signature注解(即攔截點),來指定攔截那個對象里面的某個方法。
具體來說,@Signature注解的屬性type用于指定攔截器類型,可能的值包括:
- Executor(sql的內(nèi)部執(zhí)行器)
- ParameterHandler(攔截參數(shù)的處理)
- StatementHandler(攔截sql的構(gòu)建)
- ResultSetHandler(攔截結(jié)果的處理)。
method屬性表示在指定的攔截器類型中要攔截的方法
args屬性表示攔截的方法對應(yīng)的參數(shù)
實現(xiàn)步驟
實現(xiàn)org.apache.ibatis.plugin.Interceptor接口,重寫一下的方法:

添加攔截器注解,@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
配置文件中添加攔截器

注意需要在 Spring Boot 的 application.yml 文件中配置 mybatis 配置文件的路徑。
mybatis攔截器目前不支持在application.yml配置文件中通過屬性配置,目前只支持通過xml配置或者代碼配置。
代碼實現(xiàn)
Mybatis攔截器:
package top.emanjusaka.springboottest.mybatis.plugin;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import top.emanjusaka.springboottest.mybatis.annotation.DBTableStrategy;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Author emanjusaka
* @Date 2023/10/18 17:25
* @Version 1.0
*/
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class DynamicMybatisPlugin implements Interceptor {
private Pattern pattern = Pattern.compile("(from|into|update)[\\s]{1,}(\\w{1,})", Pattern.CASE_INSENSITIVE);
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
// 獲取自定義注解判斷是否進(jìn)行分表操作
String id = mappedStatement.getId();
String className = id.substring(0, id.lastIndexOf("."));
Class<?> clazz = Class.forName(className);
DBTableStrategy dbTableStrategy = clazz.getAnnotation(DBTableStrategy.class);
if (null == dbTableStrategy || !dbTableStrategy.changeTable() || null == dbTableStrategy.tbIdx()) {
return invocation.proceed();
}
// 獲取SQL
BoundSql boundSql = statementHandler.getBoundSql();
String sql = boundSql.getSql();
// 替換SQL表名
Matcher matcher = pattern.matcher(sql);
String tableName = null;
if (matcher.find()) {
tableName = matcher.group().trim();
}
assert null != tableName;
String replaceSql = matcher.replaceAll(tableName + "_" + dbTableStrategy.tbIdx());
// 通過反射修改SQL語句
Field field = boundSql.getClass().getDeclaredField("sql");
field.setAccessible(true);
field.set(boundSql, replaceSql);
field.setAccessible(false);
return invocation.proceed();
}
}mapper的xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.emanjusaka.springboottest.score.repository.IScoreRepository">
<select id="selectAll" resultType="top.emanjusaka.springboottest.score.model.vo.ScoreVO">
select * from score
</select>
</mapper>切換表名的注解:
package top.emanjusaka.springboottest.score.repository;
import org.apache.ibatis.annotations.Mapper;
import top.emanjusaka.springboottest.mybatis.annotation.DBTableStrategy;
import top.emanjusaka.springboottest.score.model.vo.ScoreVO;
import java.util.List;
/**
* @Author emanjusaka
* @Date 2023/10/18 17:45
* @Version 1.0
*/
@Mapper
@DBTableStrategy(changeTable = true,tbIdx = "2")
public interface IScoreRepository {
List<ScoreVO> selectAll();
}測試代碼:
package top.emanjusaka.springboottest;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import top.emanjusaka.springboottest.score.model.vo.ScoreVO;
import top.emanjusaka.springboottest.score.service.IScore;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
class SpringBootTestApplicationTests {
@Resource
private IScore score;
@Test
void contextLoads() {
List<ScoreVO> list = score.selectAll();
list.forEach(System.out::println);
}
}運行結(jié)果

通過上圖可以看出,現(xiàn)在表名已經(jīng)修改成了score_2了。通過這種機(jī)制,我們可以應(yīng)用到自動分表中。本文的表名的索引是通過注解參數(shù)傳遞的,實際應(yīng)用中需要通過哈希散列計算。
到此這篇關(guān)于自定義MyBatis攔截器更改表名的文章就介紹到這了,更多相關(guān)MyBatis攔截器更改表名內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解java中String、StringBuilder、StringBuffer的區(qū)別
這篇文章主要介紹了java中String、StringBuilder、StringBuffer的區(qū)別,文中講解的很清晰,有對于這方面不太懂的同學(xué)可以研究下2021-02-02
JavaWeb實現(xiàn)顯示mysql數(shù)據(jù)庫數(shù)據(jù)
MySQL是最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),在WEB應(yīng)用方面MySQL是最好的。本文將利用JavaWeb實現(xiàn)顯示mysql數(shù)據(jù)庫數(shù)據(jù)功能,需要的可以參考一下2022-03-03
IntelliJ IDEA中如何構(gòu)建Spring Boot的項目
這篇文章主要介紹了IntelliJ IDEA中如何構(gòu)建Spring Boot的項目問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Spring?Boot?+?MySQL讀寫分離實現(xiàn)方案全過程
讀寫分離是數(shù)據(jù)庫架構(gòu)中的一種優(yōu)化策略,它將讀操作和寫操作分開處理,通常通過將讀請求和寫請求分別發(fā)送到不同的數(shù)據(jù)庫服務(wù)器來實現(xiàn),這篇文章主要介紹了Spring?Boot?+?MySQL讀寫分離實現(xiàn)方案的相關(guān)資料,需要的朋友可以參考下2025-12-12

