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 myTableName = new ThreadLocal<>(); @Value("${slice}") private String tableSlice; /** * 通过拦截器,拦截SQL请求,可更换表名 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor(); HashMap map = new HashMap(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; } }