| | |
| | | package com.moral.api.config.mybatis; |
| | | |
| | | import com.baomidou.mybatisplus.core.parser.ISqlParser; |
| | | import com.baomidou.mybatisplus.annotation.DbType; |
| | | import com.baomidou.mybatisplus.extension.parsers.DynamicTableNameParser; |
| | | import com.baomidou.mybatisplus.extension.parsers.ITableNameHandler; |
| | | 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 com.moral.api.entity.HistoryFiveMinutely; |
| | | import com.moral.api.mapper.HistoryFiveMinutelyMapper; |
| | | import com.moral.api.service.HistorySecondUavService; |
| | | import com.moral.api.service.impl.HistorySecondUavServiceImpl; |
| | | import com.moral.api.service.impl.OrganizationServiceImpl; |
| | | import org.apache.ibatis.reflection.MetaObject; |
| | | import org.apache.ibatis.session.SqlSessionFactory; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | |
| | | import java.util.*; |
| | | |
| | | |
| | | @Configuration |
| | | /*@Configuration*/ |
| | | public class MybatisPlusConfig { |
| | | |
| | | /** |
| | | * 分页插件 |
| | | */ |
| | | @Bean |
| | | public PaginationInterceptor paginationInterceptor() { |
| | | PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); |
| | | return paginationInterceptor; |
| | | } |
| | | public static ThreadLocal<String> tableName = new ThreadLocal<>(); |
| | | |
| | | /** |
| | | * @Description: 设置动态表名 |
| | | * @Param: [] |
| | | * @return: com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/7/15 |
| | | */ |
| | | @Bean |
| | | public MybatisPlusInterceptor mybatisPlusInterceptor(){ |
| | | public MybatisPlusInterceptor mybatisPlusInterceptor() { |
| | | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); |
| | | |
| | | //分页插件 |
| | | interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); |
| | | |
| | | // 动态表名SQL解析器 |
| | | DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor(); |
| | | HashMap<String, TableNameHandler> map = new HashMap<String, TableNameHandler>(); |
| | | map.put("history_five_minutely",(sql,tableName)->{ Date date = new Date(); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); |
| | | String month = sdf.format(date); |
| | | return tableName+"_"+month;}); |
| | | 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; |
| | | } |
| | | |
| | | |
| | | } |