最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

帶你了解mybatis如何實(shí)現(xiàn)讀寫(xiě)分離

 更新時(shí)間:2021年08月04日 10:44:12   作者:alexander137  
本篇文章主要介紹了MyBatis實(shí)現(xiàn)數(shù)據(jù)讀寫(xiě)分離的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來(lái)幫助

1、spring aop實(shí)現(xiàn)

首先application-test.yml增加如下數(shù)據(jù)源的配置

spring:
  datasource:
    master:
      jdbc-url: jdbc:mysql://master域名:3306/test
      username: root
      password: 123456
      driver-class-name: com.mysql.jdbc.Driver
    slave1:
      jdbc-url: jdbc:mysql://slave域名:3306/test
      username: root   # 只讀賬戶(hù)
      password: 123456
      driver-class-name: com.mysql.jdbc.Driver
    slave2:
      jdbc-url: jdbc:mysql://slave域名:3306/test
      username: root   # 只讀賬戶(hù)
      password: 123456
      driver-class-name: com.mysql.jdbc.Driver
package com.cjs.example.enums;
public enum DBTypeEnum {
    MASTER, SLAVE1, SLAVE2;
}

定義ThreadLocal上下文,將當(dāng)前線(xiàn)程的數(shù)據(jù)源進(jìn)行動(dòng)態(tài)修改

public class DBContextHolder {
    private static  volatile ThreadLocal<DBTypeEnum> contextHolder = new ThreadLocal<>();
    public static synchronized void set(DBTypeEnum dbType) {
        contextHolder.set(dbType);
    }
    public static synchronized DBTypeEnum get() {
        return contextHolder.get();
    }
    public static void master() {
        set(DBTypeEnum.MASTER);
    }
    public static void slave() {
        set(DBTypeEnum.SLAVE1);
    }
    public static void slave2(){ set(DBTypeEnum.SLAVE2); }
    // 清除數(shù)據(jù)源名
    public static void clearDB() {
        contextHolder.remove();
    }
}

重寫(xiě)mybatis數(shù)據(jù)源路由接口,在此修改數(shù)據(jù)源為我們上一塊代碼設(shè)置的上下文的數(shù)據(jù)源

public class MyRoutingDataSource extends AbstractRoutingDataSource {
    @Nullable
    @Override
    protected Object determineCurrentLookupKey() {
        DBTypeEnum dbTypeEnum=DBContextHolder.get();
        return dbTypeEnum;
    }
}

將yml配置的多數(shù)據(jù)源手動(dòng)指定注入

@Configuration
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties("spring.datasource.master")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean
    @ConfigurationProperties("spring.datasource.slave1")
    public DataSource slave1DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public DataSource myRoutingDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
                                          @Qualifier("slave1DataSource") DataSource slave1DataSource) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DBTypeEnum.MASTER, masterDataSource);
        targetDataSources.put(DBTypeEnum.SLAVE1, slave1DataSource);
        MyRoutingDataSource myRoutingDataSource = new MyRoutingDataSource();
        myRoutingDataSource.setDefaultTargetDataSource(masterDataSource);
        myRoutingDataSource.setTargetDataSources(targetDataSources);
        return myRoutingDataSource;
    }
}

sqlsession注入以上我們配置的datasource路由

@EnableTransactionManagement
@Configuration
@Import({TableSegInterceptor.class})
public class MyBatisConfig {
    @Resource(name = "myRoutingDataSource")
    private DataSource myRoutingDataSource;
    @Autowired
    private MybatisConfigProperty mybatisConfigProperty;
    @Autowired
    private TableSegInterceptor tableSegInterceptor;
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(myRoutingDataSource);
        // SpringBoot項(xiàng)目集成mybatis打包為jar運(yùn)行時(shí)setTypeAliasesPackage無(wú)效解決
        VFS.addImplClass(SpringBootVFS.class);
        sqlSessionFactoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources(mybatisConfigProperty.getMapperLocations()));
        sqlSessionFactoryBean.setTypeAliasesPackage(mybatisConfigProperty.getTypeAliasesPackage());
        sqlSessionFactoryBean.setConfigLocation(
                new PathMatchingResourcePatternResolver().getResource(mybatisConfigProperty.getConfigLocation()));
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{tableSegInterceptor});
        return sqlSessionFactoryBean.getObject();
    }
    @Bean
    public PlatformTransactionManager platformTransactionManager() {
        return new DataSourceTransactionManager(myRoutingDataSource);
    }
}

