通过 mybatis-plus 实现表名的动态替换,即通过配置或入参动态选择不同的表。
下面通过一个例子来说明该需求: 我们需要为学校开发一个成绩管理系统,需要建立三张表: 学生表、科目表和成绩表,表的 ER 图如下所示。
对应的建表语句如下:
-- 学科表
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);
根据三张表级联查询成绩的查询语句为:
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;
现在又来了一个新需求,我们的这套成绩查询系统需要部署在不同学校的服务器上,因为每个学校的学生表和成绩表都要同步到教育局的服务器中,因此需要为这两个表添加学校前缀,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