dynamic-datasource-spring-boot-starter 是一个基于 springboot 的快速集成多数据源的启动器。
- 其支持
Jdk 1.7+ 、SpringBoot 1.5.x 和 2.x.x 。
1.Maven依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.4.2</version>
</dependency>
2. @DS注解
@DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解。建议注解在serviceImpl类或mapper接口方法上。
注意:从2.0.0 不再支持@DS空注解 ,您 必须 指明你所需要的数据库 组名 或者 具体某个数据库名称 。
注解 | 结果 |
---|
没有@DS注解 | 默认数据源 | @DS(“dsName”) | 数据源名称 |
3.普通Hihari连接池
SpringBoot 2.x默认是使用HikariCP的, 所以觉得HikariCP可以的不需要换Druid。
3.1 yml配置
server:
port: 8081
spring:
datasource:
dynamic:
primary: auth
datasource:
auth:
url: jdbc:mysql://localhost:3306/auth?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mydb1:
url: jdbc:mysql://localhost:3306/mydb1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: "classpath:mapper/*.xml"
logging:
level:
com:
demo:
auth:
mapper : debug
4.Druid连接池
4.1 Druid依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
4.2 yml配置
server:
port: 8081
spring:
datasource:
dynamic:
primary: auth
datasource:
auth:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/auth?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
initial-size: 1
max-active: 20
min-idle: 1
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
validation-query: SELECT 'x'
test-on-borrow: false
test-on-return: false
test-while-idle: true
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
filters: stat,wall
web-stat-filter.enabled: true
web-stat-filter.url-pattern: /*
web-stat-filter.exclusions: "*.js , *.gif ,*.jpg ,*.png ,*.css ,*.ico , /druid/*"
web-stat-filter.session-stat-max-count: 1000
web-stat-filter.profile-enable: true
stat-view-servlet.enabled: true
stat-view-servlet.url-pattern: /druid/*
stat-view-servlet.reset-enable: true
mydb1:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/mydb1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
initial-size: 1
max-active: 20
min-idle: 1
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
validation-query: SELECT 'x'
test-on-borrow: false
test-on-return: false
test-while-idle: true
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
filters: stat,wall
web-stat-filter.enabled: true
web-stat-filter.url-pattern: /*
web-stat-filter.exclusions: "*.js , *.gif ,*.jpg ,*.png ,*.css ,*.ico , /druid/*"
web-stat-filter.session-stat-max-count: 1000
web-stat-filter.profile-enable: true
stat-view-servlet.enabled: true
stat-view-servlet.url-pattern: /druid/*
stat-view-servlet.reset-enable: true
mybatis:
mapper-locations: "classpath:mapper/*.xml"
logging:
level:
com:
demo:
auth:
mapper : debug
4.3 排除原生的Druid配置
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
5. UserController
import com.demo.auth.entity.User;
import com.demo.auth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/insertOne")
public User insertOne() {
User user = new User();
user.setUsername("ACGkaka1");
user.setPassword("123456");
user.setEmail("123@123.com");
user.setPhone("15588888888");
user.setDelFlag("0");
return userService.insert(user);
}
@GetMapping("/insertOne2")
public User insertOne2() {
User user = new User();
user.setUsername("ACGkaka2");
user.setPassword("123456");
user.setEmail("123@123.com");
user.setPhone("15588888888");
user.setDelFlag("0");
return userService.insert2(user);
}
@GetMapping("/findAll")
public List<User> findAll() {
return userService.findAll();
}
@GetMapping("/findAll2")
public List<User> findAll2() {
return userService.findAll2();
}
}
6. UserServiceImpl
import com.baomidou.dynamic.datasource.annotation.DS;
import com.demo.auth.entity.User;
import com.demo.auth.mapper.UserMapper;
import com.demo.auth.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List<User> findAll() {
return this.userMapper.queryAllByLimit(1, -1);
}
@DS("mydb1")
@Override
public List<User> findAll2() {
return this.userMapper.queryAllByLimit(1, -1);
}
@Override
public User insert(User user) {
this.userMapper.insert(user);
return user;
}
@DS("mydb1")
@Override
public User insert2(User user) {
this.userMapper.insert(user);
return user;
}
}
7.测试
7.1 新增数据
在auth数据库新增一条数据:http://localhost:8081/user/insertOne
在mydb1数据库新增一条数据:http://localhost:8081/user/insertOne2
7.2 查询数据
在auth数据库查询数据:http://localhost:8081/user/findAll
在mydb1数据库查询数据:http://localhost:8081/user/findAll2
7.3 测试结果
- /user/findAll 接口查询的是auth数据库中存储的“ACGkaka1”;
- /user/findAll2 接口查询的是mydb数据库中存储的“ACGkaka2”;
- 多数据源配置成功。
8.源码地址:
https://gitee.com/acgkaka/SpringBootExamples/tree/master/springboot-dynamic-datasource
整理完毕,完结撒花~
参考地址:
1.springBoot(六) 调用多数据源 dynamic-datasource,https://blog.csdn.net/Tang_5253/article/details/101095855 2.dynamic-datasource SpringBoot 多数据源快速启动器,https://www.oschina.net/p/dynamic-datasource-spring-boot-starter?hmsr=aladdin1e1
|