spring aop攔截指定前綴的service方法,并設(shè)置對(duì)應(yīng)所屬的上下文

@Aspect
@Component
public class DataSourceAop {
    @Pointcut("!@annotation(com.ask.student.interceptor.annotation.Master) " +
            "&& (execution(* com.ask.student.service..*.select*(..)) " +
            "|| execution(* com.ask.student.service..*.get*(..))" +
            "|| execution(* com.ask.student.service..*.find*(..))" +
            ")")
    public void readPointcut() {
    }
    @Pointcut("@annotation(com.ask.student.interceptor.annotation.Master) " +
            "|| execution(* com.ask.student.service..*.insert*(..)) " +
            "|| execution(* com.ask.student.service..*.clean*(..)) " +
            "|| execution(* com.ask.student.service..*.reset*(..)) " +
            "|| execution(* com.ask.student.service..*.add*(..)) " +
            "|| execution(* com.ask.student.service..*.update*(..)) " +
            "|| execution(* com.ask.student.service..*.edit*(..)) " +
            "|| execution(* com.ask.student.service..*.delete*(..)) " +
            "|| execution(* com.ask.student.service..*.remove*(..))")
    public void writePointcut() {
    }
    @Before("readPointcut()")
    public void read() {
        DBContextHolder.slave();
    }
    @Before("writePointcut()")
    public void write() {
        DBContextHolder.master();
    }
    @After("readPointcut()||writePointcut()")
    public void afterSwitchDS(){
        DBContextHolder.clearDB();
    }
}

以上最后一個(gè)方法的作用,在攔截器中獲取后及時(shí)清除避免導(dǎo)致來(lái)回切換當(dāng)前線(xiàn)程變量延遲問(wèn)題導(dǎo)致某些操作的數(shù)據(jù)源錯(cuò)誤

DBContextHolder.clearDB();

@After("readPointcut()||writePointcut()")

public void afterSwitchDS(){

DBContextHolder.clearDB();

}

2、mybatis-plus的實(shí)現(xiàn)方式

這個(gè)方式配置簡(jiǎn)單,代碼少,很多事情mybatis-plus都已經(jīng)做好了,推薦使用

yml配置如下

  datasource:
    dynamic:
      primary: master  #設(shè)置默認(rèn)的數(shù)據(jù)源或者數(shù)據(jù)源組,默認(rèn)值即為master
      strict: false #設(shè)置嚴(yán)格模式,默認(rèn)false不啟動(dòng). 啟動(dòng)后在未匹配到指定數(shù)據(jù)源時(shí)候會(huì)拋出異常,不啟動(dòng)則使用默認(rèn)數(shù)據(jù)源.
      datasource:
        master:
          url: jdbc:mysql://xxx:3306/db0?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
          username: admin
          password: 123456
          driver-class-name: com.mysql.cj.jdbc.Driver
          type: com.zaxxer.hikari.HikariDataSource
          hikari:
            minimum-idle: 5
            maximum-pool-size: 15
            auto-commit: true
            idle-timeout: 30000
            pool-name: springHikariCP
            max-lifetime: 1800000
            connection-timeout: 30000
            connection-test-query: SELECT 1
        slave1:
          url: jdbc:mysql://xxx:3306/db2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
          username: admin
          password: 123456
          driver-class-name: com.mysql.cj.jdbc.Driver
          type: com.zaxxer.hikari.HikariDataSource
          hikari:
            minimum-idle: 5
            maximum-pool-size: 15
            auto-commit: true
            idle-timeout: 30000
            pool-name: springHikariCP
            max-lifetime: 1800000
            connection-timeout: 30000
            connection-test-query: SELECT 1
        slave2:
          url: jdbc:mysql://xxx:3306/db3?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
          username: admin
          password: 123456
          driver-class-name: com.mysql.cj.jdbc.Driver
          type: com.zaxxer.hikari.HikariDataSource
          hikari:
            minimum-idle: 5
            maximum-pool-size: 15
            auto-commit: true
            idle-timeout: 30000
            pool-name: springHikariCP
            max-lifetime: 1800000
            connection-timeout: 30000
            connection-test-query: SELECT 1

