diff --git a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/MybatisPlusAutoConfigure.java b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/MybatisPlusAutoConfigure.java index 80132289..2b1e855e 100644 --- a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/MybatisPlusAutoConfigure.java +++ b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/MybatisPlusAutoConfigure.java @@ -1,12 +1,12 @@ package com.central.db.config; -import cn.hutool.core.collection.CollUtil; +import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; -import com.baomidou.mybatisplus.core.parser.ISqlParserFilter; -import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; -import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler; -import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.central.common.properties.TenantProperties; +import com.central.db.interceptor.CustomTenantInterceptor; import com.central.db.properties.MybatisPlusAutoFillProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -26,10 +26,7 @@ import org.springframework.context.annotation.Bean; @EnableConfigurationProperties(MybatisPlusAutoFillProperties.class) public class MybatisPlusAutoConfigure { @Autowired - private TenantHandler tenantHandler; - - @Autowired - private ISqlParserFilter sqlParserFilter; + private TenantLineHandler tenantLineHandler; @Autowired private TenantProperties tenantProperties; @@ -41,17 +38,17 @@ public class MybatisPlusAutoConfigure { * 分页插件,自动识别数据库类型 */ @Bean - public PaginationInterceptor paginationInterceptor() { - PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); + public MybatisPlusInterceptor paginationInterceptor() { + MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor(); + mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); boolean enableTenant = tenantProperties.getEnable(); //是否开启多租户隔离 if (enableTenant) { - TenantSqlParser tenantSqlParser = new TenantSqlParser() - .setTenantHandler(tenantHandler); - paginationInterceptor.setSqlParserList(CollUtil.toList(tenantSqlParser)); - paginationInterceptor.setSqlParserFilter(sqlParserFilter); + CustomTenantInterceptor tenantInterceptor = new CustomTenantInterceptor( + tenantLineHandler, tenantProperties.getIgnoreSqls()); + mpInterceptor.addInnerInterceptor(tenantInterceptor); } - return paginationInterceptor; + return mpInterceptor; } @Bean diff --git a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/TenantAutoConfigure.java b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/TenantAutoConfigure.java index 40f38d6e..8778dd6d 100644 --- a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/TenantAutoConfigure.java +++ b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/TenantAutoConfigure.java @@ -1,14 +1,11 @@ package com.central.db.config; -import com.baomidou.mybatisplus.core.parser.ISqlParserFilter; -import com.baomidou.mybatisplus.core.parser.SqlParserHelper; -import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler; +import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; import com.central.common.context.TenantContextHolder; import com.central.common.properties.TenantProperties; import net.sf.jsqlparser.expression.Expression; import net.sf.jsqlparser.expression.NullValue; import net.sf.jsqlparser.expression.StringValue; -import org.apache.ibatis.mapping.MappedStatement; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; @@ -25,13 +22,13 @@ public class TenantAutoConfigure { private TenantProperties tenantProperties; @Bean - public TenantHandler tenantHandler() { - return new TenantHandler() { + public TenantLineHandler tenantLineHandler() { + return new TenantLineHandler() { /** * 获取租户id */ @Override - public Expression getTenantId(boolean where) { + public Expression getTenantId() { String tenant = TenantContextHolder.getTenant(); if (tenant != null) { return new StringValue(TenantContextHolder.getTenant()); @@ -39,37 +36,16 @@ public class TenantAutoConfigure { return new NullValue(); } - /** - * 获取租户列名 - */ - @Override - public String getTenantIdColumn() { - return "tenant_id"; - } - /** * 过滤不需要根据租户隔离的表 * @param tableName 表名 */ @Override - public boolean doTableFilter(String tableName) { + public boolean ignoreTable(String tableName) { return tenantProperties.getIgnoreTables().stream().anyMatch( (e) -> e.equalsIgnoreCase(tableName) ); } }; } - - /** - * 过滤不需要根据租户隔离的MappedStatement - */ - @Bean - public ISqlParserFilter sqlParserFilter() { - return metaObject -> { - MappedStatement ms = SqlParserHelper.getMappedStatement(metaObject); - return tenantProperties.getIgnoreSqls().stream().anyMatch( - (e) -> e.equalsIgnoreCase(ms.getId()) - ); - }; - } } diff --git a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/interceptor/CustomTenantInterceptor.java b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/interceptor/CustomTenantInterceptor.java new file mode 100644 index 00000000..f2083e7f --- /dev/null +++ b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/interceptor/CustomTenantInterceptor.java @@ -0,0 +1,44 @@ +package com.central.db.interceptor; + +import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; +import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; + +import java.sql.SQLException; +import java.util.List; + +/** + * MyBatis-plus租户拦截器 + * + * @author zlt + * @version 1.0 + * @date 2022/5/6 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +public class CustomTenantInterceptor extends TenantLineInnerInterceptor { + private List ignoreSqls; + + public CustomTenantInterceptor(TenantLineHandler tenantLineHandler, List ignoreSqls) { + super(tenantLineHandler); + this.ignoreSqls = ignoreSqls; + } + + @Override + public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds + , ResultHandler resultHandler, BoundSql boundSql) throws SQLException { + if (isIgnoreMappedStatement(ms.getId())) { + return; + } + super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql); + } + + private boolean isIgnoreMappedStatement(String msId) { + return ignoreSqls.stream().anyMatch((e) -> e.equalsIgnoreCase(msId)); + } +}