整合Springboot2.7 + Mybatis-Plus3.5.3 + Sharding-jdbc4.1.1 读写分离

1.maven依赖


        
            org.apache.shardingsphere
            sharding-jdbc-spring-boot-starter
            ${sharding-sphere.version}
        

 
        
            com.baomidou
            mybatis-plus-boot-starter
            ${mybatis-plus.version}
        
        
            com.baomidou
            mybatis-plus-generator
            ${mybatis-plus.version}
        

2.读写分离配置

spring:
  shardingsphere:
    datasource:
      # 数据库名称
      names: master,slave1,slave2
      master:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:3310/test1?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
        username: root
        password: 123456
      slave1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:3311/test1?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
        username: root
        password: 123456
      slave2:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:3312/test1?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
        username: root
        password: 123456
    # 读写策略
    masterslave:
      load-balance-algorithm-type: round_robin
      name: ms
      master-data-source-name: master
      slave-data-source-names: slave1,slave2
    # 打印日志
    props:
      sql.show: true

3.解决springboot2.6+使用sharding-jdbc-spring-boot-starter 造成SQLFeatureNotSupportedException: isValid的问题

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadata;
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
import org.springframework.boot.jdbc.metadata.HikariDataSourcePoolMetadata;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class DataSourceHealthConfig{

    /**
     * 解决新版Spring中,集成sharding jdbc 健康检查异常
     */
    @Bean
    DataSourcePoolMetadataProvider dataSourcePoolMetadataProvider() {
        return dataSource -> dataSource instanceof HikariDataSource
                ? new HikariDataSourcePoolMetadata((HikariDataSource) dataSource)
                : new NotAvailableDataSourcePoolMetadata();
    }

    /**
     * 不可用的数据源池元数据.
     */
    private static class NotAvailableDataSourcePoolMetadata implements DataSourcePoolMetadata {
        @Override
        public Float getUsage() {
            return null;
        }

        @Override
        public Integer getActive() {
            return null;
        }

        @Override
        public Integer getMax() {
            return null;
        }

        @Override
        public Integer getMin() {
            return null;
        }

        @Override
        public String getValidationQuery() {
            return "select 1";
        }

        @Override
        public Boolean getDefaultAutoCommit() {
            return null;
        }
    }

}

你可能感兴趣的:(mybatis,spring,boot)