亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring boot mybatis多數(shù)據(jù)源解決方案過程解析

 更新時間:2019年11月01日 08:26:06   作者:全me村的希望  
這篇文章主要介紹了spring boot mybatis多數(shù)據(jù)源解決方案過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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

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

如果想使用多個數(shù)據(jù)源,就繼續(xù)增加就可以了,ok,大功告成了!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 深入淺析Spring 的aop實(shí)現(xiàn)原理

    深入淺析Spring 的aop實(shí)現(xiàn)原理

    AOP(Aspect-OrientedProgramming,面向方面編程),可以說是OOP(Object-Oriented Programing,面向?qū)ο缶幊蹋┑难a(bǔ)充和完善。本文給大家介紹Spring 的aop實(shí)現(xiàn)原理,感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • Mybatis框架搭建與簡單查詢詳解

    Mybatis框架搭建與簡單查詢詳解

    本文主要介紹了Mybatis框架搭建與簡單查詢的相關(guān)知識,具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • 一文詳解如何查看jdk版本及安裝路徑

    一文詳解如何查看jdk版本及安裝路徑

    這篇文章主要給大家介紹了關(guān)于如何查看jdk版本及安裝路徑的相關(guān)資料,JDK是Java語言的軟件開發(fā)工具包,主要用于移動設(shè)備、嵌入式設(shè)備上的java應(yīng)用程序,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • 使用迭代器模式來進(jìn)行Java的設(shè)計(jì)模式編程

    使用迭代器模式來進(jìn)行Java的設(shè)計(jì)模式編程

    這篇文章主要介紹了使用迭代器模式來進(jìn)行Java的設(shè)計(jì)模式編程,文中對迭代器模式中的容器封裝方面的知識進(jìn)行了講解,需要的朋友可以參考下
    2016-02-02
  • Java動態(tài)代理的示例詳解

    Java動態(tài)代理的示例詳解

    動態(tài)代理指的是,代理類和目標(biāo)類的關(guān)系在程序運(yùn)行的時候確定的,客戶通過代理類來調(diào)用目標(biāo)對象的方法,是在程序運(yùn)行時根據(jù)需要動態(tài)的創(chuàng)建目標(biāo)類的代理對象。本文將通過案例詳細(xì)講解一下動態(tài)代理,需要的可以參考一下
    2022-02-02
  • Java中的動態(tài)代理使用

    Java中的動態(tài)代理使用

    這篇文章主要介紹了Java中的動態(tài)代理使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java日期時間與正則表達(dá)式超詳細(xì)整理(適合新手入門)

    Java日期時間與正則表達(dá)式超詳細(xì)整理(適合新手入門)

    如果使用得當(dāng),正則表達(dá)式是匹配各種模式的強(qiáng)大工具,下面這篇文章主要給大家介紹了關(guān)于Java日期時間與正則表達(dá)式超詳細(xì)整理的相關(guān)資料,本文非常適合新手入門,需要的朋友可以參考下
    2023-04-04
  • SpringMVC高級開發(fā)功能實(shí)現(xiàn)過程解析

    SpringMVC高級開發(fā)功能實(shí)現(xiàn)過程解析

    這篇文章主要介紹了SpringMVC高級開發(fā)功能實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • hibernate一對多關(guān)聯(lián)映射學(xué)習(xí)小結(jié)

    hibernate一對多關(guān)聯(lián)映射學(xué)習(xí)小結(jié)

    這篇文章主要介紹了hibernate一對多關(guān)聯(lián)映射學(xué)習(xí)小結(jié),需要的朋友可以參考下
    2017-09-09
  • 如何通過jstack命令dump線程信息

    如何通過jstack命令dump線程信息

    這篇文章主要介紹了如何通過jstack命令dump線程信息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07

最新評論