当前位置:   article > 正文

Dynamic Datasource_dynamic-datasource

dynamic-datasource

Dynamic-Datasource是一款动态数据源的轻量级实现,封装成Spring Starter方式引入,支持Mybatis、MybatisPlus读写分离,支持通过注解动态切换切换数据源。

@DS:指定使用数据库跟配置文件中配置的对应,可以注解在方法上和类上,同时存在方法注解优先于类上注解,强烈建议注解在service实现或mapper接口方法上。

依赖

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  4. <version>版本号</version>
  5. </dependency>

配置文件,两个数据源,mysql和pgsql,三个库text、cca、gene_text

  1. server:
  2. port: 8081
  3. address: 127.0.0.1
  4. spring:
  5. application:
  6. name: template
  7. datasource:
  8. dynamic:
  9. # 默认数据库
  10. primary: master
  11. # 严格匹配数据源,默认false. true未匹配到指定数据源时抛异常
  12. strict: true
  13. datasource:
  14. text:
  15. url: jdbc:mysql://127.0.0.1:3306/text?characterEncoding=UTF-8&serverTimezone=GMT%2B8
  16. username: root
  17. password: 123456
  18. driver-class-name: com.mysql.cj.jdbc.Driver
  19. cca:
  20. url: jdbc:mysql://127.0.0.1:3306/cca?characterEncoding=UTF-8&serverTimezone=GMT%2B8
  21. username: root
  22. password: 123456
  23. driver-class-name: com.mysql.cj.jdbc.Driver
  24. geneText:
  25. url: jdbc:postgresql://127.0.0.1:5432/gene_text
  26. username: postgres
  27. password: postgres
  28. driver-class-name: org.postgresql.Driver
  29. mybatis-plus:
  30. configuration:
  31. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  32. mapper-locations: classpath:**/mapper/*.xml

text库中user表

  1. @Data
  2. @TableName("user")
  3. public class User {
  4. @TableId
  5. private Integer uId;
  6. private String uName;
  7. private Integer uAge;
  8. private LocalDateTime uBirthDay;
  9. private Integer dId;
  10. private LocalDateTime createTime;
  11. private LocalDateTime updateTime;
  12. }

Mapper

  1. @DS("text")
  2. @Mapper
  3. public interface UserMapper extends BaseMapper<User> {}

cca库中的config表

  1. @Data
  2. @TableName("config")
  3. public class Config {
  4. @TableId
  5. private Integer configId;
  6. private String configName;
  7. private String configKey;
  8. private String configValue;
  9. private LocalDateTime createTime;
  10. private LocalDateTime updateTime;
  11. }

Mapper

  1. @DS("cca")
  2. @Mapper
  3. public interface ConfigMapper extends BaseMapper<Config> {}

 gene_text库中的call表

  1. @Data
  2. @TableName("call")
  3. public class Call {
  4. @TableId
  5. private String partyId;
  6. private String dn;
  7. private String keyName;
  8. private String value;
  9. private LocalDateTime createTime;
  10. }

Mapper

  1. @DS("geneText")
  2. @Mapper
  3. public interface CallMapper extends BaseMapper<Call> {}

 使用,查询text库中的user表

  1. @RestController
  2. @RequestMapping("/template")
  3. public class TemplateController extends BaseController{
  4. @Autowired
  5. private UserServiceImpl userService;
  6. @PostMapping("/queryTemplate")
  7. public ResponseResult queryTemplate(@RequestBody QueryReq queryReq) {
  8. return success(userService.queryTemplate(queryReq));
  9. }
  10. }
  1. @Service
  2. public class UserServiceImpl implements UserService {
  3. @Autowired
  4. private UserMapper userMapper;
  5. @Override
  6. public Page<User> queryTemplate(QueryReq queryReq) {
  7. // 分页设置
  8. Page<User> userPage = Page.of(1, 10);
  9. // 查询条件设置
  10. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  11. if (StrUtil.isNotBlank(queryReq.getName())) {
  12. queryWrapper.eq("u_name", queryReq.getName());
  13. }
  14. if (queryReq.getAge() != null) {
  15. queryWrapper.eq("u_age", queryReq.getAge());
  16. }
  17. if (queryReq.getCreateTime() != null && queryReq.getEndTime() != null) {
  18. queryWrapper.between("u_birth_day", queryReq.getCreateTime(), queryReq.getEndTime());
  19. }
  20. return userMapper.selectPage(userPage, queryWrapper);
  21. }
  22. }

测试

查询geneText库call表

  1. @RestController
  2. @RequestMapping("/template")
  3. public class TemplateController extends BaseController{
  4. @Autowired
  5. private CallServiceImpl callService;
  6. @GetMapping("/query")
  7. public ResponseResult query() {
  8. return success(callService.queryTemplate());
  9. }
  10. }
  1. @Service
  2. public class CallServiceImpl implements CallService {
  3. @Autowired
  4. private CallMapper callMapper;
  5. @Override
  6. public Page<Call> queryTemplate() {
  7. // 分页设置
  8. Page<Call> callPage = Page.of(1, 10);
  9. // 查询条件设置
  10. QueryWrapper<Call> queryWrapper = new QueryWrapper<>();
  11. queryWrapper.eq("dn", "6001");
  12. return callMapper.selectPage(callPage, queryWrapper);
  13. }
  14. }

测试

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/677923
推荐阅读
相关标签
  

闽ICP备14008679号