diff --git a/dynamic-datasource/dynamic-datasource-master-slave/pom.xml b/dynamic-datasource/dynamic-datasource-master-slave/pom.xml index a2c8f68..51d8d19 100644 --- a/dynamic-datasource/dynamic-datasource-master-slave/pom.xml +++ b/dynamic-datasource/dynamic-datasource-master-slave/pom.xml @@ -19,7 +19,7 @@ 1.8 5.1.47 2.0.1 - 3.0.0 + 3.0.0 @@ -50,7 +50,7 @@ com.baomidou dynamic-datasource-spring-boot-starter - ${mybatis-plus.dynamic-datasouce.starter.version} + ${dynamic-datasouce.starter.version} diff --git a/dynamic-datasource/dynamic-datasource-master-slave/src/main/resources/config/application.yml b/dynamic-datasource/dynamic-datasource-master-slave/src/main/resources/config/application.yml index a42a014..d0e6559 100644 --- a/dynamic-datasource/dynamic-datasource-master-slave/src/main/resources/config/application.yml +++ b/dynamic-datasource/dynamic-datasource-master-slave/src/main/resources/config/application.yml @@ -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 diff --git a/dynamic-datasource/dynamic-datasource-multi/pom.xml b/dynamic-datasource/dynamic-datasource-multi/pom.xml new file mode 100644 index 0000000..71af0a0 --- /dev/null +++ b/dynamic-datasource/dynamic-datasource-multi/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.1.6.RELEASE + + + + name.guolanren + dynamic-datasource-multi + 1.0.0 + + + 1.8 + 5.1.47 + 2.0.1 + 3.0.0 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + mysql + mysql-connector-java + ${mysql.version} + runtime + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + ${mybatis.starter.version} + + + + com.baomidou + dynamic-datasource-spring-boot-starter + ${dynamic-datasouce.starter.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/dynamic-datasource/dynamic-datasource-multi/src/main/java/name/guolanren/DynamicDatasourceMultiApplication.java b/dynamic-datasource/dynamic-datasource-multi/src/main/java/name/guolanren/DynamicDatasourceMultiApplication.java new file mode 100644 index 0000000..2ba126b --- /dev/null +++ b/dynamic-datasource/dynamic-datasource-multi/src/main/java/name/guolanren/DynamicDatasourceMultiApplication.java @@ -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); + } + +} diff --git a/dynamic-datasource/dynamic-datasource-multi/src/main/resources/config/application.yml b/dynamic-datasource/dynamic-datasource-multi/src/main/resources/config/application.yml new file mode 100644 index 0000000..b41e07e --- /dev/null +++ b/dynamic-datasource/dynamic-datasource-multi/src/main/resources/config/application.yml @@ -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 \ No newline at end of file diff --git a/dynamic-datasource/dynamic-datasource-multi/src/main/resources/mybatis/config/mybatis-config.xml b/dynamic-datasource/dynamic-datasource-multi/src/main/resources/mybatis/config/mybatis-config.xml new file mode 100644 index 0000000..7f8baac --- /dev/null +++ b/dynamic-datasource/dynamic-datasource-multi/src/main/resources/mybatis/config/mybatis-config.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/dynamic-datasource/dynamic-datasource-multi/src/test/java/name/guolanren/service/impl/UserServiceImplTest.java b/dynamic-datasource/dynamic-datasource-multi/src/test/java/name/guolanren/service/impl/UserServiceImplTest.java new file mode 100644 index 0000000..be932cd --- /dev/null +++ b/dynamic-datasource/dynamic-datasource-multi/src/test/java/name/guolanren/service/impl/UserServiceImplTest.java @@ -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()); + } +}