郑永安
2023-06-19 59e91a4e9ddaf23cebb12993c774aa899ab22d16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.gk.firework.Config.DataSource;
 
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.util.HashMap;
 
/**
 * @author : jingjy
 * @date : 2021/4/1 17:15
 */
@Configuration
@MapperScan("com.gk.firework.Mapper")
public class MybatisPlusConfig {
 
    public static ThreadLocal<String> myTableName = new ThreadLocal<>();
 
    @Value("${slice}")
    private String tableSlice;
 
    /**
     * 通过拦截器,拦截SQL请求,可更换表名
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        HashMap<String, TableNameHandler> map = new HashMap<String, TableNameHandler>(2) {{
            put("deliveryorder", (sql, tableName) -> tableName + tableSlice);
            put("deliverydetail", (sql, tableName) -> tableName + tableSlice);
            put("saleorder", (sql, tableName) -> tableName + tableSlice);
            put("saleorderdetail", (sql, tableName) -> tableName + tableSlice);
            put("customer", (sql, tableName) -> tableName + tableSlice);
            put("entryorder", (sql, tableName) -> tableName + tableSlice);
            put("entrydetail", (sql, tableName) -> tableName + tableSlice);
 
        }};
        dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        return interceptor;
    }
 
}