美文网首页
多数据源

多数据源

作者: Ktry | 来源:发表于2021-07-05 11:52 被阅读0次
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
                <version>3.0.0</version>
            </dependency>
    
    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<Map<String, Object>> find(){
            List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from menu");
            return  list;
        }
    
        @DS(value = "slave_1")
        @GetMapping("find1")
        @ResponseBody
        public List<Map<String, Object>> find1(){
            List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from qiye");
            return  list;
        }
    
        @DS(value = "slave_2")
        @GetMapping("find2")
        @ResponseBody
        public List<Map<String, Object>> find2(){
            List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from pdbs_admin");
            return  list;
        }
    }
    

    相关文章

      网友评论

          本文标题:多数据源

          本文链接:https://www.haomeiwen.com/subject/dqgbultx.html