多数据源

        
            com.baomidou
            dynamic-datasource-spring-boot-starter
            3.0.0
        
spring:
  datasource:
    dynamic:
      primary: master #设置默认的数据源或者数据源组,默认值即为master
      datasource:
        master:
          username: root
          password: root
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://localhost:3306/dp?serverTimezone=Asia/Shanghai
        slave_1:
          username: root
          password: root
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://localhost:3306/cs?serverTimezone=Asia/Shanghai
        slave_2:
          username: root
          password: root
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://localhost:3306/pdbs?serverTimezone=Asia/Shanghai
@Controller
public class MyController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("find")
    @ResponseBody
    public List> find(){
        List> list = jdbcTemplate.queryForList("select * from menu");
        return  list;
    }

    @DS(value = "slave_1")
    @GetMapping("find1")
    @ResponseBody
    public List> find1(){
        List> list = jdbcTemplate.queryForList("select * from qiye");
        return  list;
    }

    @DS(value = "slave_2")
    @GetMapping("find2")
    @ResponseBody
    public List> find2(){
        List> list = jdbcTemplate.queryForList("select * from pdbs_admin");
        return  list;
    }
}

你可能感兴趣的:(多数据源)