提交 6cc797ef 编写于 作者: J jarvis

Merge branch 'dev' of https://gitee.com/zlt2000/microservices-platform into dev

...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
<redisson-starter.version>3.16.1</redisson-starter.version> <redisson-starter.version>3.16.1</redisson-starter.version>
<easyCaptcha.version>1.6.2</easyCaptcha.version> <easyCaptcha.version>1.6.2</easyCaptcha.version>
<hutool.version>5.7.20</hutool.version> <hutool.version>5.7.20</hutool.version>
<mybatis-plus-boot-starter.version>3.4.0</mybatis-plus-boot-starter.version> <mybatis-plus-boot-starter.version>3.5.1</mybatis-plus-boot-starter.version>
<aliyun-sdk-oss>3.8.1</aliyun-sdk-oss> <aliyun-sdk-oss>3.8.1</aliyun-sdk-oss>
<qiniu-java-sdk>7.2.28</qiniu-java-sdk> <qiniu-java-sdk>7.2.28</qiniu-java-sdk>
<easypoi.version>4.1.3</easypoi.version> <easypoi.version>4.1.3</easypoi.version>
......
...@@ -56,9 +56,4 @@ public class FileInfo extends Model<FileInfo> { ...@@ -56,9 +56,4 @@ public class FileInfo extends Model<FileInfo> {
private Date createTime; private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE) @TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime; private Date updateTime;
@Override
protected Serializable pkVal() {
return this.id;
}
} }
...@@ -27,9 +27,4 @@ public class SuperEntity<T extends Model<?>> extends Model<T> { ...@@ -27,9 +27,4 @@ public class SuperEntity<T extends Model<?>> extends Model<T> {
private Date createTime; private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE) @TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime; private Date updateTime;
@Override
protected Serializable pkVal() {
return this.id;
}
} }
...@@ -42,7 +42,7 @@ public class SuperServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, ...@@ -42,7 +42,7 @@ public class SuperServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,
) { ) {
if (lock != null) { if (lock != null) {
//判断记录是否已存在 //判断记录是否已存在
int count = super.count(countWrapper); long count = super.count(countWrapper);
if (count == 0) { if (count == 0) {
return super.save(entity); return super.save(entity);
} else { } else {
......
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; ...@@ -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 { ...@@ -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
......
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 { ...@@ -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 { ...@@ -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())
);
};
}
} }
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));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册