Merge branch 'dev' of https://gitee.com/zlt2000/microservices-platform into dev
This commit is contained in:
@@ -27,9 +27,4 @@ public class SuperEntity<T extends Model<?>> extends Model<T> {
|
||||
private Date createTime;
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private Date updateTime;
|
||||
|
||||
@Override
|
||||
protected Serializable pkVal() {
|
||||
return this.id;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ public class SuperServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,
|
||||
) {
|
||||
if (lock != null) {
|
||||
//判断记录是否已存在
|
||||
int count = super.count(countWrapper);
|
||||
long count = super.count(countWrapper);
|
||||
if (count == 0) {
|
||||
return super.save(entity);
|
||||
} else {
|
||||
|
||||
+13
-16
@@ -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
|
||||
|
||||
+5
-29
@@ -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())
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+44
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user