Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

租户基础代码 #142

Merged
merged 7 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
package com.hccake.ballcat.admin;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.hccake.ballcat.admin.config.prop.DbProperties;
import com.hccake.ballcat.auth.annotation.EnableOauth2AuthorizationServer;
import com.hccake.ballcat.common.security.annotation.EnableOauth2ResourceServer;
import com.hccake.ballcat.common.security.properties.SecurityProperties;
import com.hccake.ballcat.system.authentication.CustomTokenEnhancer;
import com.hccake.ballcat.system.authentication.SysUserDetailsServiceImpl;
import com.hccake.ballcat.system.authentication.DefaultUserInfoCoordinatorImpl;
import com.hccake.ballcat.system.authentication.SysUserDetailsServiceImpl;
import com.hccake.ballcat.system.authentication.UserInfoCoordinator;
import com.hccake.ballcat.system.properties.UpmsProperties;
import com.hccake.ballcat.system.service.SysUserService;
import com.hccake.extend.mybatis.plus.methods.InsertBatchSomeColumnByCollection;
import lombok.AllArgsConstructor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
Expand All @@ -30,7 +40,7 @@
@ComponentScan({ "com.hccake.ballcat.admin", "com.hccake.ballcat.auth", "com.hccake.ballcat.system",
"com.hccake.ballcat.log", "com.hccake.ballcat.file", "com.hccake.ballcat.notify" })
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({ UpmsProperties.class, SecurityProperties.class })
@EnableConfigurationProperties({ UpmsProperties.class, SecurityProperties.class, DbProperties.class })
@EnableOauth2AuthorizationServer
@EnableOauth2ResourceServer
public class UpmsAutoConfiguration {
Expand Down Expand Up @@ -78,4 +88,40 @@ public UserInfoCoordinator userInfoCoordinator() {

}

@Configuration
@AllArgsConstructor
static class MybatisPlusBeanConfiguration {

private final DbProperties dbProperties;

/**
* 分页插件
* @return InnerInterceptor
*/
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE + 100)
@ConditionalOnMissingBean(PaginationInnerInterceptor.class)
public InnerInterceptor paginationInnerInterceptor() {
PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
// 单页分页条数限制
paginationInterceptor.setMaxLimit(dbProperties.getMaxLimit());
// 数据库类型
paginationInterceptor.setDbType(DbType.MYSQL);
// 生成 countSql 优化掉 join 现在只支持 left join
paginationInterceptor.setOptimizeJoin(dbProperties.isOptimizeJoin());
return paginationInterceptor;
}

/**
* 自定义批量插入方法注入
* @return AbstractMethod
*/
@Bean
public AbstractMethod insertBatchSomeColumnByCollection() {
// 对于只在更新时进行填充的字段不做插入处理
return new InsertBatchSomeColumnByCollection(t -> t.getFieldFill() != FieldFill.UPDATE);
}

}

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package com.hccake.ballcat.admin.config.mybatis;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.hccake.extend.mybatis.plus.injector.CustomSqlInjector;
import com.hccake.extend.mybatis.plus.methods.InsertBatchSomeColumnByCollection;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;

import java.util.ArrayList;
import java.util.List;

