springboot中mybatis多數(shù)據(jù)源的示例分析-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“spring boot中mybatis多數(shù)據(jù)源的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學習一下“spring boot中mybatis多數(shù)據(jù)源的示例分析”這篇文章吧。

成都創(chuàng)新互聯(lián)主要業(yè)務(wù)有網(wǎng)站營銷策劃、成都網(wǎng)站設(shè)計、成都做網(wǎng)站、微信公眾號開發(fā)、成都微信小程序H5響應(yīng)式網(wǎng)站、程序開發(fā)等業(yè)務(wù)。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當客戶,還把客戶視為我們的合作伙伴,在開展業(yè)務(wù)的過程中,公司還積累了豐富的行業(yè)經(jīng)驗、成都全網(wǎng)營銷推廣資源和合作伙伴關(guān)系資源,并逐漸建立起規(guī)范的客戶服務(wù)和保障體系。 

在我們的項目中不免會遇到需要在一個項目中使用多個數(shù)據(jù)源的問題,像我在得到一個任務(wù)將用戶的聊天記錄進行遷移的時候,就是用到了三個數(shù)據(jù)源,當時使用的AOP的編程方式根據(jù)訪問的方法的不同進行動態(tài)的切換數(shù)據(jù)源,覺得性能不太好,先在又新用到了一種使用方式,覺得不錯,記錄下來。

介紹一下DEMO項目,使用的spring boot集成mybatis,mybatis查詢數(shù)據(jù)庫是基于注解形式查詢的,目的查詢兩個數(shù)據(jù)庫test1和test2的用戶信息,并在控制臺打印。

1.pom文件

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.1</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.1.10</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
      <version>5.1.27</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>
            **/*.xml
          </include>
        </includes>
      </resource>
      <resource>
        <directory>src/resources</directory>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

其中添加了alibaba的druid的數(shù)據(jù)源代替了spring boot默認的hikacp,注意mysql的驅(qū)動的版本號要與自己所使用的mysql的版本號保持一致,其中build模塊里面的resources里面是為了防止spring boot 過濾掉src/main/java的XML文件,畢竟有的人喜歡mybatsi查詢數(shù)據(jù)庫的時候使用的是XML映射文件,不過我們本次使用的 是注解的形式 ,所以<resources>里面的內(nèi)容在項目中沒有用到。如果直接使用注解,可以忽略該內(nèi)容。

2.用戶類

public class User {
  public Integer id;
  public String name;
  public String address;

  @Override
  public String toString() {
    return "User{" +
        "id=" + id +
        ", name='" + name + '\'' +
        ", address='" + address + '\'' +
        '}';
  }
    //get set方法省略...........
}

用戶類沒有什么好說的,就是基本的幾個屬性。

3.application.properties文件配置

spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.username=root
spring.datasource.one.password=123456
spring.datasource.one.url=jdbc:mysql://localhost:3306/test1

spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.username=root
spring.datasource.two.password=123456
spring.datasource.two.url=jdbc:mysql://localhost:3306/test2

這里主要配置了兩個數(shù)據(jù)庫的訪問屬性,注意兩個的區(qū)別為one何two的前綴不同,方便在下面使用spring boot的類安全屬性的方式創(chuàng)建不同的數(shù)據(jù)源。

4.根據(jù)不同的前綴創(chuàng)建不同的數(shù)據(jù)源

@Configuration
public class DataSourceConfig {
  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.one")
  public DataSource dsOne(){
    return DruidDataSourceBuilder.create().build();
  }
  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.two")
  public DataSource dsTwo(){
    return DruidDataSourceBuilder.create().build();
  }
}

該類位于主目錄的config目錄下面,使用@ConfigurationProperties注解表明對應(yīng)的DataSource創(chuàng)建的時候使用的配置文件的內(nèi)容。

5.在主目錄下面分別創(chuàng)建mapper1和mapper2包,在對應(yīng)的包下面創(chuàng)建對應(yīng)的數(shù)據(jù)層訪問接口UserMapper1和UserMapper2,內(nèi)容都如下所示

@Mapper
public interface UserMapper1 {
  @Select("select * from users")
  List<User> getAllUser();
}

這接口里面沒有什么好說的就是一個簡單的mytatis的基于注解的查詢所有用戶的接口。

6.在config包下面創(chuàng)建不同的配置類MybatisConfigOne和MybatisConfigTwo兩個類分別對應(yīng)去掃描mapper1和mapper2兩個路徑下面的dao層接口。

@Configuration
@MapperScan(basePackages = "com.hopec.mybatis.mapper1",sqlSessionFactoryRef = "sqlSessionFactory1",
    sqlSessionTemplateRef = "sqlSessionTemplate1")
public class MybatisConfigOne {
  @Autowired
  @Qualifier("dsOne")
  DataSource ds1;
@Bean
  SqlSessionFactory sqlSessionFactory1(){
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(ds1);
    try {
      return bean.getObject();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  @Bean
  SqlSessionTemplate sqlSessionTemplate1(){
    return new SqlSessionTemplate(sqlSessionFactory1());
  }
}
public class MybatisConfigTwo {
  @Autowired
  @Qualifier("dsTwo")
  DataSource ds2;
@Bean
  SqlSessionFactory sqlSessionFactory2(){
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(ds2);
    try {
      return bean.getObject();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  @Bean
  SqlSessionTemplate sqlSessionTemplate2(){
    return new SqlSessionTemplate(sqlSessionFactory2());
  }
}

7.測試

@SpringBootTest
class MybatisApplicationTests {
  @Autowired
  UserMapper1 userMapper1;
  @Autowired
  UserMapper2 userMapper2;
  @Test
  void contextLoads() {
    List<User> users = userMapper1.getAllUser();
    System.out.println(users);
    List<User> allUser = userMapper2.getAllUser();
    System.out.println(allUser);

  }
}

測試結(jié)果:

spring boot中mybatis多數(shù)據(jù)源的示例分析

以上是“spring boot中mybatis多數(shù)據(jù)源的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站bm7419.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當前文章:springboot中mybatis多數(shù)據(jù)源的示例分析-創(chuàng)新互聯(lián)
標題路徑:http://bm7419.com/article16/dpcedg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、關(guān)鍵詞優(yōu)化Google、全網(wǎng)營銷推廣、服務(wù)器托管品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營