内容包括动态数据源以及多数据源的自动配置包括jpa和mybatis,包含源码以及使用方法。
<dependency>
<groupId>cn.hiboot.mcngroupId>
<artifactId>mcn-spring-boot-starterartifactId>
<version>2.7.18version>
dependency>
multiple.datasource.hello.url=jdbc:mysql://127.0.0.1:3306/test?createDatabaseIfNotExist=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
multiple.datasource.hello.username=root
multiple.datasource.hello.password=123456
multiple.datasource.world.url=jdbc:mysql://127.0.0.1:3306/web_template?createDatabaseIfNotExist=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
multiple.datasource.world.username=root
multiple.datasource.world.password=123456
dynamic.datasource.enabled=true
@RequestMapping("test")
@RestController
@SwitchSource("hello")
public class TestRestApi {
private UserDao userDao;
public TestRestApi(UserDao userDao) {
this.userDao = userDao;
}
@GetMapping("list")
public RestResp<List<User>> list() {
return new RestResp<>(userDao.findAll());
}
@GetMapping("list2")
@SwitchSource("world")
public RestResp<List<User>> list2() {
return new RestResp<>(userDao.findAll());
}
}
提示:SwitchSource注解既可以用在类上也可以用在方法上,方法上的优先级高
使用jpa:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
<version>2.7.18version>
dependency>
使用mybatis:
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.2version>
dependency>
<dependency>
<groupId>cn.hiboot.mcngroupId>
<artifactId>mcn-spring-boot-starterartifactId>
<version>2.7.18version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
<version>2.7.18version>
dependency>
multiple.datasource.hello.url=jdbc:mysql://127.0.0.1:3306/test?createDatabaseIfNotExist=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
multiple.datasource.hello.username=root
multiple.datasource.hello.password=123456
multiple.datasource.world.url=jdbc:mysql://127.0.0.1:3306/web_template?createDatabaseIfNotExist=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
multiple.datasource.world.username=root
multiple.datasource.world.password=123456
jpa.multiple.datasource.enabled=true
dao层必须在启动类所在包的子包dao下且用数据源的名称当子包名称,如下图所示
@RequestMapping("test")
@RestController
public class TestRestApi {
private UserDao userDao;
private UserDao2 userDao2;
public TestRestApi(UserDao userDao, UserDao2 userDao2) {
this.userDao = userDao;
this.userDao2 = userDao2;
}
@GetMapping("list3")
public RestResp<List<User>> list3() {
List<User> all = userDao2.findAll();
all.addAll(userDao.findAll());
return new RestResp<>(all);
}
}
<dependency>
<groupId>cn.hiboot.mcngroupId>
<artifactId>mcn-spring-boot-starterartifactId>
<version>2.7.18version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.2version>
dependency>
multiple.datasource.hello.url=jdbc:mysql://127.0.0.1:3306/test?createDatabaseIfNotExist=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
multiple.datasource.hello.username=root
multiple.datasource.hello.password=123456
multiple.datasource.world.url=jdbc:mysql://127.0.0.1:3306/web_template?createDatabaseIfNotExist=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
multiple.datasource.world.username=root
multiple.datasource.world.password=123456
mybatis.multiple.datasource.enabled=true
dao层必须在启动类所在包的子包dao下且用数据源的名称当子包名称,如下图所示
@RequestMapping("test")
@RestController
@SwitchSource("hello")
public class TestRestApi {
@GetMapping("list")
public RestResp<List<User>> list() {
return new RestResp<>(userDao.findAll());
}
@GetMapping("list2")
@SwitchSource("world")
public RestResp<List<User>> list2() {
return new RestResp<>(userDao.findAll());
}
}