|
|
@@ -1,56 +1,47 @@
|
|
|
-package com.nb.framework.config.mybatisplus;
|
|
|
+package com.nb.auth.config;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.util.ClassUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import com.baomidou.mybatisplus.annotation.DbType;
|
|
|
import com.baomidou.mybatisplus.annotation.TableName;
|
|
|
-import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.inner.*;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
|
|
import com.nb.auth.bean.LoginUser;
|
|
|
import com.nb.auth.utils.SecurityUtil;
|
|
|
import com.nb.core.entity.TenantGenericEntity;
|
|
|
-import com.nb.framework.config.mybatisplus.handler.CreateAndUpdateMetaObjectHandler;
|
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
|
import net.sf.jsqlparser.expression.StringValue;
|
|
|
import net.sf.jsqlparser.schema.Column;
|
|
|
-import org.mybatis.spring.annotation.MapperScan;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
|
-import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;
|
|
|
|
|
|
/**
|
|
|
- * mybatis-plus配置类
|
|
|
- *
|
|
|
- * @author Kevin
|
|
|
+ * @author lifang
|
|
|
+ * @version 1.0.0
|
|
|
+ * @ClassName DefaultTenantLineInnerInterceptor.java
|
|
|
+ * @Description 默认多租户插件
|
|
|
+ * @createTime 2022年08月02日 08:50:00
|
|
|
*/
|
|
|
-@EnableTransactionManagement(proxyTargetClass = true)
|
|
|
+@ConditionalOnMissingBean({TenantLineInnerInterceptor.class,TenantIdManager.class})
|
|
|
@Configuration
|
|
|
-@MapperScan({"${mybatis-plus.mapperPackage}","com.nb.framework.test.mapper"})
|
|
|
-public class MybatisPlusConfig {
|
|
|
-
|
|
|
- private final TenantIdManager tenantIdManager;
|
|
|
-
|
|
|
+public class DefaultTenantLineInnerInterceptor {
|
|
|
+ private final List<String> ignoreUrlTenantId= Arrays.asList("/login","/getUserInfo","/token");
|
|
|
private final List<Class<?>> tableClass;
|
|
|
|
|
|
- private final List<String> ignoreUrlTenantId=Arrays.asList("/login","/getUserInfo","/token");
|
|
|
-
|
|
|
private Set<String> ignoreTableName;
|
|
|
- @Autowired
|
|
|
- public MybatisPlusConfig() {
|
|
|
- this.tenantIdManager=new TenantIdManager();
|
|
|
+
|
|
|
+ public DefaultTenantLineInnerInterceptor(){
|
|
|
Set<Class<?>> classes = ClassUtil.scanPackage("com.nb");
|
|
|
//找到所有@TableName修饰的类
|
|
|
tableClass = classes.stream()
|
|
|
@@ -75,51 +66,11 @@ public class MybatisPlusConfig {
|
|
|
.map(aClass -> StrUtil.toUnderlineCase(aClass.getSimpleName()))
|
|
|
.collect(Collectors.toSet()));
|
|
|
}
|
|
|
-
|
|
|
- @Bean
|
|
|
- public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
|
|
- MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
|
|
- //多租户插件
|
|
|
- interceptor.addInnerInterceptor(tenantLineInnerInterceptor());
|
|
|
-
|
|
|
- // 分页插件
|
|
|
- interceptor.addInnerInterceptor(paginationInnerInterceptor());
|
|
|
-
|
|
|
- //禁止全表更新
|
|
|
- interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
|
|
|
-
|
|
|
- // 乐观锁插件
|
|
|
- interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
|
|
|
-
|
|
|
- return interceptor;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 分页插件
|
|
|
- */
|
|
|
- public PaginationInnerInterceptor paginationInnerInterceptor() {
|
|
|
- PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
|
|
|
-
|
|
|
- // 设置数据库类型为mysql
|
|
|
- paginationInnerInterceptor.setDbType(DbType.MYSQL);
|
|
|
- // 设置最大单页限制数量,默认 500 条,-1 不受限制
|
|
|
- paginationInnerInterceptor.setMaxLimit(-1L);
|
|
|
- paginationInnerInterceptor.setDialect(new MySqlDialect());
|
|
|
-
|
|
|
- return paginationInnerInterceptor;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 乐观锁插件
|
|
|
- */
|
|
|
- public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
|
|
|
- return new OptimisticLockerInnerInterceptor();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 多租户插件
|
|
|
*/
|
|
|
- public TenantLineInnerInterceptor tenantLineInnerInterceptor() {
|
|
|
+ @Bean
|
|
|
+ public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantIdManager tenantIdManager) {
|
|
|
TenantLineInnerInterceptor tenantInterceptor = new TenantLineInnerInterceptor();
|
|
|
tenantInterceptor.setTenantLineHandler(new TenantLineHandler() {
|
|
|
@Override
|
|
|
@@ -163,13 +114,4 @@ public class MybatisPlusConfig {
|
|
|
});
|
|
|
return tenantInterceptor;
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 元对象字段填充控制器
|
|
|
- */
|
|
|
- @Bean
|
|
|
- public MetaObjectHandler metaObjectHandler() {
|
|
|
- return new CreateAndUpdateMetaObjectHandler();
|
|
|
- }
|
|
|
-
|
|
|
}
|