溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot Mybatis配置多數(shù)據(jù)源并分包的方法

發(fā)布時(shí)間:2020-07-28 15:47:16 來(lái)源:億速云 閱讀:256 作者:小豬 欄目:編程語(yǔ)言

這篇文章主要講解了SpringBoot Mybatis配置多數(shù)據(jù)源并分包的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

第一個(gè)子項(xiàng)目ds01即時(shí)使用分包方式完成多數(shù)據(jù)源配置。

總結(jié)項(xiàng)目中出現(xiàn)的問(wèn)題和解決辦法:

數(shù)據(jù)庫(kù)的連接信息:

連接信息是寫在db.properties文件中的:

#數(shù)據(jù)庫(kù)ds1
spring.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1?serverTimezone=UTC
spring.datasource.ds1.username=root
spring.datasource.ds1.password=root
#數(shù)據(jù)庫(kù)ds2
spring.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.ds2.url=jdbc:mysql://localhost:3306/ds2?serverTimezone=UTC
spring.datasource.ds2.username=root
spring.datasource.ds2.password=root

這些信息將在配置類DbConfig1.java中引用。一開始我是通過(guò)使用注解@ImportResource(...)引進(jìn)db.properties文件,但在運(yùn)行時(shí)報(bào)了org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允許有內(nèi)容的錯(cuò)誤;使用這個(gè)注解也是我瞎猜的。后是通過(guò)使用注解@PropertySource(value = "classpath:/db.properties",encoding = "utf-8")解決問(wèn)題。

其次是關(guān)于在配置類中使用@ConfigurationProperties注解自動(dòng)注入連接信息值(value)的問(wèn)題:spring.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1?serverTimezone=UTC

注意要使用.url而不是.jdbc-url;

指定數(shù)據(jù)連接池類型DataType:

數(shù)據(jù)源類型可以在配置類生成DataSource的方法中指定:

@Bean(name = "ds1DataSource")
  @Primary
  @ConfigurationProperties(prefix = "spring.datasource.ds1")
  public DataSource getDataSource(){
    DataSourceBuilder<&#63;> dataSourceBuilder = DataSourceBuilder.create();
    dataSourceBuilder.type(com.alibaba.druid.pool.DruidDataSource.class);
    return dataSourceBuilder.build();
  }

指定***Mapper.xml文件的路徑掃描問(wèn)題:(相當(dāng)重要)

使用配置類進(jìn)行數(shù)據(jù)源相關(guān)進(jìn)行配置后,原先在application.yml中配置的相關(guān)參數(shù)就不起作用了(原因未知),原先我是在application.yml中配置了.xml文件的掃描路徑:

mybatis:
mapper-locations: classpath:/mybatis/**/*.xml
type-aliases-package: com.kong.ds01.model

但在運(yùn)行時(shí)報(bào)錯(cuò):Mapper Bound Error(not found);后來(lái)通過(guò)在配置類中寫入掃描路徑解決:

public final static String mapperXmlLocation = "classpath:mybatis/*/*.xml";

@Bean(name = "ds1SqlSessionFactory")
  @Primary
  public SqlSessionFactory getSqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);
    sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperXmlLocation));
    return sqlSessionFactoryBean.getObject();
  }

而且通過(guò)這種方法表示任意路徑不能使用/**/,要使用/*/,否則識(shí)別不出來(lái)又會(huì)報(bào)相同的錯(cuò)誤,這點(diǎn)真是太坑了!

指定執(zhí)行器的類型(Execute.Type):

可以通過(guò)在配置類中的sqlSessionTemplate中指定:

@Bean(name = "ds1SqlSessionTemplate")
  @Primary
  public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
    return new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH);
  }

指定為BATCH類型后在進(jìn)行批量操作時(shí)效率有明顯的提高。

看完上述內(nèi)容,是不是對(duì)SpringBoot Mybatis配置多數(shù)據(jù)源并分包的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI