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

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

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

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

介紹一下DEMO項(xiàng)目,使用的spring boot集成mybatis,mybatis查詢(xún)數(shù)據(jù)庫(kù)是基于注解形式查詢(xún)的,目的查詢(xún)兩個(gè)數(shù)據(jù)庫(kù)test1和test2的用戶(hù)信息,并在控制臺(tái)打印。

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

2.用戶(hù)類(lèi)

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方法省略...........
}

用戶(hù)類(lèi)沒(méi)有什么好說(shuō)的,就是基本的幾個(gè)屬性。

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

這里主要配置了兩個(gè)數(shù)據(jù)庫(kù)的訪問(wèn)屬性,注意兩個(gè)的區(qū)別為one何two的前綴不同,方便在下面使用spring boot的類(lèi)安全屬性的方式創(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();
  }
}

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

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

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

這接口里面沒(méi)有什么好說(shuō)的就是一個(gè)簡(jiǎn)單的mytatis的基于注解的查詢(xún)所有用戶(hù)的接口。

6.在config包下面創(chuàng)建不同的配置類(lèi)MybatisConfigOne和MybatisConfigTwo兩個(gè)類(lèi)分別對(duì)應(yīng)去掃描mapper1和mapper2兩個(gè)路徑下面的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.測(cè)試

@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);

  }
}

測(cè)試結(jié)果:

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

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

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

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

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

廣告

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

網(wǎng)站托管運(yùn)營(yíng)