Dynamic-Datasource是一款动态数据源的轻量级实现,封装成Spring Starter方式引入,支持Mybatis、MybatisPlus读写分离,支持通过注解动态切换切换数据源。
@DS:指定使用数据库跟配置文件中配置的对应,可以注解在方法上和类上,同时存在方法注解优先于类上注解,强烈建议注解在service实现或mapper接口方法上。
依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>版本号</version>
</dependency>
配置文件,两个数据源,mysql和pgsql,三个库text、cca、gene_text
server:
port: 8081
address: 127.0.0.1
spring:
application:
name: template
datasource:
dynamic:
# 默认数据库
primary: master
# 严格匹配数据源,默认false. true未匹配到指定数据源时抛异常
strict: true
datasource:
text:
url: jdbc:mysql://127.0.0.1:3306/text?characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
cca:
url: jdbc:mysql://127.0.0.1:3306/cca?characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
geneText:
url: jdbc:postgresql://127.0.0.1:5432/gene_text
username: postgres
password: postgres
driver-class-name: org.postgresql.Driver
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:**/mapper/*.xml
text库中user表
@Data
@TableName("user")
public class User {
@TableId
private Integer uId;
private String uName;
private Integer uAge;
private LocalDateTime uBirthDay;
private Integer dId;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
Mapper
@DS("text")
@Mapper
public interface UserMapper extends BaseMapper<User> {}
cca库中的config表
@Data
@TableName("config")
public class Config {
@TableId
private Integer configId;
private String configName;
private String configKey;
private String configValue;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
Mapper
@DS("cca")
@Mapper
public interface ConfigMapper extends BaseMapper<Config> {}
gene_text库中的call表
@Data
@TableName("call")
public class Call {
@TableId
private String partyId;
private String dn;
private String keyName;
private String value;
private LocalDateTime createTime;
}
Mapper
@DS("geneText")
@Mapper
public interface CallMapper extends BaseMapper<Call> {}
使用,查询text库中的user表
@RestController
@RequestMapping("/template")
public class TemplateController extends BaseController{
@Autowired
private UserServiceImpl userService;
@PostMapping("/queryTemplate")
public ResponseResult queryTemplate(@RequestBody QueryReq queryReq) {
return success(userService.queryTemplate(queryReq));
}
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public Page<User> queryTemplate(QueryReq queryReq) {
// 分页设置
Page<User> userPage = Page.of(1, 10);
// 查询条件设置
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
if (StrUtil.isNotBlank(queryReq.getName())) {
queryWrapper.eq("u_name", queryReq.getName());
}
if (queryReq.getAge() != null) {
queryWrapper.eq("u_age", queryReq.getAge());
}
if (queryReq.getCreateTime() != null && queryReq.getEndTime() != null) {
queryWrapper.between("u_birth_day", queryReq.getCreateTime(), queryReq.getEndTime());
}
return userMapper.selectPage(userPage, queryWrapper);
}
}
测试
查询geneText库call表
@RestController
@RequestMapping("/template")
public class TemplateController extends BaseController{
@Autowired
private CallServiceImpl callService;
@GetMapping("/query")
public ResponseResult query() {
return success(callService.queryTemplate());
}
}
@Service
public class CallServiceImpl implements CallService {
@Autowired
private CallMapper callMapper;
@Override
public Page<Call> queryTemplate() {
// 分页设置
Page<Call> callPage = Page.of(1, 10);
// 查询条件设置
QueryWrapper<Call> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("dn", "6001");
return callMapper.selectPage(callPage, queryWrapper);
}
}
测试