欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot 整合mybatis 使用多數(shù)據(jù)源的實(shí)現(xiàn)方法

 更新時(shí)間:2018年03月06日 09:10:53   作者:互扯程序  
這篇文章主要介紹了Spring Boot 整合mybatis 使用多數(shù)據(jù)源的實(shí)現(xiàn)方法,需要的朋友可以參考下

前言

本篇教程偏向?qū)崙?zhàn),程序猿直接copy代碼加入到自己的項(xiàng)目中做簡(jiǎn)單的修修改改便可使用,而對(duì)于springboot以及mybatis不在此進(jìn)行展開介紹,如有讀者希望了解可以給我留言,并持續(xù)關(guān)注,我后續(xù)會(huì)慢慢更新。(黑色區(qū)域代碼部分,安卓手機(jī)可手動(dòng)向左滑動(dòng),來查看全部代碼)

整合

其實(shí)整合很簡(jiǎn)單,如果是用gradle的話,在build.gradle文件里加入

compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1')

如果是用maven的話在pom.xml文件里加入

 

單庫配置:

引入之后,默認(rèn)情況下,Spring Boot會(huì)自動(dòng)為我們配置好一個(gè)DataSource,它會(huì)在classpath中搜索H2、hsqldb等內(nèi)存數(shù)據(jù)庫的jar包,如果找到了,就會(huì)自動(dòng)配置一個(gè)內(nèi)存數(shù)據(jù)庫的DataSource。

如果在application.yml或application.property中指定了spring.datasource.*的相關(guān)配置參數(shù),Spring Boot就會(huì)使用該配置創(chuàng)建一個(gè)DataSource。

然后會(huì)自動(dòng)創(chuàng)建使用該DataSource的SqlSessionFactoryBean以及SqlSessionTemplate。會(huì)自動(dòng)掃描你的Mappers,連接到SqlSessionTemplate,并注冊(cè)到Spring上下文中。

 spring.datasource.url=jdbc:mysql://localhost/test
 spring.datasource.username=dbuser
 spring.datasource.password=dbpass
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver

更多參數(shù)請(qǐng)查看DataSourceProperties

多庫配置:

由于業(yè)務(wù)需要,項(xiàng)目要同時(shí)使用多個(gè)數(shù)據(jù)庫進(jìn)行業(yè)務(wù)開發(fā):

首先,我們必須在application.property中自定義兩個(gè)數(shù)據(jù)源的配置,一個(gè)使用first.datasource.*,另一個(gè)使用second.datasource.*,為了能使別人一眼看出連接的是什么庫,可以使用數(shù)據(jù)庫命名,比如user庫,則可以使用user.datasource.*,在使用多數(shù)據(jù)源的時(shí)候,所有必要配置都不能省略。

first.datasource.url=jdbc:mysql://localhost/first
first.datasource.username=dbuser1
first.datasource.password=dbpass1
first.datasource.driver-class-name=com.mysql.jdbc.Driver
first.datasource.type=com.alibaba.druid.pool.DruidDataSource//我用的是Druid,也可以不加用默認(rèn)的
second.datasource.url=jdbc:mysql://localhost/second
second.datasource.username=dbuser2
second.datasource.password=dbpass2
second.datasource.driver-class-name=com.mysql.jdbc.Driver
second.datasource.type=com.alibaba.druid.pool.DruidDataSource

直接上代碼,我的做法是將兩個(gè)數(shù)據(jù)源用兩個(gè)配置類創(chuàng)建:

@Configuration
@MapperScan(basePackages = {"com.user.server.dao"}, sqlSessionTemplateRef = "userSqlSessionTemplate")
public class UserMybatisConfig {
 @Bean(name = "userDataSource")
 @Primary //必須加此注解,不然報(bào)錯(cuò),下一個(gè)類則不需要添加
 @ConfigurationProperties(prefix = "first.datasource") // prefix值必須是application.properteis中對(duì)應(yīng)屬性的前綴
 public DataSource userDataSource() {
  return DataSourceBuilder.create().build();
 }
 @Bean
 public SqlSessionFactory userSqlSessionFactory(@Qualifier("userDataSource") DataSource dataSource) throws Exception {
  SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  bean.setDataSource(dataSource);
  //添加XML目錄
  ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
 try {
   bean.setMapperLocations(resolver.getResources("classpath*:com/user/server/dao/mapping/*.xml"));
   return bean.getObject();
  } catch (Exception e) {
   e.printStackTrace();
   throw new RuntimeException(e);
  }
 }
 @Bean
 public SqlSessionTemplate userSqlSessionTemplate(@Qualifier("userSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
  SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
  return template;
 }
}
@Configuration
@MapperScan(basePackages = {"com.airmi.server.dao"}, sqlSessionTemplateRef = "autoTestSqlSessionTemplate")
 public class AutoTestMybatisConfig {
  @Bean
  @ConfigurationProperties(prefix = "autotest.datasource")
   public DataSource autoTestDataSource() {
   return DataSourceBuilder.create().build();
  }
  @Bean
  public SqlSessionTemplate autoTestSqlSessionTemplate(@Qualifier("autoTestSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
   SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory);
    return template;
   }
  @Bean
  public SqlSessionFactory autoTestSqlSessionFactory(@Qualifier("autoTestDataSource") DataSource dataSource) throws Exception {
   SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
   bean.setDataSource(dataSource);
   //添加XML目錄
   ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
   try {
    bean.setMapperLocations(resolver.getResources("classpath*:com/airmi/server/dao/mapping/*.xml"));
    return bean.getObject();
   } catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException(e);
   }
  }
 }

 @Primary //該注解表示在同一個(gè)接口有多個(gè)實(shí)現(xiàn)類可以注入的時(shí)候,默認(rèn)選擇哪一個(gè),而不是讓autowire注解報(bào)錯(cuò),官網(wǎng)要求當(dāng)多個(gè)數(shù)據(jù)源時(shí),必須指定一個(gè)datasource,另一個(gè)datasource則不用添加。

