[Mybatis]MybatisPlus的使用参考:https://www.jianshu.com/p/ceb1df475021
多源数据配置dynamic-datasourc
yml:
spring:
devtools:
restart:
enabled: true #设置开启热部署
freemarker:
cache: false #页面不加载缓存,修改即时生效
datasource:
dynamic:
primary: db1 #设置默认的数据源或者数据源组,默认值即为db1
strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候回抛出异常,不启动会使用默认数据源.
datasource:
db1:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxx:3306/fgw_test?characterEncoding=UTF-8
username: xxx
password: xxx
initialSize: 5
minIdle: 5
maxActive: 20
db2:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxxx/fgw_test?characterEncoding=UTF-8
username: xxxx
password: xxxx
initialSize: 5
minIdle: 5
maxActive: 20
Dao层实现分别访问:
package com.springboot.dao;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.springboot.entity.FileRecordEntity;
import org.springframework.stereotype.Repository;
/**
* 查看文件记录DAO层
* @author : 张智顺
* @date : 2020-05-14 11:23
**/
@Repository
@DS("db1")
public interface FileRecordDao extends BaseMapper<FileRecordEntity> {
/**
* 根据ID获取文件记录
* @param id id
* @return FileRecordEntity
* */
@DS("db2")
FileRecordEntity getById(Integer id);
}
pom:
<!-- https://mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.5.4</version>
</dependency>