mybatis-plus?實現(xiàn)查詢表名動態(tài)修改的示例代碼
通過 mybatis-plus 實現(xiàn)表名的動態(tài)替換,即通過配置或入?yún)討B(tài)選擇不同的表。
下面通過一個例子來說明該需求: 我們需要為學校開發(fā)一個成績管理系統(tǒng),需要建立三張表: 學生表、科目表和成績表,表的 ER 圖如下所示。

對應(yīng)的建表語句如下:
-- 學科表 drop table if exists subject; create table subject(id int primary key , name varchar(64)); -- 學生表 drop table if exists student; create table student (id int primary key , name varchar(64)); -- 成績表(學生-學科 多對多) drop table if exists score; create table score(id int primary key , student_id int, subject_id int, result int);
根據(jù)三張表級聯(lián)查詢成績的查詢語句為:
select subject.name as subject_name, student.name as student_name, score.result as score
from score, student, subject where score.student_id=student.id and score.subject_id=subject.id;
現(xiàn)在又來了一個新需求,我們的這套成績查詢系統(tǒng)需要部署在不同學校的服務(wù)器上,因為每個學校的學生表和成績表都要同步到教育局的服務(wù)器中,因此需要為這兩個表添加學校前綴,ER 圖如下所示。

不同學校的建表語句不同,對于 USTC 學校而言,建表語句為:
-- 學科表 drop table if exists subject; create table subject(id int primary key , name varchar(64)); -- 學生表 drop table if exists ustc_student; create table ustc_student (id int primary key , name varchar(64)); -- 成績表(學生-學科 多對多) drop table if exists ustc_score; create table ustc_score(id int primary key , student_id int, subject_id int, result int);
對于 ZJU 學校而言,建表語句為:
-- 學科表 drop table if exists subject; create table subject(id int primary key , name varchar(64)); -- 學生表 drop table if exists zju_student; create table zju_student (id int primary key , name varchar(64)); -- 成績表(學生-學科 多對多) drop table if exists zju_score; create table zju_score(id int primary key , student_id int, subject_id int, result int);
我們的成績查詢系統(tǒng)會安裝在不同的學校,并且校名是動態(tài)可配的,因此該成績查詢系統(tǒng)需要根據(jù)配置文件動態(tài)修改 sql 語句表名的功能。
實現(xiàn)
源碼地址: https://github.com/foolishflyfox/blog/tree/main/backend-code/mp-dynamic-tablename
數(shù)據(jù)庫初始化
創(chuàng)建程序需要的表:
create database if not exists mp_dynamic_tablename_test;
use mp_dynamic_tablename_test;
-- 學科表
drop table if exists subject;
create table subject(id int primary key , name varchar(64));
insert into subject(id, name) values (1, 'Math'), (2, 'English'), (3, 'Chinese');
-- 學生表
drop table if exists student;
create table student (id int primary key , name varchar(64));
insert into student(id, name) values(1, 'aaa'), (2, 'bbb');
-- 成績表(學生-學科 多對多)
drop table if exists score;
create table score(id int primary key , student_id int, subject_id int, result int);
insert into score(id, student_id, subject_id, result) values (1, 1, 1, 74), (2, 1, 2, 83), (3, 1, 3, 69),
(4, 2, 1, 91), (5, 2, 3, 87);
-- 指定前綴 ustc 的表
-- 學生表
drop table if exists ustc_student;
create table ustc_student (id int primary key , name varchar(64));
insert into ustc_student(id, name) values(1, 'u_aaa'), (2, 'u_bbb');
-- 成績表(學生-學科 多對多)
drop table if exists ustc_score;
create table ustc_score(id int primary key , student_id int, subject_id int, result int);
insert into ustc_score(id, student_id, subject_id, result) values (1, 1, 1, 89), (2, 1, 2, 81), (3, 1, 3, 32),
(4, 2, 1, 71), (5, 2, 2, 77);
-- 指定前綴 zju 的表
-- 學生表
drop table if exists zju_student;
create table zju_student (id int primary key , name varchar(64));
insert into zju_student(id, name) values(5, 'z_aaa'), (6, 'z_bbb');
-- 成績表(學生-學科 多對多)
drop table if exists zju_score;
create table zju_score(id int primary key , student_id int, subject_id int, result int);
insert into zju_score(id, student_id, subject_id, result) values (1, 5, 1, 91), (2, 5, 2, 66), (3, 5, 3, 85),
(4, 6, 1, 48), (5, 6, 2, 59);
依賴包
需要引入 spring-boot-starter-web、spring-boot-starter-test、spring-boot-configuration-processor、mybatis-plus-boot-starter、mysql-connector-java、lombok 庫。
配置讀取類
我們先定義一個配置讀取類,用于獲取動態(tài)配置的學校以及需要動態(tài)添加學校前綴的表名。
package cn.fff.config.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.HashSet;
import java.util.Set;
@Component
@ConfigurationProperties("school")
@Data
public class SchoolProperties {
/** 學校名,動態(tài)表名會添加前綴: 學校名_ */
private String name;
/** 需要動態(tài)添加前綴的表 */
private Set<String> dynamicTables = new HashSet<>();
}
為 application.yml 添加如下配置:
school:
name: ustc
dynamic-tables:
- student
- score
表示需要為 student 和 score 動態(tài)添加前綴 ustc,即查詢 student 表時會動態(tài)替換為 ustc_student,查詢 score 表時會動態(tài)替換為 ustc_score。如果 school.name 修改為 zju,則查詢 student 表時會動態(tài)替換為 zju_student,查詢 score 表時會動態(tài)替換為 zju_score。
設(shè)置 mybatis-plus 插件
實體類、mapper、服務(wù)類的創(chuàng)建比較基礎(chǔ),此處略過,可直接查看源碼。動態(tài)表面主要通過創(chuàng)建一個 mybatis 插件實現(xiàn):
package cn.fff.config.mp;
import cn.fff.config.properties.SchoolProperties;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
@Configuration
public class DynamicTableNameConfig {
@Autowired
private SchoolProperties schoolProperties;
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor());
return interceptor;
}
private DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor() {
DynamicTableNameInnerInterceptor innerInterceptor = new DynamicTableNameInnerInterceptor();
innerInterceptor.setTableNameHandler((sql, tableName) -> {
String newTableName = tableName;
// 配置了學校名并且當前查詢的表名在指定配置中,則添加表名前綴
if (StringUtils.hasLength(schoolProperties.getName())
&& schoolProperties.getDynamicTables().contains(tableName)) {
newTableName = schoolProperties.getName() + "_" + tableName;
}
return newTableName;
});
return innerInterceptor;
}
}
測試
在 test 中創(chuàng)建一個測試類 ScoreServiceTest :
package cn.fff;
import cn.fff.entity.StudentScore;
import cn.fff.service.ScoreService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class ScoreServiceTest {
@Autowired
private ScoreService scoreService;
@Test
public void testDynameTableName() {
List<StudentScore> studentScores = scoreService.queryStudentScore();
studentScores.forEach(e -> System.out.printf("%s %s %d\n", e.getStudentName(), e.getSubjectName(), e.getScore()));
}
}
當 school.name 為 ustc 時,輸出為:
u_aaa Math 89 u_aaa English 81 u_aaa Chinese 32 u_bbb Math 71 u_bbb English 77
當 school.name 為 zju 時,輸出為:
z_aaa Math 91 z_aaa English 66 z_aaa Chinese 85 z_bbb Math 48 z_bbb English 59
這樣我們就實現(xiàn)了根據(jù)配置動態(tài)切換操作表名的功能。
到此這篇關(guān)于mybatis-plus 實現(xiàn)查詢表名動態(tài)修改的示例代碼的文章就介紹到這了,更多相關(guān)mybatis-plus 查詢表名動態(tài)修改內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實現(xiàn)發(fā)送郵件任務(wù)
這篇文章主要為大家詳細介紹了SpringBoot實現(xiàn)發(fā)送郵件任務(wù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02
Java刪除ArrayList中的重復(fù)元素的兩種方法
在Java編程中,ArrayList是一種常用的集合類,它允許我們存儲一組元素,在某些情況下,我們可能需要移除其中重復(fù)的元素,只保留唯一的元素,下面介紹兩種常見的刪除ArrayList中重復(fù)元素的方法,需要的朋友可以參考下2024-12-12
微服務(wù)?Spring?Boot?整合?Redis?BitMap?實現(xiàn)?簽到與統(tǒng)計功能
這篇文章主要介紹了微服務(wù)?Spring?Boot?整合?Redis?BitMap?實現(xiàn)?簽到與統(tǒng)計功能,文章簡單介紹了Redis BitMap 基本用法結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-01-01
Springboot+Bootstrap實現(xiàn)增刪改查實戰(zhàn)
這篇文章主要介紹了Springboot+Bootstrap實現(xiàn)增刪改查實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
Java volatile關(guān)鍵字原理剖析與實例講解
volatile是Java提供的一種輕量級的同步機制,Java?語言包含兩種內(nèi)在的同步機制:同步塊(或方法)和?volatile?變量,本文將詳細為大家總結(jié)Java volatile關(guān)鍵字,通過詳細的代碼示例給大家介紹的非常詳細,需要的朋友可以參考下2023-07-07
spring-boot-maven-plugin?配置有啥用
這篇文章主要介紹了spring-boot-maven-plugin?配置是干啥的,這個是SpringBoot的Maven插件,主要用來打包的,通常打包成jar或者war文件,本文通過示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-08-08
Mybatis-plus 批量插入太慢的問題解決(提升插入性能)
公司使用的Mybatis-Plus操作SQL,用過Mybatis-Plus的小伙伴一定知道他有很多API提供給我們使用,但是批量插入大數(shù)據(jù)太慢應(yīng)該怎么解決,本文就詳細的介紹一下,感興趣的可以了解一下2021-11-11

