SpringBoot+Mybatis-Plus實現mysql讀寫分離方案的示例代碼

1. 引入mybatis-plus相關包,pom.xml文件

mybatis-plus包

2. 配置文件application.property增加多庫配置

mysql 數據源配置

spring.datasource.primary.jdbc-url=jdbc:mysql://xx.xx.xx.xx:3306/portal?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
#mysql slave 數據源配置

spring.datasource.slave.jdbc-url=jdbc:mysql://xx.xx.xx.xx:3306/portal?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.slave.username=root
spring.datasource.slave.password=root
spring.datasource.slave.driver-class-name=com.mysql.cj.jdbc.Driver

3. 配置數據源及註解

數據源配置 MultiDataSourceConfig.Java

/**
 * 配置多數據源
 */
@Profile("dev")//開發模式配置文件
@Configuration
@MapperScan(basePackages = "com.csc.portal.mapper")//掃描包
public class MultiDataSourceConfig {

  /**
   * 主數據源
   * @return
   */
  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.primary")
  public DataSource masterDataSource() {
    return DataSourceBuilder.create().build();
  }

  /**
   * 從數據源
   * @return
   */
  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.slave")
  public DataSource slaveDataSource() {
    return DataSourceBuilder.create().build();
  }

  /**
   * 路由數據源,前面兩個數據源是為瞭創建此數據源
   * @param masterDataSource 主數據源
   * @param slaveDataSource 從數據源
   * @return
   */
  @Bean
  public DataSource myRoutingDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
                     @Qualifier("slaveDataSource") DataSource slaveDataSource) {
    Map<Object, Object> targetDataSources = new HashMap<>();
    targetDataSources.put(DBTypeEnum.MASTER, masterDataSource);
    targetDataSources.put(DBTypeEnum.SLAVE, slaveDataSource);
    MyRoutingDataSource myRoutingDataSource = new MyRoutingDataSource();
    myRoutingDataSource.setDefaultTargetDataSource(slaveDataSource);//設置默認數據源
    myRoutingDataSource.setTargetDataSources(targetDataSources);//設置路由表,使用map的key,value方式得到對應數據源
    return myRoutingDataSource;
  }

數據庫枚舉類

public enum DBTypeEnum {
 MASTER, SLAVE;
}

註解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Master {
}
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Slave {
}

4. Mybatis-plus配置

@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = "com.csc.portal.mapper")
public class MybatisPlusConfig {
  /**
  * 分頁插件
  */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
  }

  @Resource(name = "myRoutingDataSource")
  private DataSource myRoutingDataSource;
  /**
  * 使用MyBatis Plus的sqlSessionFactory代替,
  * 此處註意mybatis與mybatisPlus的配置不同,不然掃描不到對數據操作的方法。會報未綁定錯誤
  * @return sqlSessionFactory
  * @throws Exception
  */
  @Bean
  public SqlSessionFactory sqlSessionFactory() throws Exception {
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(myRoutingDataSource);
    sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
    MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
    sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
    return sqlSessionFactoryBean.getObject();
  }

  /**
  * 此處為使用mybatis時的sqlsessionFactory配置
  * @return
  * @throws Exception
  */
  /*
  @Bean
  public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(myRoutingDataSource);
    sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
    return sqlSessionFactoryBean.getObject();
  }
  */

  /**
  * 事務配置
  * @return 事務管理器
  */
  @Bean
  public DataSourceTransactionManager transactionManager() {
    DataSourceTransactionManager tx = new DataSourceTransactionManager();
    tx.setDataSource(myRoutingDataSource);
    return tx;
  }

5. 增加數據源管理類

DBContextHolder.java

public class DBContextHolder {

  /**
   * 外部一個請求將會產生一個線程與之對應,每個線程的變量可用ThreadLocal進行存儲
   */
  private static final ThreadLocal<DBTypeEnum> contextHolder = new ThreadLocal<>();

  public static void set(DBTypeEnum dbType) {
    contextHolder.set(dbType);
  }

