Guwan-smartSchool/shapelight-admin/src/main/java/net/shapelight/common/config/MybatisPlusConfig.java

111 lines
4.4 KiB
Java
Raw Normal View History

2020-10-19 17:44:19 +08:00
package net.shapelight.common.config;
2024-11-11 16:56:43 +08:00
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
2024-11-14 13:05:57 +08:00
import net.shapelight.common.handler.CustomizeTableNameHandler;
2024-11-18 18:03:31 +08:00
import net.shapelight.common.handler.SnowflakeHandler;
2024-11-15 18:10:29 +08:00
import net.shapelight.common.handler.TimeHandler;
2020-10-19 17:44:19 +08:00
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* mybatis-plus配置
*
*
*/
@Configuration
public class MybatisPlusConfig {
2024-11-15 18:10:29 +08:00
@Bean
public TimeHandler metaObjectHandler() {
return new TimeHandler();
}
2020-10-19 17:44:19 +08:00
/**
2024-11-14 13:05:57 +08:00
* 分表插件
2020-10-19 17:44:19 +08:00
*/
2024-11-11 16:56:43 +08:00
// 最新版
2020-10-19 17:44:19 +08:00
@Bean
2024-11-11 16:56:43 +08:00
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
2024-11-14 13:05:57 +08:00
// 分表插件
2024-11-11 16:56:43 +08:00
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
dynamicTableNameInnerInterceptor.setTableNameHandler(
//可以传多个表名参数指定哪些表使用MonthTableNameHandler处理表名称
2024-11-18 19:10:50 +08:00
new CustomizeTableNameHandler("ten_person_extract","mobile_device","mobile_package_order","mobile_call_logs","mobile_contact")
2024-11-11 16:56:43 +08:00
);
//以拦截器的方式处理表名称
interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
return interceptor;
2020-10-19 17:44:19 +08:00
}
// @Bean
// public ISqlInjector sqlInjector() {
// return new LogicSqlInjector();
// }
2024-11-11 16:56:43 +08:00
/*// @Bean
2020-10-19 17:44:19 +08:00
// public PaginationInterceptor paginationInterceptor() {
// PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// DynamicTableNameParser dynamicTableNameParser = new DynamicTableNameParser();
// // 创建SQL解析器集合
// List<ISqlParser> sqlParserList = new ArrayList<>();
//
// Map<String,ITableNameHandler> tableNameHandlerMap = new HashMap<>();
// // Map的key就是需要替换的原始表名
// tableNameHandlerMap.put("ten_cell",new ITableNameHandler(){
// @Override
// public String dynamicTableName(MetaObject metaObject, String sql, String tableName) {
// // 自定义表名规则或者从配置文件、request上下文中读取
//
// // 假设这里的用户表根据年份来进行分表操作
// Date date = new Date();
// String year = String.format("%tY", date);
// // 返回最后需要操作的表名sys_user_2019
// return "ten_cell_" + year;
// }
// });
//
// tableNameHandlerMap.put("ten_cell",new ITableNameHandler(){
// @Override
// public String dynamicTableName(MetaObject metaObject, String sql, String tableName) {
// // 自定义表名规则或者从配置文件、request上下文中读取
//
// // 假设这里的用户表根据年份来进行分表操作
// Date date = new Date();
// String year = String.format("%tY", date);
// // 返回最后需要操作的表名sys_user_2019
// return "ten_cell_" + 2019;
// }
// });
//
//
// dynamicTableNameParser.setTableNameHandlerMap(tableNameHandlerMap);
// sqlParserList.add(dynamicTableNameParser);
// paginationInterceptor.setSqlParserList(sqlParserList);
//
//// dynamicTableNameParser.setTableNameHandlerMap(new HashMap<String, ITableNameHandler>(2) {{
//// put("sysUserEntity", (metaObject, sql, tableName) -> {
//// // metaObject 可以获取传入参数,这里实现你自己的动态规则
//// String year = "_2018";
//// int random = new Random().nextInt(10);
//// if (random % 2 == 1) {
//// year = "_2019";
//// }
//// return tableName + year;
//// return tableName + (String)metaObject.getValue("schema");
//// });
//// }});
//// paginationInterceptor.setSqlParserList(Collections.singletonList(dynamicTableNameParser));
// return paginationInterceptor;
2024-11-11 16:56:43 +08:00
// }*/
2020-10-19 17:44:19 +08:00
}