From 59e91a4e9ddaf23cebb12993c774aa899ab22d16 Mon Sep 17 00:00:00 2001 From: 郑永安 <zyazyz250@sina.com> Date: 星期一, 19 六月 2023 14:22:45 +0800 Subject: [PATCH] 描述 --- src/main/java/com/gk/firework/Config/DataSource/MybatisPlusConfig.java | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 48 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/gk/firework/Config/DataSource/MybatisPlusConfig.java b/src/main/java/com/gk/firework/Config/DataSource/MybatisPlusConfig.java new file mode 100644 index 0000000..588b788 --- /dev/null +++ b/src/main/java/com/gk/firework/Config/DataSource/MybatisPlusConfig.java @@ -0,0 +1,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; + } + +} -- Gitblit v1.9.2