  public static DBTypeEnum get() {
    return contextHolder.get();
  }

  public static void master() {
    set(DBTypeEnum.MASTER);
    System.out.println("切換到master");
  }

  public static void slave() {
    set(DBTypeEnum.SLAVE);
    System.out.println("切換到slave");
  }

}

指定選擇數據源

MyRoutingDataSource.java 方法determineCurrentLookupKey決定最終使用哪個數據源進行操作,若為空則使用默認數據源。

public class MyRoutingDataSource extends AbstractRoutingDataSource {
  @Nullable
  @Override
  protected Object determineCurrentLookupKey() {
    System.out.println("線程名:"+Thread.currentThread().getName()+":"+DBContextHolder.get());
    return DBContextHolder.get();
/*    if (DBContextHolder.get() != null) {
      System.out.println("線程名:"+Thread.currentThread().getName()+":"+DBContextHolder.get());
      return DBContextHolder.get();
    } else {
      System.out.println("未匹配到指定數據庫,默認切換到Master");
      return DBTypeEnum.MASTER;
    }*/
    //return DBContextHolder.get();
  }

}

6. 增加aop切面

@Aspect
@Component
@Order(0)//配置註解優先級,優於事物註解@Transactional先進行數據源切換,
//不然在事物中進行數據源切換無效
public class DataSourceAop {

  @Pointcut(/*"!@annotation(com.csc.portal.annotation.Master) " +
      "&& (execution(* com.csc.portal.service..*.select*(..)) " +
      "|| execution(* com.csc.portal.service..*.get*(..))"+*/
      " @annotation(com.csc.portal.annotation.Slave)")
  public void readPointcut() {

  }


  @Pointcut("@annotation(com.csc.portal.annotation.Master) " //+
     /* "|| execution(* com.csc.portal.service..*.insert*(..)) " +
      "|| execution(* com.csc.portal.service..*.add*(..)) " +
      "|| execution(* com.csc.portal.service..*.update*(..)) " +
      "|| execution(* com.csc.portal.service..*.edit*(..)) " +
      "|| execution(* com.csc.portal.service..*.delete*(..)) " +
      "|| execution(* com.csc.portal.service..*.remove*(..))"*/)
  public void writePointcut() {

  }

  @Before("readPointcut()")
  public void read() {
    //獲取攔截類
    DBContextHolder.slave();
    System.out.println(Thread.currentThread().getName()+DBContextHolder.get());
  }

  @Before("writePointcut()")
  public void write() {
    //獲取攔截類
/*    String className = pjp.getTarget().getClass().getName();
    System.out.println("當前線程"+Thread.currentThread().getName()+" 攔截類為:" + className);

    //獲取攔截的方法名
    MethodSignature msig = (MethodSignature) pjp.getSignature();
    Method currentMethod = null;
    try {
      currentMethod = pjp.getTarget().getClass().getMethod(msig.getName(), msig.getParameterTypes());
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    }
    String methodName = currentMethod.getName();
    System.out.println("攔截方法名為:" + methodName);*/
    DBContextHolder.master();
    System.out.println(Thread.currentThread().getName()+DBContextHolder.get());
  }  
}

6. 實際應用

  1. 在service層方法前增加註解@Master表示使用主庫,進行增刪改的操作使用主庫。
  2. 在service層方法前增加註解@Slave表示使用從庫,進行查的操作使用從庫,默認使用從庫,可不配置。
  3. @ Transactional註解加到service層,增加瞭@Transactional註解後,啟用事務後,一個事務內部的connection是復用的,所以就算AOP切瞭數據源字符串,但是數據源並不會被真正修改。所以@Transactional註解不要寫在controller層,不然在service層也切換不瞭數據源。
  4. @Transactional與@Master可同時使用,已經配置@Master註解的優先級較高,先切換數據源後執行事務。

到此這篇關於SpringBoot+Mybatis-Plus實現mysql讀寫分離方案的示例代碼的文章就介紹到這瞭,更多相關SpringBoot Mybatis-Plus mysql讀寫分離內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: