核心代码
- mybatisplus的配置
@Configuration
public class MybatisPlusConfig {
// 这里是存储需要动态变化的表,防止乱操作
static List<String> tableList() {
List<String> tables = new ArrayList<>();
//表名
tables.add("user");
return tables;
}
//拦截器,获取到表名给替换
@Bean
public MybatisPlusInterceptor dynamicTableNameInnerInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
//这里可以自定义,只要返回正确的表名就行了
String newTable = null;
for (String table : tableList()) {
newTable = RequestDataHelper.getRequestData(table);
if (table.equals(tableName) && newTable != null) {
tableName = newTable;
break;
}
}
return tableName;
});
interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
return interceptor;
}
}
- 表更换的辅助类
public class RequestDataHelper {
/**
* 请求参数存取
*/
private static final ThreadLocal<Map<String, Object>> REQUEST_DATA = new ThreadLocal<>();
/**
* 设置请求参数
*
* @param requestData 请求参数 MAP 对象
*/
public static void setRequestData(Map<String, Object> requestData) {
REQUEST_DATA.set(requestData);
}
/**
* 获取请求参数
*
* @param param 请求参数
* @return 请求参数 MAP 对象
*/
public static <T> T getRequestData(String param) {
Map<String, Object> dataMap = getRequestData();
if (CollectionUtils.isNotEmpty(dataMap)) {
return (T) dataMap.get(param);
}
return null;
}
/**
* 获取请求参数
*
* @return 请求参数 MAP 对象
*/
public static Map<String, Object> getRequestData() {
return REQUEST_DATA.get();
}
}
*测试代码
@Test
public void test1() {
//表名:user_01
RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
put("user", "user" + "_01");
}});
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
//表名:user
RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
put("user", "user");
}});
List<User> users1 = userMapper.selectList(null);
users1.forEach(System.out::println);
}
以下代码是对上面代码的详解
完整项目介绍:
-
项目目录
-
依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.keeson.www</groupId>
<artifactId>DynamicTableName</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DynamicTableName</name>
<description>DynamicTableName</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 配置
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false
username: root
password: 123456
- 代码
1.实体类
@Data
public class User extends Model<User> {
//主键
private Long id;
//用户名
private String userName;
//密码
private String password;
private Integer isDelete;
private Integer age;
}
2.数据访问mapper
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
3.springboot启动类
@SpringBootApplication
public class DynamicTableNameApplication {
public static void main(String[] args) {
SpringApplication.run(DynamicTableNameApplication.class, args);
}
}
4.配置类1
@Configuration
public class MybatisPlusConfig {
// 这里是存储需要动态变化的表,防止乱操作
static List<String> tableList() {
List<String> tables = new ArrayList<>();
//表名
tables.add("user");
return tables;
}
//拦截器,获取到表名给替换
@Bean
public MybatisPlusInterceptor dynamicTableNameInnerInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
//这里可以自定义,只要返回正确的表名就行了
String newTable = null;
for (String table : tableList()) {
newTable = RequestDataHelper.getRequestData(table);
if (table.equals(tableName) && newTable != null) {
tableName = newTable;
break;
}
}
return tableName;
});
interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
return interceptor;
}
}
5.配置类2
public class RequestDataHelper {
/**
* 请求参数存取
*/
private static final ThreadLocal<Map<String, Object>> REQUEST_DATA = new ThreadLocal<>();
/**
* 设置请求参数
*
* @param requestData 请求参数 MAP 对象
*/
public static void setRequestData(Map<String, Object> requestData) {
REQUEST_DATA.set(requestData);
}
/**
* 获取请求参数
*
* @param param 请求参数
* @return 请求参数 MAP 对象
*/
public static <T> T getRequestData(String param) {
Map<String, Object> dataMap = getRequestData();
if (CollectionUtils.isNotEmpty(dataMap)) {
return (T) dataMap.get(param);
}
return null;
}
/**
* 获取请求参数
*
* @return 请求参数 MAP 对象
*/
public static Map<String, Object> getRequestData() {
return REQUEST_DATA.get();
}
}
- 测试代码
@SpringBootTest
class DynamicTableNameApplicationTests {
@Autowired
UserMapper userMapper;
@Test
void contextLoads() {
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
@Test
public void test1() {
RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
put("user", "user" + "_01");
}});
//表名会被动态替换成"TestUser_2022_05"
//如果实际表名与实体类与不同,可先在实体类类注明表名@TableName("TestUser")
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
put("user", "user");
}});
List<User> users1 = userMapper.selectList(null);
users1.forEach(System.out::println);
}
}