Skip to content

Commit

Permalink
Add support for `com.baomidou:dynamic-datasource-spring-boot-starter:…
Browse files Browse the repository at this point in the history
…3.6.1`
  • Loading branch information
linghengqian committed Dec 21, 2022
1 parent c102e6c commit 246f363
Show file tree
Hide file tree
Showing 27 changed files with 702 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
# Project versions
nativeBuildTools = "0.9.16"
nativeBuildTools = "0.9.18"

# External dependencies
junitPlatform = "1.8.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
"jni-config.json",
"proxy-config.json",
"reflect-config.json",
"resource-config.json",
"serialization-config.json"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"lambdaCapturingTypes": [],
"types": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"latest": true,
"metadata-version": "3.6.1",
"module": "com.baomidou:dynamic-datasource-spring-boot-starter",
"tested-versions": [
"3.6.1"
]
}
]
4 changes: 4 additions & 0 deletions metadata/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,9 @@
{
"directory": "org.eclipse.jetty/jetty-client",
"module": "org.eclipse.jetty:jetty-client"
},
{
"directory": "com.baomidou/dynamic-datasource-spring-boot-starter",
"module": "com.baomidou:dynamic-datasource-spring-boot-starter"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gradlew.bat
gradlew
gradle/
build/
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright and related rights waived via CC0
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/

plugins {
id "org.graalvm.internal.tck"
id 'org.springframework.boot' version '3.0.0'
id 'io.spring.dependency-management' version '1.1.0'
}

String libraryVersion = tck.testedLibraryVersion.get()

dependencies {
testImplementation "com.baomidou:dynamic-datasource-spring-boot-starter:$libraryVersion"
testImplementation 'org.assertj:assertj-core:3.22.0'
testImplementation 'org.springframework.boot:spring-boot-starter-web'
testRuntimeOnly 'com.h2database:h2:2.1.214'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

graalvmNative {
agent {
defaultMode = "conditional"
modes {
conditional {
userCodeFilterPath = "user-code-filter.json"
}
}
metadataCopy {
mergeWithExisting = true
inputTaskNames.add("test")
outputDirectories.add("src/test/resources/META-INF/native-image/com.baomidou/dynamic-datasource-spring-boot-starter")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
library.version = 3.6.1
metadata.dir = com.baomidou/dynamic-datasource-spring-boot-starter/3.6.1/
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pluginManagement {
def tckPath = Objects.requireNonNullElse(
System.getenv("GVM_TCK_TCKDIR"),
"../../../../tck-build-logic"
)
includeBuild(tckPath)
}

plugins {
id "org.graalvm.internal.tck-settings" version "1.0.0-SNAPSHOT"
}

rootProject.name = 'com.baomidou.dynamic-datasource-spring-boot-starter_tests'
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright and related rights waived via CC0
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
package com_baomidou.dynamic_datasource_spring_boot_starter;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.hikari.HikariCpConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@SpringBootTest(classes = AddRemoveDatasourceApplication.class, webEnvironment = RANDOM_PORT)
public class AddRemoveDatasourceTest {
@Autowired
DataSource dataSource;
@Autowired
DefaultDataSourceCreator dataSourceCreator;

@Test
void testAddAndRemoveDataSource() {
HikariCpConfig hikariCpConfig = new HikariCpConfig();
hikariCpConfig.setConnectionTestQuery("select 1");
DataSourceProperty dataSourceProperty = new DataSourceProperty()
.setPoolName("slave_1").setDriverClassName("org.h2.Driver").setUrl("jdbc:h2:mem:test1;MODE=MySQL")
.setUsername("sa").setPassword("").setHikari(hikariCpConfig);
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
ds.addDataSource(dataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(dataSourceProperty));
assertThat(ds.getDataSources().keySet()).contains("slave_1");
ds.removeDataSource("slave_1");
assertThat(ds.getDataSources().keySet()).doesNotContain("slave_1");
}
}

@SpringBootApplication
class AddRemoveDatasourceApplication {
public static void main(String[] args) {
SpringApplication.run(AddRemoveDatasourceApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright and related rights waived via CC0
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
package com_baomidou.dynamic_datasource_spring_boot_starter;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
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.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@SpringBootTest(classes = LoadDatasourceFromJDBCApplication.class, webEnvironment = RANDOM_PORT)
public class LoadDatasourceFromJDBCTest {
@Autowired
DataSource dataSource;

@Test
void testExistDataSource() {
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
assertThat(ds.getDataSources().keySet()).contains("master", "db1", "db2", "db3");
}
}

@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
@SpringBootApplication
class LoadDatasourceFromJDBCApplication {

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

@Bean
public DynamicDataSourceProvider dynamicDataSourceProvider() {
return new AbstractJdbcDataSourceProvider("org.h2.Driver", "jdbc:h2:mem:test;MODE=MySQL", "sa", "") {
@Override
protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
statement.execute("""
CREATE TABLE IF NOT EXISTS `DB`
(
`name` VARCHAR(30) NULL DEFAULT NULL,
`username` VARCHAR(30) NULL DEFAULT NULL,
`password` VARCHAR(30) NULL DEFAULT NULL,
`url` VARCHAR(30) NULL DEFAULT NULL,
`driver` VARCHAR(30) NULL DEFAULT NULL
)""");
statement.executeUpdate("insert into DB values ('master','sa','','jdbc:h2:mem:test;MODE=MySQL','org.h2.Driver')");
statement.executeUpdate("insert into DB values ('db1','sa','','jdbc:h2:mem:test2','org.h2.Driver')");
statement.executeUpdate("insert into DB values ('db2','sa','','jdbc:h2:mem:test3','org.h2.Driver')");
statement.executeUpdate("insert into DB values ('db3','sa','','jdbc:h2:mem:test4','org.h2.Driver')");
Map<String, DataSourceProperty> map = new HashMap<>();
ResultSet rs = statement.executeQuery("select * from DB");
while (rs.next()) {
map.put(rs.getString("name"), new DataSourceProperty()
.setUsername(rs.getString("username")).setPassword(rs.getString("password"))
.setUrl(rs.getString("url")).setDriverClassName(rs.getString("driver")));
}
return map;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright and related rights waived via CC0
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
package com_baomidou.dynamic_datasource_spring_boot_starter;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com_baomidou.dynamic_datasource_spring_boot_starter.service.nest.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@SpringBootTest(classes = NestApplication.class, webEnvironment = RANDOM_PORT)
public class NestDataSourceTest {
@Autowired
DataSource dataSource;
@Autowired
DefaultDataSourceCreator dataSourceCreator;
@Autowired
private TeacherService teacherService;
@Autowired
private StudentService studentService;
@Autowired
private SchoolService schoolService;

@Test
void testNest() {
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/add-remove-datasource.sql'")
.setUsername("sa").setPassword("");
DataSourceProperty teacherDataSourceProperty = new DataSourceProperty()
.setPoolName("teacher").setDriverClassName("org.h2.Driver").setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE")
.setUsername("sa").setPassword("");
DataSourceProperty studentDataSourceProperty = new DataSourceProperty()
.setPoolName("student").setDriverClassName("org.h2.Driver").setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE")
.setUsername("sa").setPassword("");
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
ds.addDataSource(masterDataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(masterDataSourceProperty));
ds.addDataSource(teacherDataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(teacherDataSourceProperty));
ds.addDataSource(studentDataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(studentDataSourceProperty));
assertThat(ds.getDataSources().keySet()).contains("master", "teacher", "student");
assertThat(teacherService.addTeacherWithTx("ss", 1)).isEqualTo(1);
assertThat(studentService.addStudentWithTx("tt", 2)).isEqualTo(1);
assertThat(teacherService.selectTeachers()).isEqualTo(List.of(new Teacher(1, "tt", 2)));
assertThat(studentService.selectStudents()).isEqualTo(List.of(new Student(1, "tt", 2)));
assertThat(schoolService.addTeacherAndStudentWithTx()).isEqualTo(2);
assertThat(teacherService.selectTeachers()).isEqualTo(List.of(new Teacher(1, "tt", 2), new Teacher(2, "bb", 4)));
assertThat(studentService.selectStudents()).isEqualTo(List.of(new Student(1, "tt", 2), new Student(2, "bb", 4)));
}
}

@SpringBootApplication
class NestApplication {
public static void main(String[] args) {
SpringApplication.run(NestApplication.class, args);
}
}
Loading

0 comments on commit 246f363

Please sign in to comment.