MyBatisPlus实现MySQL的读写分离

方式1,使用MyBatisPlus插件实现读写分离

导包

<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.4.0</version>
</dependency>

<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
	<version>3.4.0</version>
</dependency>

配置

server:
  port: 9090
  servlet:
    encoding:
      charset: UTF-8
    context-path: /visual-web
spring:
  application:
    name: visual-web
  datasource:
    dynamic:
      primary: master
      strict: false   #是否弃用严格模式,如果启用在味匹配到指定数据源时抛出异常
      datasource:
        master:
          url: jdbc:mysql://IP:3390/bcimdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true
          username: root
          password: PWD
          driverClassName: com.mysql.cj.jdbc.Driver
        slave1:
          url: jdbc:mysql://IP:3379/bcimdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true
          username: root
          password: PWD
          driverClassName: com.mysql.cj.jdbc.Driver
        slave2:
          url: jdbc:mysql://IP:3369/bcimdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true
          username: root
          password: PWD
          driverClassName: com.mysql.cj.jdbc.Driver

指定方法使用指定数据源

@Slf4j
@Service
public class MenuServiceImpl {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private DataSource dataSource;

    public void getDataSourceUrl() {
        try {
            String url = dataSource.getConnection().getMetaData().getURL();
            log.info("======================= url = " + url);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @DS("slave1")
    public List<MenuDTO> listMenu() {
        getDataSourceUrl();
        List<MenuEntity> menuEntityList = menuMapper.selectMenuList();
        return CopyUtils.copyListByShallow(menuEntityList, MenuDTO.class);
    }
}

方式2,自定义切面实现读写分离

  • 思路:
  1. 配置多个数据源。
  2. 使用aop拦截方法的请求。

导包

<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.4.0</version>
</dependency>

配置

server:
  port: 9090
  servlet:
    encoding:
      charset: UTF-8
    context-path: /visual-web
spring:
  application:
    name: visual-web
  datasource:
    dynamic:
      primary: master
      strict: false   #是否弃用严格模式,如果启用在味匹配到指定数据源时抛出异常
      datasource:
        master:
          url: jdbc:mysql://IP:3390/bcimdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true
          username: root
          password: PWD
          driverClassName: com.mysql.cj.jdbc.Driver
        slave1:
          url: jdbc:mysql://IP:3379/bcimdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true
          username: root
          password: PWD
          driverClassName: com.mysql.cj.jdbc.Driver
        slave2:
          url: jdbc:mysql://IP:3369/bcimdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true
          username: root
          password: PWD
          driverClassName: com.mysql.cj.jdbc.Driver
posted @ 2023-07-06 09:42  sunpeiyu  阅读(253)  评论(0编辑  收藏  举报