SpringBoot+MybatisPlus+Druid (超简单超详细)实现读写分离

说明:

开始: 

  • 使用开源中国发布的优秀框架 SpringBoot 多数据源快速启动器 dynamic-datasource

  • 本文只简单使用,详细请点击上方链接查看文档
  • 本文环境:
    • springboot    2.1.2.RELEASE
    • mysql              8.0.15
    • mybatisplus  2.3.3
    • druid               1.1.10

1. maven导入jar包

<!--主从配置依赖-->
<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
   <version>2.4.2</version>
</dependency>

2. application.yml中配置,替换掉原来的druid配置,下图配置中,改为你自己的主从库的url,用户名,密码

spring:
  # 数据源
  datasource:
    dynamic:
      primary: master
      datasource:
        master:
          username: root
          password: root
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://localhost:3306/xboot?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&useSSL=false
          druid: #以下均为默认值
            initial-size: 3
            max-active: 8
            min-idle: 2
            max-wait: -1
            min-evictable-idle-time-millis: 30000
            max-evictable-idle-time-millis: 30000
            time-between-eviction-runs-millis: 0
            validation-query: select 1
            validation-query-timeout: -1
            test-on-borrow: false
            test-on-return: false
            test-while-idle: true
            pool-prepared-statements: true
            max-open-prepared-statements: 100
            filters: stat,wall
            share-prepared-statements: true
        slave:
          username: root
          password: root
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://localhost:3307/xboot?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&useSSL=false
          druid: #以下均为默认值
            initial-size: 3
            max-active: 8
            min-idle: 2
            max-wait: -1
            min-evictable-idle-time-millis: 30000
            max-evictable-idle-time-millis: 30000
            time-between-eviction-runs-millis: 0
            validation-query: select 1
            validation-query-timeout: -1
            test-on-borrow: false
            test-on-return: false
            test-while-idle: true
            pool-prepared-statements: true
            max-open-prepared-statements: 100
            filters: stat,wall
            share-prepared-statements: true

3. 在启动类@SpringBootApplication注解中,添加排除原生Druid的配置类。如下:

DruidDataSourceAutoConfigure会注入一个DataSourceWrapper,其会在原生的spring.datasource下找url,username,password等。而我们动态数据源的配置路径是变化的。

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

4. 上面三步,已配置好读写分离,下面使用注解即可选择使用主从库

@DS("slave_1") 括号内即是yml中配置的数据源名称,不加注解默认是访问主库master,可加在service方法上,也可加在mapper方法上。

下图示例,是用在service方法

/**
 * @Description: 查出所有是审批人的员工
 * @auther: Hanweihu
 * @date: 8:10 2019/4/16
 * @param: [pageVo]
 * @return: com.baomidou.mybatisplus.plugins.Page
 */
@DS("slave_1")
public Page selectAudit(PageVo pageVo) {
    Wrapper<Staff> entity = new EntityWrapper<Staff>();
    entity.eq("is_audit","1");
    entity.eq("IS_DELETE","0");
    Page<Staff> page = new Page<>(pageVo.getPageNumber(), pageVo.getPageSize());
    List<Staff> staffList = staffMapper.selectPage(page, entity);
    return page.setRecords(staffList);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章