Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
huayanYu committed Jul 16, 2023
1 parent 66c9a37 commit 6509a43
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.baomidou.samples.ds.controller;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.samples.ds.dto.DataSourceDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/
package com.baomidou.samples.load;

import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
Expand All @@ -31,13 +33,16 @@
@SpringBootApplication
public class LoadDsApplication {

@Autowired
private DefaultDataSourceCreator dataSourceCreator;

public static void main(String[] args) {
SpringApplication.run(LoadDsApplication.class, args);
}

@Bean
public DynamicDataSourceProvider dynamicDataSourceProvider() {
return new AbstractJdbcDataSourceProvider("org.h2.Driver", "jdbc:h2:mem:test;MODE=MySQL", "sa", "") {
return new AbstractJdbcDataSourceProvider(dataSourceCreator, "org.h2.Driver", "jdbc:h2:mem:test;MODE=MySQL", "sa", "") {
@Override
protected Map<String, DataSourceProperty> executeStmt(Statement statement)
throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.baomidou.samples.spel.test;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.samples.spel.SpelApplication;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -53,22 +53,36 @@ void setup(WebApplicationContext webApplicationContext) {

@Test
void testSPEL() {
DataSourceProperty masterDataSourceProperty = new DataSourceProperty()
.setPoolName("master").setDriverClassName("org.h2.Driver")
.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'")
.setUsername("sa").setPassword("");
DataSourceProperty tenant1_1DataSourceProperty = new DataSourceProperty()
.setPoolName("tenant1_1").setDriverClassName("org.h2.Driver").setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE")
.setUsername("sa").setPassword("");
DataSourceProperty tenant1_2DataSourceProperty = new DataSourceProperty()
.setPoolName("tenant1_2").setDriverClassName("org.h2.Driver").setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE")
.setUsername("sa").setPassword("");
DataSourceProperty tenant2_1DataSourceProperty = new DataSourceProperty()
.setPoolName("tenant2_1").setDriverClassName("org.h2.Driver").setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE")
.setUsername("sa").setPassword("");
DataSourceProperty tenant2_2DataSourceProperty = new DataSourceProperty()
.setPoolName("tenant2_2").setDriverClassName("org.h2.Driver").setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE")
.setUsername("sa").setPassword("");
DataSourceProperty masterDataSourceProperty = new DataSourceProperty();
masterDataSourceProperty.setPoolName("master");
masterDataSourceProperty.setDriverClassName("org.h2.Driver");
masterDataSourceProperty.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
masterDataSourceProperty.setUsername("sa");
masterDataSourceProperty.setPassword("");
DataSourceProperty tenant1_1DataSourceProperty = new DataSourceProperty();
masterDataSourceProperty.setPoolName("tenant1_1");
masterDataSourceProperty.setDriverClassName("org.h2.Driver");
masterDataSourceProperty.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
masterDataSourceProperty.setUsername("tenant1_1");
masterDataSourceProperty.setPassword("");
DataSourceProperty tenant1_2DataSourceProperty = new DataSourceProperty();
masterDataSourceProperty.setPoolName("tenant1_2");
masterDataSourceProperty.setDriverClassName("org.h2.Driver");
masterDataSourceProperty.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
masterDataSourceProperty.setUsername("tenant1_2");
masterDataSourceProperty.setPassword("");
DataSourceProperty tenant2_1DataSourceProperty = new DataSourceProperty();
masterDataSourceProperty.setPoolName("tenant2_1");
masterDataSourceProperty.setDriverClassName("org.h2.Driver");
masterDataSourceProperty.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
masterDataSourceProperty.setUsername("tenant2_1");
masterDataSourceProperty.setPassword("");
DataSourceProperty tenant2_2DataSourceProperty = new DataSourceProperty();
masterDataSourceProperty.setPoolName("tenant2_2");
masterDataSourceProperty.setDriverClassName("org.h2.Driver");
masterDataSourceProperty.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
masterDataSourceProperty.setUsername("tenant2_2");
masterDataSourceProperty.setPassword("");
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
ds.addDataSource(masterDataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(masterDataSourceProperty));
ds.addDataSource(tenant1_1DataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(tenant1_1DataSourceProperty));
Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@
</dependencies>
</dependencyManagement>

<repositories>
<repository>
<id>aliyunmaven</id>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
</repositories>

<build>
<plugins>
<plugin>
Expand Down

0 comments on commit 6509a43

Please sign in to comment.