@Qualifier 根據(jù)名稱進(jìn)行注入,通常是在具有相同的多個(gè)類型的實(shí)例的一個(gè)注入(例如有多個(gè)DataSource類型的實(shí)例)。

 @MapperScan (basePackages = {"com.user.server.dao"}, sqlSessionTemplateRef = "userSqlSessionTemplate") basePackages為mapper所在的包,sqlSessionTemplateRef要引用的實(shí)例。

user代碼結(jié)構(gòu)如下:

 

總結(jié)

以上所述是小編給大家介紹的Spring Boot 整合mybatis 使用多數(shù)據(jù)源的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • SpringBoot短鏈接跳轉(zhuǎn)的代碼實(shí)現(xiàn)

    SpringBoot短鏈接跳轉(zhuǎn)的代碼實(shí)現(xiàn)

    短鏈跳轉(zhuǎn)是一種通過將長(zhǎng)鏈接轉(zhuǎn)換為短鏈接的方式,以便在互聯(lián)網(wǎng)上進(jìn)行鏈接共享和傳播的技術(shù),短鏈將原始長(zhǎng)鏈接通過特定算法轉(zhuǎn)換為較短的鏈接,使得它更容易分享、傳播和展示,本文給大家介紹了SpringBoot短鏈接跳轉(zhuǎn)的代碼實(shí)現(xiàn),需要的朋友可以參考下
    2024-03-03
  • SpringDataJPA之Specification復(fù)雜查詢實(shí)戰(zhàn)

    SpringDataJPA之Specification復(fù)雜查詢實(shí)戰(zhàn)

    這篇文章主要介紹了SpringDataJPA之Specification復(fù)雜查詢實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java的ThreadPoolExecutor業(yè)務(wù)線程池詳細(xì)解析

    Java的ThreadPoolExecutor業(yè)務(wù)線程池詳細(xì)解析

    這篇文章主要介紹了Java線程池ThreadPoolExecutor詳細(xì)解析,任務(wù)剛開始進(jìn)來的時(shí)候就創(chuàng)建核心線程,核心線程滿了會(huì)把任務(wù)放到阻塞隊(duì)列,阻塞隊(duì)列滿了之后才會(huì)創(chuàng)建空閑線程,達(dá)到最大線程數(shù)之后,再有任務(wù)進(jìn)來,就只能執(zhí)行拒絕策略了,需要的朋友可以參考下
    2024-01-01
  • Spring Boot 如何使用Liquibase 進(jìn)行數(shù)據(jù)庫遷移(操作方法)

    Spring Boot 如何使用Liquibase 進(jìn)行數(shù)據(jù)庫遷移(操作方法)

    在Spring Boot應(yīng)用程序中使用Liquibase進(jìn)行數(shù)據(jù)庫遷移是一種強(qiáng)大的方式來管理數(shù)據(jù)庫模式的變化,本文重點(diǎn)講解如何在Spring Boot應(yīng)用程序中使用Liquibase進(jìn)行數(shù)據(jù)庫遷移,從而更好地管理數(shù)據(jù)庫模式的變化,感興趣的朋友跟隨小編一起看看吧
    2023-09-09
  • javac、java打jar包命令實(shí)例

    javac、java打jar包命令實(shí)例

    這篇文章主要演示Java中使用命令打jar包的實(shí)例過程,很實(shí)用,希望能給大家做一個(gè)參考。
    2016-06-06
  • 解析Java中的默認(rèn)方法

    解析Java中的默認(rèn)方法

    這篇文章主要介紹了Java中的默認(rèn)方法,包括繼承和調(diào)用等Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-07-07
  • 深入解析Java多態(tài)進(jìn)階學(xué)習(xí)

    深入解析Java多態(tài)進(jìn)階學(xué)習(xí)

    java的動(dòng)態(tài)綁定機(jī)制非常重要。這篇文章將帶大家更深入的學(xué)習(xí)一下Java的多態(tài),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下
    2022-07-07
  • Java基礎(chǔ)之并發(fā)相關(guān)知識(shí)總結(jié)

    Java基礎(chǔ)之并發(fā)相關(guān)知識(shí)總結(jié)

    隨著摩爾定律逐步失效,cpu單核性能達(dá)到瓶頸,并發(fā)逐漸逐漸得到廣泛應(yīng)用,因而學(xué)習(xí)了解以及使用并發(fā)就顯得十分重要,但并發(fā)相關(guān)的知識(shí)比較瑣碎,不易系統(tǒng)學(xué)習(xí),因而本篇文章參照王寶令老師《Java并發(fā)編程》來勾勒出一張“并發(fā)全景圖”,需要的朋友可以參考下
    2021-05-05
  • MybatisPlus中selectPage的使用方法

    MybatisPlus中selectPage的使用方法

    本文主要介紹了MybatisPlus中selectPage的使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • IDEA熱部署配置詳細(xì)教程

    IDEA熱部署配置詳細(xì)教程

    這篇文章主要介紹了IDEA熱部署配置詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10

最新評(píng)論