SpringBoot多数据源配置插件dynamic-datasource-spring-b。。。⼀、约定
1. 本框架只做 切换数据源 这件核⼼的事情,并不限制你的具体操作,切换了数据源可以做任何CRUD。
2. 配置⽂件所有以下划线 _ 分割的数据源 ⾸部 即为组的名称,相同组名称的数据源会放在⼀个组下。
3. 切换数据源可以是组名,也可以是具体数据源名称。组名则切换时采⽤负载均衡算法切换。
4. 默认的数据源名称为 master ,你可以通过 spring.datasource.dynamic.primary 修改。
5. ⽅法上的注解优先于类上注解。
6. 强烈建议只在service的类和⽅法上添加注解,不建议在mapper上添加注解。
⼆、⽤法
使⽤ @DS 切换数据源
@DS 可以注解在⽅法上和类上,同时存在⽅法注解优先于类上注解。
强烈建议只注解在service实现上。
三、源码⽰例
准备两个数据库:day27和quartz,day27库下查user表,quartz库下查询quartz_user表。
3.1 pom依赖
<dependency>
<groupId>com.baomidou</groupId>
springboot aop<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.2.1</version>
</dependency>
参考完整pom⽂件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.lsh</groupId>
<artifactId>dynamic-datasource</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- SpringBoot整合Mybatis-->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!--JPA-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
<!--阿⾥巴巴数据库连接池Druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.3</version>
</dependency>
<!-- aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--动态数据源插件-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 yml配置⽂件
spring:
profiles:
active: test
spring:
datasource:
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict:false#严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使⽤默认数据源
datasource:
master:
url: jdbc:mysql://localhost:3306/day27?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF8 username: root
password:123456
driver-class-name: sql.jdbc.Driver # 3.2.0开始⽀持SPI可省略此配置
slave_1:
url: jdbc:mysql://localhost:3306/quartz?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF8 username: root
password:123456
driver-class-name: sql.jdbc.Driver
logging:
level:
com.baomidou.dynamic: debug
3.3 实体类
@Data
@Entity
@Table(name ="user")
public class Day27User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
}
@Data
@Entity
@Table(name ="quartz_user")
public class QuartzUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
}
3.4 持久层使⽤JPA
Repository
public interface Day27UserRepository extends JpaRepository<Day27User,Integer>{ }
public interface QuartzUserRepository extends JpaRepository<QuartzUser,Integer>{ }
ServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
Day27UserRepository day27UserRepository;
@Autowired
QuartzUserRepository quartzUserRepository;
@Override
@DS("master")
public List<Day27User>findDay27User(){
List<Day27User> all = day27UserRepository.findAll();
return all;
}
@Override
@DS("slave_1")
public List<QuartzUser>findQuartzUser(){
List<QuartzUser> all = quartzUserRepository.findAll();
return all;
}
}
测试类
@SpringBootTest
@RunWith(SpringRunner.class)
public class UserServiceTest {
@Autowired
UserService userService;
@Test
public void test1(){
List<Day27User> day27User = userService.findDay27User(); day27User.forEach(System.out::println);
}
@Test
public void test2(){
List<QuartzUser> quartzUser = userService.findQuartzUser(); quartzUser.forEach(System.out::println);
}
}
多数据源可⾏。
3.5 持久层使⽤MyBatis
Mapper接⼝
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论