|  |  |  | 
|---|
|  |  |  | package com.moral.api.config.mybatis; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.annotation.DbType; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; | 
|---|
|  |  |  | import org.springframework.context.annotation.Bean; | 
|---|
|  |  |  | import org.springframework.context.annotation.Configuration; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Configuration | 
|---|
|  |  |  | public class MybatisPlusConfig { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static ThreadLocal<String> tableName = new ThreadLocal<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 分页插件 | 
|---|
|  |  |  | 
|---|
|  |  |  | return page; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Bean | 
|---|
|  |  |  | public MybatisPlusInterceptor mybatisPlusInterceptor() { | 
|---|
|  |  |  | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //分页插件 | 
|---|
|  |  |  | interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 动态表名SQL解析器 | 
|---|
|  |  |  | DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor(); | 
|---|
|  |  |  | HashMap<String, TableNameHandler> map = new HashMap<>(); | 
|---|
|  |  |  | map.put("history_hourly", (sql, tableName) -> tableName+this.tableName.get()); | 
|---|
|  |  |  | map.put("history_five_minutely", (sql, tableName) -> tableName+this.tableName.get()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map); | 
|---|
|  |  |  | interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return interceptor; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|