package com.ruoyi.framework.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; import com.ruoyi.common.extension.MpSqlInjector; import lombok.AllArgsConstructor; import net.sf.jsqlparser.expression.Expression; import net.sf.jsqlparser.expression.LongValue; @AllArgsConstructor @EnableTransactionManagement(proxyTargetClass = true) @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() { // manager_id = 1088248166370832385 // 獲取租戶 ID 值表達式,只支持單個 ID 值 @Override public Expression getTenantId() { // return new LongValue(1088248166370832385L); return new LongValue(1L); } // 這是 default 方法,默認返回 false 表示所有表都需要拼多租戶條件, // 這里設置 role表不需要該條件 @Override public boolean ignoreTable(String tableName) { // if ("role".equals(tableName)) { // return true; // } // return false; if (tableName.substring(0, 2).equals("at")) return false; return true; } @Override public String getTenantIdColumn() { return "tenantid"; } })); // interceptor.addInnerInterceptor(tenantLineInnerInterceptor()); // 如果用了分頁插件注意先 add TenantLineInnerInterceptor 再 add // PaginationInnerInterceptor // 用了分頁插件必須設置 MybatisConfiguration#useDeprecatedExecutor = false // 分頁插件 // interceptor.addInnerInterceptor(paginationInnerInterceptor()); // 樂觀鎖插件 interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor()); // 阻斷插件 interceptor.addInnerInterceptor(blockAttackInnerInterceptor()); return interceptor; } @SuppressWarnings("deprecation") @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> configuration.setUseDeprecatedExecutor(Boolean.FALSE); } @Bean public MpSqlInjector easySqlInjector() { return new MpSqlInjector(); } /** * 分頁插件,自動識別數據庫類型 https://baomidou.com/guide/interceptor-pagination.html */ public PaginationInnerInterceptor paginationInnerInterceptor() { PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); // 設置數據庫類型為mysql paginationInnerInterceptor.setDbType(DbType.MYSQL); // 設置最大單頁限制數量,默認 500 條,-1 不受限制 paginationInnerInterceptor.setMaxLimit(-1L); return paginationInnerInterceptor; } /** * 樂觀鎖插件 https://baomidou.com/guide/interceptor-optimistic-locker.html */ public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() { return new OptimisticLockerInnerInterceptor(); } /** * 如果是對全表的刪除或更新操作,就會終止該操作 * https://baomidou.com/guide/interceptor-block-attack.html */ public BlockAttackInnerInterceptor blockAttackInnerInterceptor() { return new BlockAttackInnerInterceptor(); } /** * sql性能規范插件(垃圾SQL攔截) 如有需要可以啟用 */ // public IllegalSQLInnerInterceptor illegalSQLInnerInterceptor() { // return new IllegalSQLInnerInterceptor(); // } /** * 自定義主鍵策略 https://baomidou.com/guide/id-generator.html */ // @Bean // public IdentifierGenerator idGenerator() { // return new CustomIdGenerator(); // } /** * 元對象字段填充控制器 https://baomidou.com/guide/auto-fill-metainfo.html */ // @Bean // public MetaObjectHandler metaObjectHandler() { // return new MyMetaObjectHandler(); // } /** * sql注入器配置 https://baomidou.com/guide/sql-injector.html */ // @Bean // public ISqlInjector sqlInjector() { // return new DefaultSqlInjector(); // } /** * TenantLineInnerInterceptor 多租戶插件 本項目已經實現完整的多租戶功能,但應用不廣泛,基本框架中刪除 */ // @Bean // public TenantLineInnerInterceptor tenantLineInnerInterceptor() // { // return new TenantLineInnerInterceptor(new TenantLineHandler() // { // /** // * 獲取租戶ID // * @return // */ // @Override // public Expression getTenantId() // { // String tenant = TenantContextHolder.getTenantId(); // if (tenant != null) // { // return new StringValue(TenantContextHolder.getTenantId()); // } // return new StringValue("99999"); // } // // /** // * 獲取多租戶的字段名 // * @return String // */ // @Override // public String getTenantIdColumn() // { // return tenantProperties.getColumn(); // } // // /** // * 過濾不需要根據租戶隔離的表 // * 這是 default 方法,默認返回 false 表示所有表都需要拼多租戶條件 // * @param tableName 表名 // */ // @Override // public boolean ignoreTable(String tableName) // { // return tenantProperties.getIgnores().stream().anyMatch((t) -> // t.equalsIgnoreCase(tableName)); // } // }); // } }
package com.ruoyi.framework.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; import com.ruoyi.common.extension.MpSqlInjector; import lombok.AllArgsConstructor; import net.sf.jsqlparser.expression.Expression; import net.sf.jsqlparser.expression.LongValue; @AllArgsConstructor @EnableTransactionManagement(proxyTargetClass = true) @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() { // manager_id = 1088248166370832385 // 獲取租戶 ID 值表達式,只支持單個 ID 值 @Override public Expression getTenantId() { // return new LongValue(1088248166370832385L); return new LongValue(1L); } // 這是 default 方法,默認返回 false 表示所有表都需要拼多租戶條件, // 這里設置 role表不需要該條件 @Override public boolean ignoreTable(String tableName) { // if ("role".equals(tableName)) { // return true; // } // return false; if (tableName.substring(0, 2).equals("at")) return false; return true; } @Override public String getTenantIdColumn() { return "tenantid"; } })); // interceptor.addInnerInterceptor(tenantLineInnerInterceptor()); // 如果用了分頁插件注意先 add TenantLineInnerInterceptor 再 add // PaginationInnerInterceptor // 用了分頁插件必須設置 MybatisConfiguration#useDeprecatedExecutor = false // 分頁插件 // interceptor.addInnerInterceptor(paginationInnerInterceptor()); // 樂觀鎖插件 interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor()); // 阻斷插件 interceptor.addInnerInterceptor(blockAttackInnerInterceptor()); return interceptor; } @SuppressWarnings("deprecation") @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> configuration.setUseDeprecatedExecutor(Boolean.FALSE); } @Bean public MpSqlInjector easySqlInjector() { return new MpSqlInjector(); } /** * 分頁插件,自動識別數據庫類型 https://baomidou.com/guide/interceptor-pagination.html */ public PaginationInnerInterceptor paginationInnerInterceptor() { PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); // 設置數據庫類型為mysql paginationInnerInterceptor.setDbType(DbType.MYSQL); // 設置最大單頁限制數量,默認 500 條,-1 不受限制 paginationInnerInterceptor.setMaxLimit(-1L); return paginationInnerInterceptor; } /** * 樂觀鎖插件 https://baomidou.com/guide/interceptor-optimistic-locker.html */ public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() { return new OptimisticLockerInnerInterceptor(); } /** * 如果是對全表的刪除或更新操作,就會終止該操作 * https://baomidou.com/guide/interceptor-block-attack.html */ public BlockAttackInnerInterceptor blockAttackInnerInterceptor() { return new BlockAttackInnerInterceptor(); } /** * sql性能規范插件(垃圾SQL攔截) 如有需要可以啟用 */ // public IllegalSQLInnerInterceptor illegalSQLInnerInterceptor() { // return new IllegalSQLInnerInterceptor(); // } /** * 自定義主鍵策略 https://baomidou.com/guide/id-generator.html */ // @Bean // public IdentifierGenerator idGenerator() { // return new CustomIdGenerator(); // } /** * 元對象字段填充控制器 https://baomidou.com/guide/auto-fill-metainfo.html */ // @Bean // public MetaObjectHandler metaObjectHandler() { // return new MyMetaObjectHandler(); // } /** * sql注入器配置 https://baomidou.com/guide/sql-injector.html */ // @Bean // public ISqlInjector sqlInjector() { // return new DefaultSqlInjector(); // } /** * TenantLineInnerInterceptor 多租戶插件 本項目已經實現完整的多租戶功能,但應用不廣泛,基本框架中刪除 */ // @Bean // public TenantLineInnerInterceptor tenantLineInnerInterceptor() // { // return new TenantLineInnerInterceptor(new TenantLineHandler() // { // /** // * 獲取租戶ID // * @return // */ // @Override // public Expression getTenantId() // { // String tenant = TenantContextHolder.getTenantId(); // if (tenant != null) // { // return new StringValue(TenantContextHolder.getTenantId()); // } // return new StringValue("99999"); // } // // /** // * 獲取多租戶的字段名 // * @return String // */ // @Override // public String getTenantIdColumn() // { // return tenantProperties.getColumn(); // } // // /** // * 過濾不需要根據租戶隔離的表 // * 這是 default 方法,默認返回 false 表示所有表都需要拼多租戶條件 // * @param tableName 表名 // */ // @Override // public boolean ignoreTable(String tableName) // { // return tenantProperties.getIgnores().stream().anyMatch((t) -> // t.equalsIgnoreCase(tableName)); // } // }); // } }