<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;
}
}
网友评论