SpringBoot + Mybatis-Plus多數(shù)據(jù)源配置整合dynamic-datasource

pro文件引入依賴(lài)
       <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.6</version>
        </dependency>
        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!--主從配置依賴(lài)-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>2.5.6</version>
        </dependency>
        <!--lombok用來(lái)簡(jiǎn)化實(shí)體類(lèi):需要安裝lombok插件-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
application.yml配置
spring:
  datasource:
    dynamic:
      primary: master #設(shè)置默認(rèn)數(shù)據(jù)源或數(shù)據(jù)源組,master默認(rèn)值(數(shù)據(jù)源名稱(chēng)可以隨意起名,沒(méi)有固定值,eg:db1,db2)
      strict: false #設(shè)置嚴(yán)格模式,默認(rèn)false不啟動(dòng). 啟動(dòng)后在未匹配到指定數(shù)據(jù)源時(shí)候回拋出異常,不啟動(dòng)會(huì)使用默認(rèn)數(shù)據(jù)源.
      datasource:
        master:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://192.168.3.220:3306/mchouse_test1?useUnicode=true&characterEncoding=utf-8
          username: *****
          password: *****
        slave_1:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://112.30.184.149:3306/net_trans_sup_hefei_edi?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
          username: *****
          password: *****
        slave_2:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://120.55.168.100:33066/net_trans_sup_hefei_edi?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
          username: *****
          password: *****
mybatis-plus:
#  configuration:
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #org.apache.ibatis.logging.slf4j.Slf4jImpl
  mapper-locations: classpath:mapper/*.xml #配置mybatis.xml文件路勁 classpath根路徑
  global-config:
    # 邏輯刪除配置
    db-config:
      # 刪除后
      logic-delete-value: 1
      # 刪除前
      logic-not-delete-value: 0
修改Application啟動(dòng)類(lèi)
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})

這里要排除DruidDataSourceAutoConfigure ,因?yàn)镈ruidDataSourceAutoConfigure會(huì)注入一個(gè)DataSourceWrapper,其會(huì)在原生的spring.datasource下找url,username,password等。而我們動(dòng)態(tài)數(shù)據(jù)源的配置路徑是變化的。

創(chuàng)建MybatisPlusConfig
@Configuration
@EnableTransactionManagement
@MapperScan("com.example.md5_demo.com.db.**.mapper")
public class MyBatisPlusConfig {

    /**
     * SQL 執(zhí)行性能分析插件
     * 開(kāi)發(fā)環(huán)境使用,線上不推薦。 maxTime 指的是 sql 最大執(zhí)行時(shí)長(zhǎng)
     */
    @Bean
    @Profile({"dev","test"})// 設(shè)置 dev test 環(huán)境開(kāi)啟
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setMaxTime(100000);//ms,超過(guò)此處設(shè)置的ms則sql不執(zhí)行
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }


    /**
     * 邏輯刪除插件
     */
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }

    /**
     * 分頁(yè)插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}
創(chuàng)建mapper接口
@Mapper
public interface DemoMapper extends BaseMapper<Demo> {

    List<Demo> getAllList();

    @DS("slave_2")
    List<Demo> getShopList();

}
測(cè)試類(lèi)測(cè)試
@SpringBootTest
class Md5DemoApplicationTests {

    @Autowired
    private DemoMapper demoMapper;

    @Test
    void contextLoads() {
        List<Demo> list=demoMapper.getAllList();
        System.out.println(list);
        System.out.println("***************");
        List<Demo> shopList=demoMapper.getShopList();
        System.out.println(shopList);
    }
}

@DS優(yōu)先級(jí):方法 > 類(lèi)
@DS 可以注解在方法上和類(lèi)上,同時(shí)存在方法注解優(yōu)先于類(lèi)上注解,mapper或者service都可以添加,建議只在一個(gè)方法上添加即可。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容