适配 mybatis-plus 拦截器新配置方式

This commit is contained in:
zhult13
2022-05-06 23:22:55 +08:00
parent b7aa931fb2
commit 7526f18d4a
3 changed files with 62 additions and 45 deletions
@@ -1,12 +1,12 @@
package com.central.db.config; 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.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
import com.central.common.properties.TenantProperties; import com.central.common.properties.TenantProperties;
import com.central.db.interceptor.CustomTenantInterceptor;
import com.central.db.properties.MybatisPlusAutoFillProperties; import com.central.db.properties.MybatisPlusAutoFillProperties;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -26,10 +26,7 @@ import org.springframework.context.annotation.Bean;
@EnableConfigurationProperties(MybatisPlusAutoFillProperties.class) @EnableConfigurationProperties(MybatisPlusAutoFillProperties.class)
public class MybatisPlusAutoConfigure { public class MybatisPlusAutoConfigure {
@Autowired @Autowired
private TenantHandler tenantHandler; private TenantLineHandler tenantLineHandler;
@Autowired
private ISqlParserFilter sqlParserFilter;
@Autowired @Autowired
private TenantProperties tenantProperties; private TenantProperties tenantProperties;
@@ -41,17 +38,17 @@ public class MybatisPlusAutoConfigure {
* 分页插件,自动识别数据库类型 * 分页插件,自动识别数据库类型
*/ */
@Bean @Bean
public PaginationInterceptor paginationInterceptor() { public MybatisPlusInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
boolean enableTenant = tenantProperties.getEnable(); boolean enableTenant = tenantProperties.getEnable();
//是否开启多租户隔离 //是否开启多租户隔离
if (enableTenant) { if (enableTenant) {
TenantSqlParser tenantSqlParser = new TenantSqlParser() CustomTenantInterceptor tenantInterceptor = new CustomTenantInterceptor(
.setTenantHandler(tenantHandler); tenantLineHandler, tenantProperties.getIgnoreSqls());
paginationInterceptor.setSqlParserList(CollUtil.toList(tenantSqlParser)); mpInterceptor.addInnerInterceptor(tenantInterceptor);
paginationInterceptor.setSqlParserFilter(sqlParserFilter);
} }
return paginationInterceptor; return mpInterceptor;
} }
@Bean @Bean
@@ -1,14 +1,11 @@
package com.central.db.config; package com.central.db.config;
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter; import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.core.parser.SqlParserHelper;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
import com.central.common.context.TenantContextHolder; import com.central.common.context.TenantContextHolder;
import com.central.common.properties.TenantProperties; import com.central.common.properties.TenantProperties;
import net.sf.jsqlparser.expression.Expression; import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.NullValue; import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.StringValue; import net.sf.jsqlparser.expression.StringValue;
import org.apache.ibatis.mapping.MappedStatement;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@@ -25,13 +22,13 @@ public class TenantAutoConfigure {
private TenantProperties tenantProperties; private TenantProperties tenantProperties;
@Bean @Bean
public TenantHandler tenantHandler() { public TenantLineHandler tenantLineHandler() {
return new TenantHandler() { return new TenantLineHandler() {
/** /**
* 获取租户id * 获取租户id
*/ */
@Override @Override
public Expression getTenantId(boolean where) { public Expression getTenantId() {
String tenant = TenantContextHolder.getTenant(); String tenant = TenantContextHolder.getTenant();
if (tenant != null) { if (tenant != null) {
return new StringValue(TenantContextHolder.getTenant()); return new StringValue(TenantContextHolder.getTenant());
@@ -39,37 +36,16 @@ public class TenantAutoConfigure {
return new NullValue(); return new NullValue();
} }
/**
* 获取租户列名
*/
@Override
public String getTenantIdColumn() {
return "tenant_id";
}
/** /**
* 过滤不需要根据租户隔离的表 * 过滤不需要根据租户隔离的表
* @param tableName 表名 * @param tableName 表名
*/ */
@Override @Override
public boolean doTableFilter(String tableName) { public boolean ignoreTable(String tableName) {
return tenantProperties.getIgnoreTables().stream().anyMatch( return tenantProperties.getIgnoreTables().stream().anyMatch(
(e) -> e.equalsIgnoreCase(tableName) (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())
);
};
}
} }
@@ -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
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
public class CustomTenantInterceptor extends TenantLineInnerInterceptor {
private List<String> ignoreSqls;
public CustomTenantInterceptor(TenantLineHandler tenantLineHandler, List<String> 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));
}
}