1. pom.xml引入多数据源支持
<properties>
<dynamic-datasource.version>2.2.0</dynamic-datasource.version>
<druid.version>1.0.29</druid.version>
<druid-spring.version>1.1.10</druid-spring.version>
</properties>
<!-- Druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-spring.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--多数据源支持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>${dynamic-datasource.version}</version>
</dependency>
2. application.properties中增加mysql配置和默认数据源配置
########mysql#######
spring.datasource.dynamic.druid.initial-size=5
spring.datasource.dynamic.druid.min-idle=5
spring.datasource.dynamic.druid.max-active=20
spring.datasource.dynamic.druid.max-wait=60000
spring.datasource.dynamic.druid.time-between-eviction-runs-millis=60000
spring.datasource.dynamic.druid.min-evictable-idle-time-millis=300000
spring.datasource.dynamic.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.dynamic.druid.test-while-idle=true
spring.datasource.dynamic.druid.test-on-borrow=true
spring.datasource.dynamic.druid.test-on-return=true
spring.datasource.dynamic.druid.pool-prepared-statements=true
spring.datasource.dynamic.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.dynamic.druid.filter=slf4j
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#默认数据源
spring.datasource.dynamic.primary=lop_dev
spring.datasource.dynamic.datasource.lop_dev.url=jdbc:mysql://10.2.4.56:3306/lop_dev?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
spring.datasource.dynamic.datasource.lop_dev.username=lop_rw
spring.datasource.dynamic.datasource.lop_dev.password=X1xDNftSScsXSWYB
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.mryx.autotest.lop.dao.entities
3. Application文件中增加注解
@MapperScan("com.mryx.autotest.lop.dao.mapper")
public class LopApplication {
…………
}
4. Mapper文件指定数据库
@DS("lop_dev")
public interface AdjustRecordMapper {
……
}