使用起來(lái)非常簡(jiǎn)單,只需要加上這個(gè)master的注解即可

    @Override
    @DS("master")
    public DestMedia getOneByCodeFromEpg(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("code", code);
        return super.getOne(queryWrapper);
    }

總結(jié)

本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • SpringBoot使用JWT實(shí)現(xiàn)登錄驗(yàn)證的方法示例

    SpringBoot使用JWT實(shí)現(xiàn)登錄驗(yàn)證的方法示例

    這篇文章主要介紹了SpringBoot使用JWT實(shí)現(xiàn)登錄驗(yàn)證的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • jvm垃圾回收GC調(diào)優(yōu)基礎(chǔ)原理分析

    jvm垃圾回收GC調(diào)優(yōu)基礎(chǔ)原理分析

    談到調(diào)優(yōu),這一定是針對(duì)特定場(chǎng)景、特定目的的事情, 對(duì)于 GC 調(diào)優(yōu)來(lái)說(shuō),首先就需要清楚調(diào)優(yōu)的目標(biāo)是什么?從性能的角度看,通常關(guān)注三個(gè)方面,內(nèi)存占用(footprint)、延時(shí)(latency)和吞吐量(throughput)
    2022-01-01
  • idea 2023.1字體設(shè)置及自動(dòng)調(diào)整大小的圖文教程

    idea 2023.1字體設(shè)置及自動(dòng)調(diào)整大小的圖文教程

    這篇文章主要介紹了idea 2023.1字體設(shè)置及自動(dòng)調(diào)整大小的教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • Java多線(xiàn)程中的CountDownLatch詳細(xì)解讀

    Java多線(xiàn)程中的CountDownLatch詳細(xì)解讀

    這篇文章主要介紹了Java多線(xiàn)程中的CountDownLatch詳細(xì)解讀,一個(gè)同步輔助類(lèi),在完成一組正在其他線(xiàn)程中執(zhí)行的操作之前,它允許一個(gè)或多個(gè)線(xiàn)程一直等待,用給定的計(jì)數(shù) 初始化 CountDownLatch,需要的朋友可以參考下
    2023-11-11
  • 解決springboot接入springfox-swagger2遇到的一些問(wèn)題

    解決springboot接入springfox-swagger2遇到的一些問(wèn)題

    這篇文章主要介紹了解決springboot接入springfox-swagger2遇到的一些問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java中將List拆分為多個(gè)小list集合的實(shí)現(xiàn)代碼

    Java中將List拆分為多個(gè)小list集合的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java中如何將List拆分為多個(gè)小list集合,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • jdk17?SpringBoot?JPA集成多數(shù)據(jù)庫(kù)的示例詳解

    jdk17?SpringBoot?JPA集成多數(shù)據(jù)庫(kù)的示例詳解

    這篇文章主要介紹了jdk17?SpringBoot?JPA集成多數(shù)據(jù)庫(kù)的示例代碼,包括配置類(lèi)、請(qǐng)求攔截器、線(xiàn)程上下文等相關(guān)知識(shí),代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • springboot maven 項(xiàng)目打包jar 最后名稱(chēng)自定義的教程

    springboot maven 項(xiàng)目打包jar 最后名稱(chēng)自定義的教程

    這篇文章主要介紹了springboot maven 項(xiàng)目打包jar 最后名稱(chēng)自定義的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • Spring Boot多模塊化后,服務(wù)間調(diào)用的坑及解決

    Spring Boot多模塊化后,服務(wù)間調(diào)用的坑及解決

    這篇文章主要介紹了Spring Boot多模塊化后,服務(wù)間調(diào)用的坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 如何獲取springboot打成jar后的classpath

    如何獲取springboot打成jar后的classpath

    這篇文章主要介紹了如何獲取springboot打成jar后的classpath問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07

最新評(píng)論

外汇| 武定县| 千阳县| 石狮市| 中宁县| 拜泉县| 宁德市| 万州区| 宜春市| 元谋县| 惠东县| 运城市| 邳州市| 砚山县| 金寨县| 余干县| 海原县| 肇源县| 临猗县| 玉树县| 响水县| 弥勒县| 蒙山县| 台东市| 赤壁市| 甘德县| 宜兰市| 鹿泉市| 澄江县| 泰宁县| 西峡县| 栾城县| 广东省| 涿鹿县| 洞口县| 礼泉县| 兴仁县| 栾城县| 喀喇沁旗| 盐津县| 邢台县|