-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
195 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.6.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<groupId>name.guolanren</groupId> | ||
<artifactId>dynamic-datasource-multi</artifactId> | ||
<version>1.0.0</version> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
<mysql.version>5.1.47</mysql.version> | ||
<mybatis.starter.version>2.0.1</mybatis.starter.version> | ||
<dynamic-datasouce.starter.version>3.0.0</dynamic-datasouce.starter.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>${mysql.version}</version> | ||
<scope>runtime</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>${mybatis.starter.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>dynamic-datasource-spring-boot-starter</artifactId> | ||
<version>${dynamic-datasouce.starter.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
16 changes: 16 additions & 0 deletions
16
...amic-datasource-multi/src/main/java/name/guolanren/DynamicDatasourceMultiApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package name.guolanren; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* @author guolanren | ||
*/ | ||
@SpringBootApplication | ||
public class DynamicDatasourceMultiApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(DynamicDatasourceMultiApplication.class, args); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
dynamic-datasource/dynamic-datasource-multi/src/main/resources/config/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# dynamic-datasource | ||
spring: | ||
datasource: | ||
dynamic: | ||
#设置默认的数据源或者数据源组,默认值即为master | ||
primary: dsa | ||
datasource: | ||
dsa_1: | ||
username: *** | ||
password: *** | ||
driver-class-name: com.mysql.jdbc.Driver | ||
url: jdbc:mysql://localhost:3306/dsa_1?useUnicode=true&characterEncoding=utf-8&useSSL=true | ||
dsa_2: | ||
username: *** | ||
password: *** | ||
driver-class-name: com.mysql.jdbc.Driver | ||
url: jdbc:mysql://localhost:3306/dsa_2?useUnicode=true&characterEncoding=utf-8&useSSL=true | ||
dsb: | ||
username: *** | ||
password: *** | ||
driver-class-name: com.mysql.jdbc.Driver | ||
url: jdbc:mysql://localhost:3306/dsb?useUnicode=true&characterEncoding=utf-8&useSSL=true | ||
|
||
# mybatis | ||
mybatis: | ||
config-location: classpath:mybatis/config/mybatis-config.xml | ||
mapper-locations: classpath:mybatis/mapper/*.xml |
13 changes: 13 additions & 0 deletions
13
...-datasource/dynamic-datasource-multi/src/main/resources/mybatis/config/mybatis-config.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!DOCTYPE configuration | ||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-config.dtd"> | ||
|
||
<configuration> | ||
<settings> | ||
<setting name="mapUnderscoreToCamelCase" value="true"/> | ||
<setting name="lazyLoadingEnabled" value="true"/> | ||
<setting name="aggressiveLazyLoading" value="false"/> | ||
<setting name="logImpl" value="STDOUT_LOGGING"/> | ||
</settings> | ||
</configuration> |
66 changes: 66 additions & 0 deletions
66
...namic-datasource-multi/src/test/java/name/guolanren/service/impl/UserServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package name.guolanren.service.impl; | ||
|
||
import name.guolanren.model.User; | ||
import name.guolanren.service.UserService; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.annotation.Rollback; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* @author guolanren | ||
*/ | ||
@SpringBootTest | ||
@RunWith(SpringRunner.class) | ||
public class UserServiceImplTest { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@Test | ||
public void testInsert() { | ||
User user = new User(); | ||
user.setName("guolanren"); | ||
user.setAge(0); | ||
|
||
Long rows = userService.insert(user); | ||
assertEquals(1L, rows.longValue()); | ||
|
||
System.out.println(user); | ||
} | ||
|
||
@Test | ||
public void testUpdate() { | ||
User user = new User(); | ||
user.setId(1L); | ||
user.setName("g0"); | ||
user.setAge(0); | ||
|
||
Long rows = userService.update(user); | ||
assertEquals(1L, rows.longValue()); | ||
|
||
System.out.println(user); | ||
} | ||
|
||
@Test | ||
public void testDelete() { | ||
Long rows = userService.delete(1L); | ||
assertEquals(1L, rows.longValue()); | ||
} | ||
|
||
@Test | ||
public void testGet() { | ||
System.out.println(userService.get(1L)); | ||
} | ||
|
||
@Test | ||
public void testGetAll() { | ||
System.out.printf("第一次获取:%s", userService.getAll()); | ||
System.out.printf("第二次获取:%s", userService.getAll()); | ||
} | ||
} |