package net.shapelight.common.config; 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; import net.shapelight.common.handler.CustomizeTableNameHandler; import net.shapelight.common.handler.TimeHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * mybatis-plus配置 * * */ @Configuration public class MybatisPlusConfig { @Bean public TimeHandler metaObjectHandler() { return new TimeHandler(); } /** * 分表插件 */ // 最新版 @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 分表插件 interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor(); dynamicTableNameInnerInterceptor.setTableNameHandler( //可以传多个表名参数,指定哪些表使用MonthTableNameHandler处理表名称 new CustomizeTableNameHandler("ten_person_extract,mobile_device,mobile_package_order,mobile_call_logs") ); //以拦截器的方式处理表名称 interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor); return interceptor; } // @Bean // public ISqlInjector sqlInjector() { // return new LogicSqlInjector(); // } /*// @Bean // public PaginationInterceptor paginationInterceptor() { // PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); // DynamicTableNameParser dynamicTableNameParser = new DynamicTableNameParser(); // // 创建SQL解析器集合 // List sqlParserList = new ArrayList<>(); // // Map 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(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; // }*/ }