/**
* @author hccake
* @date 2020/04/19 默认配置MybatisPlus分页插件,通过conditional注解达到覆盖效用
*/
@Configuration
@AllArgsConstructor
public class MybatisPlusConfig {

private final List<InnerInterceptor> innerInterceptors;

private final List<AbstractMethod> injectMethods;

/**
* MybatisPlusInterceptor 插件,默认提供分页插件</br>
* 如需其他MP内置插件,则需自定义该Bean
Expand All @@ -32,7 +35,8 @@ public class MybatisPlusConfig {
@ConditionalOnMissingBean(MybatisPlusInterceptor.class)
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
AnnotationAwareOrderComparator.sort(innerInterceptors);
innerInterceptors.forEach(interceptor::addInnerInterceptor);
return interceptor;
}

Expand All @@ -53,10 +57,7 @@ public MetaObjectHandler fillMetaObjectHandle() {
@Bean
@ConditionalOnMissingBean(ISqlInjector.class)
public ISqlInjector customSqlInjector() {
List<AbstractMethod> list = new ArrayList<>();
// 对于只在更新时进行填充的字段不做插入处理
list.add(new InsertBatchSomeColumnByCollection(t -> t.getFieldFill() != FieldFill.UPDATE));
return new CustomSqlInjector(list);
return new CustomSqlInjector(injectMethods);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.hccake.ballcat.admin.config.prop;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* 数据库配置属性
*
* @author huyuanzhi
*/
@Data
@ConfigurationProperties("ballcat.db")
public class DbProperties {

private Long maxLimit = 500L;

private boolean optimizeJoin = false;

}
28 changes: 28 additions & 0 deletions ballcat-admin/ballcat-admin-tenant/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ballcat-admin</artifactId>
<groupId>com.hccake</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>ballcat-admin-tenant</artifactId>

<properties>
</properties>

<dependencies>
<dependency>
<groupId>com.hccake</groupId>
<artifactId>ballcat-tenant-controller</artifactId>
</dependency>
<dependency>
<groupId>com.hccake</groupId>
<artifactId>ballcat-spring-boot-starter-tenant</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.hccake.ballcat.admin.tenant;

import com.hccake.ballcat.common.tenant.datasource.DataSourceDetailService;
import com.hccake.ballcat.common.tenant.properties.TenantProperties;
import com.hccake.ballcat.tenant.datasource.SysDataSourceDetailServiceImpl;
import com.hccake.ballcat.tenant.service.SysDatasourceService;
import com.hccake.ballcat.tenant.service.SysTenantDatasourceService;
import com.hccake.ballcat.tenant.service.SysTenantService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
* 租戶注冊
*
* @author huyuanzhi
*/
@ComponentScan("com.hccake.ballcat.tenant")
public class TenantAutoConfiguration {

@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = TenantProperties.PREFIX, name = "enable", havingValue = "true")
static class DataSourceDetailServiceConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = TenantProperties.PREFIX, name = "tenantType", havingValue = "DATASOURCE")
public DataSourceDetailService dataSourceDetailService(SysTenantService sysTenantService,
SysDatasourceService sysDatasourceService, SysTenantDatasourceService sysTenantDatasourceService) {
return new SysDataSourceDetailServiceImpl(sysTenantService, sysDatasourceService,
sysTenantDatasourceService);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.hccake.ballcat.admin.tenant.TenantAutoConfiguration


3 changes: 2 additions & 1 deletion ballcat-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<module>ballcat-admin-core</module>
<module>ballcat-admin-websocket</module>
<module>ballcat-admin-i18n</module>
</modules>
<module>ballcat-admin-tenant</module>
</modules>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
* @see com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
* @author Hccake
*/
public class JavaTimeModule extends SimpleModule {
public class CustomJavaTimeModule extends SimpleModule {

public JavaTimeModule() {
public CustomJavaTimeModule() {
super(PackageVersion.VERSION);

this.addSerializer(LocalDateTime.class,
Expand Down
46 changes: 46 additions & 0 deletions ballcat-common/ballcat-common-tenant/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ballcat-common</artifactId>
<groupId>com.hccake</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>ballcat-common-tenant</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hccake.ballcat.common.tenant.annotation;

import com.baomidou.dynamic.datasource.annotation.DS;

import java.lang.annotation.*;

/**
* 切换租户数据源 注意 租户数据源名称一定要是 master
* @author huyuanzhi
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@DS("master")
public @interface Tenant {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.hccake.ballcat.common.tenant.core;

/**
* 数据源连接池类型
* @author huyuanzhi
*/
public enum DatasourceType {

/**
* hikari连接池
*/
HIKARI,
/**
* druid连接池
*/
DRUID,

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.hccake.ballcat.common.tenant.core;


/**
* 保存租户信息
*
* @author huyuanzhi
*/
public final class TenantContext {

private static final ThreadLocal<String> THREAD_LOCAL = new InheritableThreadLocal<>();

private TenantContext() {
}

public static void set(String value) {
THREAD_LOCAL.set(value);
}

public static void clear() {
THREAD_LOCAL.remove();
}

public static String get() {
return THREAD_LOCAL.get();
}

}
Loading