saas-datasource-spring-boot-starter

spring boot starter for Software-as-a-Service datasource base on mybatis-plus(https://github.com/baomidou/mybatis-plus)

License

License

Categories

Categories

Spring Boot Container Microservices Data
GroupId

GroupId

com.github.kuhn-he
ArtifactId

ArtifactId

saas-datasource-spring-boot-starter
Last Version

Last Version

3.0.6.3
Release Date

Release Date

Type

Type

jar
Description

Description

saas-datasource-spring-boot-starter
spring boot starter for Software-as-a-Service datasource base on mybatis-plus(https://github.com/baomidou/mybatis-plus)
Project URL

Project URL

https://github.com/kuhn-he/saas-datasource-spring-boot-starter
Source Code Management

Source Code Management

https://github.com/kuhn-he/saas-datasource-spring-boot-starter

Download saas-datasource-spring-boot-starter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.kuhn-he/saas-datasource-spring-boot-starter/ -->
<dependency>
    <groupId>com.github.kuhn-he</groupId>
    <artifactId>saas-datasource-spring-boot-starter</artifactId>
    <version>3.0.6.3</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.kuhn-he/saas-datasource-spring-boot-starter/
implementation 'com.github.kuhn-he:saas-datasource-spring-boot-starter:3.0.6.3'
// https://jarcasting.com/artifacts/com.github.kuhn-he/saas-datasource-spring-boot-starter/
implementation ("com.github.kuhn-he:saas-datasource-spring-boot-starter:3.0.6.3")
'com.github.kuhn-he:saas-datasource-spring-boot-starter:jar:3.0.6.3'
<dependency org="com.github.kuhn-he" name="saas-datasource-spring-boot-starter" rev="3.0.6.3">
  <artifact name="saas-datasource-spring-boot-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.kuhn-he', module='saas-datasource-spring-boot-starter', version='3.0.6.3')
)
libraryDependencies += "com.github.kuhn-he" % "saas-datasource-spring-boot-starter" % "3.0.6.3"
[com.github.kuhn-he/saas-datasource-spring-boot-starter "3.0.6.3"]

Dependencies

compile (5)

Group / Artifact Type Version
com.baomidou : mybatis-plus-boot-starter jar 3.0.6
com.baomidou : dynamic-datasource-spring-boot-starter jar 2.4.2
com.alibaba : druid-spring-boot-starter jar 1.1.10
org.projectlombok : lombok jar 1.18.0
org.apache.commons : commons-lang3 jar 3.5

provided (4)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-web jar 2.0.3.RELEASE
com.fasterxml.jackson.core : jackson-annotations jar 2.9.5
com.fasterxml.jackson.core : jackson-core jar 2.9.5
com.fasterxml.jackson.core : jackson-databind jar 2.9.5

runtime (1)

Group / Artifact Type Version
com.h2database : h2 jar 1.4.197

Project Modules

There are no modules declared in this project.

saas-datasource-spring-boot-starter

spring boot starter for Software-as-a-Service datasource base on mybatis-plus(https://github.com/baomidou/mybatis-plus)

How to publish saas spring boot starter

  • add Dependencies:
<dependency>
    <groupId>com.github.kuhn-he</groupId>
    <artifactId>saas-datasource-spring-boot-starter</artifactId>
    <version>3.0.6.3</version>
</dependency>
  • add saas datasource configuration in application.properties, demo:
spring.autoconfigure.exclude=com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
spring.datasource.dynamic.primary=mysql
spring.datasource.dynamic.datasource.mysql.url = jdbc:mysql://127.0.0.1:3306/saas?characterEncoding=utf-8
spring.datasource.dynamic.datasource.mysql.username = root
spring.datasource.dynamic.datasource.mysql.password = root
spring.datasource.dynamic.datasource.mysql.driver-class-name = com.mysql.jdbc.Driver

How to switch business datasources

  • custom dynamic datasource provider for switch business datasources, demo:
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.github.kuhn_he.saas.ds.DynamicDataSourcePlusCreator;
import com.github.kuhn_he.saas.ds.DynamicDataSourcePlusProvider;

@Component
public class SampleDynamicDataSourceProvider implements DynamicDataSourcePlusProvider {

	@Autowired
	private DynamicDataSourcePlusCreator dynamicDataSourcePlusCreator;
	
	@Override
	public DataSource createDataSource(String dsKey) {
		//...
		DataSourceProperty dataSourceProperty=new DataSourceProperty();
		//...
		return dynamicDataSourcePlusCreator.createDruidDataSource(dataSourceProperty);
	}
}
  • then add @EnableDynamicRouteDataSource on Spring Boot Application, indicates that saas is enabled.
import com.github.kuhn_he.saas.ds.annotation.EnableDynamicRouteDataSource;

@SpringBootApplication
@EnableDynamicRouteDataSource
public class ApplicationBootstrap {
 //...
}
  • code your database Mapper, add @SAAS(import com.github.kuhn_he.mapper.SAAS) on your Mapper interface(non-annotation @SAAS is default datasource which is saas datasouce configuration in application.properties).
import com.github.kuhn_he.saas.ds.annotation.SAAS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

@SAAS //default is x-datasource-key which datasource key put in header or session. 
@Mapper
public interface SampleMapper extends BaseMapper<TSample>{
 //...
}

Reference

sample see: saas-springboot-sample

Versions

Version
3.0.6.3
3.0.6.2
mybatis-plus-3.0.6.1