springboot-配置多数据源之番外篇(分包实现)
场景:
随着业务发展,系统连接多数据库成为常态,继前面AOP的实现方式之后,这里记录一下分包实现的方式。
实现:
1.pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mumu</groupId>
<artifactId>springboot-mumu</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>mumu-web</artifactId>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- sqlserver -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.配置文件
server: port: 9587 spring: datasource: hikari: master: driverClassName: com.mysql.cj.jdbc.Driver jdbcUrl: jdbc:mysql://127.0.0.1:3306/boltloan?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8 username: root password: 123456 slave: driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver jdbcUrl: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=TestDB username: SA password: <1234567890@Passw0rd> #mybatis-plus mybatis-plus: type-aliases-package: com.mumu.model mapper-locations: classpath:/mapper/**/*.xml configuration: jdbc-type-for-null: null map-underscore-to-camel-case: true cache-enabled: false global-config: db-config: id-type: auto field-strategy: not_empty
3.数据源配置类
采用注解的方式:每种数据源,使用一个对应 注解!
敬请期待...