package com.ruoyi.framework.config; import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; import com.baomidou.mybatisplus.core.toolkit.PluginUtils; import com.baomidou.mybatisplus.core.toolkit.TableNameParser; import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler; import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; @Data @NoArgsConstructor @AllArgsConstructor @SuppressWarnings({"rawtypes"}) public class DynamicTableNameInnerInterceptor implements InnerInterceptor { private Map<String, TableNameHandler> tableNameHandlerMap; @Override public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); if (InterceptorIgnoreHelper.willIgnoreDynamicTableName(ms.getId())) return; mpBs.sql(this.changeTable(mpBs.sql())); } @Override public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) { PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh); MappedStatement ms = mpSh.mappedStatement(); SqlCommandType sct = ms.getSqlCommandType(); if (sct == SqlCommandType.INSERT || sct == SqlCommandType.UPDATE || sct == SqlCommandType.DELETE) { if (InterceptorIgnoreHelper.willIgnoreDynamicTableName(ms.getId())) return; PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql(); mpBs.sql(this.changeTable(mpBs.sql())); } } protected String changeTable(String sql) { TableNameParser parser = new TableNameParser(sql); List<TableNameParser.SqlToken> names = new ArrayList<>(); parser.accept(names::add); StringBuilder builder = new StringBuilder(); int last = 0; for (TableNameParser.SqlToken name : names) { int start = name.getStart(); if (start != last) { builder.append(sql, last, start); String value = name.getValue(); TableNameHandler handler = tableNameHandlerMap.get(value); if (handler != null) { builder.append(handler.dynamicTableName(sql, value)); } else { builder.append(value); } } last = name.getEnd(); } if (last != sql.length()) { builder.append(sql.substring(last)); } return builder.toString(); } }
package com.ruoyi.framework.config; import cn.hutool.core.lang.Console; import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler; public class MyTableNameHandler implements TableNameHandler { /** * @param sql 原始SQL * @param tableName 表名 * @return 动态表名 */ @Override public String dynamicTableName(String sql, String tableName) { Console.log("sql:{}",sql); // 模拟获取月份参数,实际应该从参数中获取 String[] month = {"epay"}; // 随机获取 String dynamicTableName = month[0] + "." + tableName ; System.err.println("动态查询表:" + dynamicTableName); return dynamicTableName; } }
@Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 多租户插件 interceptor.addInnerInterceptor(tenantLineInnerInterceptor()); // 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add // PaginationInnerInterceptor // 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false // 分页插件 // interceptor.addInnerInterceptor(paginationInnerInterceptor()); // 添加动态表名插件 DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor(); TableNameHandler tableNameHandler = new MyTableNameHandler(); Map<String, TableNameHandler> tableNameHandlerMap = new HashMap<>(); tableNameHandlerMap.put("sys_user", tableNameHandler); // sys_user 表配置动态表名插件 dynamicTableNameInnerInterceptor.setTableNameHandlerMap(tableNameHandlerMap); interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor); // 乐观锁插件 interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor()); // 阻断插件 interceptor.addInnerInterceptor(blockAttackInnerInterceptor()); return interceptor; }
标签:插件,String,表名,plus,sql,mybatis,import,interceptor,com From: https://www.cnblogs.com/wujf/p/17136892.html