Skip to content

Commit

Permalink
dynamic-datasource-multi模块
Browse files Browse the repository at this point in the history
  • Loading branch information
guolanren committed Apr 18, 2020
1 parent 1044e2b commit 5d5a391
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 8 deletions.
4 changes: 2 additions & 2 deletions dynamic-datasource/dynamic-datasource-master-slave/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<java.version>1.8</java.version>
<mysql.version>5.1.47</mysql.version>
<mybatis.starter.version>2.0.1</mybatis.starter.version>
<mybatis-plus.dynamic-datasouce.starter.version>3.0.0</mybatis-plus.dynamic-datasouce.starter.version>
<dynamic-datasouce.starter.version>3.0.0</dynamic-datasouce.starter.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -50,7 +50,7 @@
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>${mybatis-plus.dynamic-datasouce.starter.version}</version>
<version>${dynamic-datasouce.starter.version}</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ spring:
primary: master
datasource:
master:
username: guolanren
password: hahaha
username: ***
password: ***
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/master?useUnicode=true&characterEncoding=utf-8&useSSL=true
# slave组下有两个子库slave_1,slave_2
slave_1:
username: guolanren
password: hahaha
username: ***
password: ***
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/slave1?useUnicode=true&characterEncoding=utf-8&useSSL=true
slave_2:
username: guolanren
password: hahaha
username: ***
password: ***
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/slave2?useUnicode=true&characterEncoding=utf-8&useSSL=true

Expand Down
65 changes: 65 additions & 0 deletions dynamic-datasource/dynamic-datasource-multi/pom.xml
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>
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);
}

}
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
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>
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());
}
}

0 comments on commit 5d5a391

Please sign in to comment.