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

教你使用springboot配置多數(shù)據(jù)源

 更新時(shí)間:2021年05月23日 10:55:48   作者:Jay_Chou12580  
發(fā)現(xiàn)有很多小伙伴還不會(huì)用springboot配置多數(shù)據(jù)源,今天特地給大家整理了本篇文章,文中有非常詳細(xì)的圖文介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴很有幫助,需要的朋友可以參考下

一、建庫建表

1.1 創(chuàng)建數(shù)據(jù)庫db1和數(shù)據(jù)庫db2

在這里插入圖片描述
在這里插入圖片描述

1.2 在數(shù)據(jù)庫db1中創(chuàng)建表db1

在這里插入圖片描述

CREATE TABLE `db1` (
  `id` int unsigned zerofill NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `age` int unsigned zerofill DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;

1.3 在數(shù)據(jù)庫db2中創(chuàng)建表db2

在這里插入圖片描述

CREATE TABLE `db2` (
  `id` int unsigned zerofill NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `age` int unsigned zerofill DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;

二、創(chuàng)建springboot項(xiàng)目

2.1 pom.xml導(dǎo)入依賴

<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.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

2.2 創(chuàng)建application.yml文件(與 2.3 二選一進(jìn)行配置,推薦此方法)

server:
  port: 8080 # 啟動(dòng)端口
spring:
  datasource:
    db1: # 數(shù)據(jù)源1
      jdbc-url: jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver
    db2: # 數(shù)據(jù)源2
      jdbc-url: jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver

2.3 創(chuàng)建application.properties文件(與 2.2 二選一進(jìn)行配置)

server.port=8080

      spring.datasource.db1.url=jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
      spring.datasource.db1.username=root
      spring.datasource.db1.password=root
      spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver

      spring.datasource.db2.url=jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
      spring.datasource.db2.username=root
      spring.datasource.db2.password=root
      spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver

2.4 創(chuàng)建mapper文件

我個(gè)人是放在mapper包下,文件隨便命名的
代碼隨便寫的,測(cè)試而已

在這里插入圖片描述

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;

/**
 * @Author if
 * @Description: What is it
 * @Date 2021-05-20 下午 09:52
 */
@Mapper
public interface Db1Mapper {
    @Insert("insert into db1(name,age) values('if',18)")
    int add();
}
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;

/**
 * @Author if
 * @Description: What is it
 * @Date 2021-05-20 下午 09:52
 */
@Mapper
public interface Db2Mapper {
    @Insert("insert into db2(name,age) values('fi',81)")
    int add();
}

2.5 創(chuàng)建config配置文件

我個(gè)人是放在config包下,文件隨便命名的

在這里插入圖片描述

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;

/**
 * @Author if
 * @Description: 注意以下有些文件路徑需要更改
 * @Date 2021-05-20 下午 09:56
 */
@Configuration
@MapperScan(basePackages = "com.ifyyf.study.mapper.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class Db1DataSourceConfig {
    @Bean("db1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db1") //讀取application.yml中的配置參數(shù)映射成為一個(gè)對(duì)象
    public DataSource getDb1DataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("db1SqlSessionFactory")
    public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // mapper的xml形式文件位置必須要配置,不然將報(bào)錯(cuò):no statement (這種錯(cuò)誤也可能是mapper的xml中,namespace與項(xiàng)目的路徑不一致導(dǎo)致)
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/db1/*.xml"));
        return bean.getObject();
    }

    @Bean("db1SqlSessionTemplate")
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;

/**
 * @Author if
 * @Description: 注意以下有些文件路徑需要更改
 * @Date 2021-05-20 下午 09:56
 */
@Configuration
@MapperScan(basePackages = "com.ifyyf.study.mapper.db2", sqlSessionFactoryRef = "db2SqlSessionFactory")
public class Db2DataSourceConfig {
    @Bean("db2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db2") //讀取application.yml中的配置參數(shù)映射成為一個(gè)對(duì)象
    public DataSource getDb2DataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("db2SqlSessionFactory")
    public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // mapper的xml形式文件位置必須要配置,不然將報(bào)錯(cuò):no statement (這種錯(cuò)誤也可能是mapper的xml中,namespace與項(xiàng)目的路徑不一致導(dǎo)致)
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/db2/*.xml"));
        return bean.getObject();
    }

    @Bean("db2SqlSessionTemplate")
    public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

三、測(cè)試代碼運(yùn)行

3.1 測(cè)試類中測(cè)試代碼

springboot項(xiàng)目中測(cè)試類進(jìn)行測(cè)試

import com.ifyyf.study.mapper.db1.Db1Mapper;
import com.ifyyf.study.mapper.db2.Db2Mapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;

@SpringBootTest
class StudyApplicationTests {
    @Resource
    private Db1Mapper db1Mapper;
    @Resource
    private Db2Mapper db2Mapper;
    @Test
    void contextLoads() {
        System.out.println(db1Mapper.add());
        System.out.println(db2Mapper.add());
    }
}

3.2 運(yùn)行結(jié)果

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

到此這篇關(guān)于教你使用springboot配置多數(shù)據(jù)源的文章就介紹到這了,更多相關(guān)springboot配置多數(shù)據(jù)源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 深入探討Java內(nèi)存區(qū)域

    深入探討Java內(nèi)存區(qū)域

    本篇文章對(duì)Java內(nèi)存區(qū)域的使用進(jìn)行了詳細(xì)的介紹,內(nèi)容很全面,需要的朋友可以參考下
    2015-07-07
  • Java實(shí)現(xiàn)鏈表中元素的獲取、查詢和修改方法詳解

    Java實(shí)現(xiàn)鏈表中元素的獲取、查詢和修改方法詳解

    這篇文章主要介紹了Java實(shí)現(xiàn)鏈表中元素的獲取、查詢和修改方法,結(jié)合實(shí)例形式詳細(xì)分析了Java針對(duì)鏈表中元素的獲取、查詢和修改相關(guān)原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-03-03
  • Java的堵塞隊(duì)列BlockingQueue詳解

    Java的堵塞隊(duì)列BlockingQueue詳解

    這篇文章主要介紹了Java的堵塞隊(duì)列BlockingQueue詳解,阻塞隊(duì)列常用于生產(chǎn)者和消費(fèi)者的場(chǎng)景,生產(chǎn)者是向隊(duì)列里添加元素的線程,消費(fèi)者是從隊(duì)列里取元素的線程,需要的朋友可以參考下
    2023-12-12
  • java中的 toString()方法實(shí)例代碼

    java中的 toString()方法實(shí)例代碼

    toString()方法 相信大家都用到過,一般用于以字符串的形式返回對(duì)象的相關(guān)數(shù)據(jù)。這篇文章主要介紹了java中的 toString()方法,需要的朋友可以參考下
    2017-05-05
  • JAVA中使用MD5加密實(shí)現(xiàn)密碼加密

    JAVA中使用MD5加密實(shí)現(xiàn)密碼加密

    本篇文章主要介紹JAVA中使用MD5加密實(shí)現(xiàn)密碼加密,很多地方都要存儲(chǔ)用戶密碼,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下
    2017-07-07
  • RocketMq深入分析講解兩種削峰方式

    RocketMq深入分析講解兩種削峰方式

    當(dāng)上游調(diào)用下游服務(wù)速率高于下游服務(wù)接口QPS時(shí),那么如果不對(duì)調(diào)用速率進(jìn)行控制,那么會(huì)發(fā)生很多失敗請(qǐng)求,通過消息隊(duì)列的削峰方法有兩種,這篇文章主要介紹了RocketMq深入分析講解兩種削峰方式
    2023-01-01
  • 高并發(fā)下如何避免重復(fù)數(shù)據(jù)產(chǎn)生技巧

    高并發(fā)下如何避免重復(fù)數(shù)據(jù)產(chǎn)生技巧

    這篇文章主要為大家介紹了高并發(fā)下如何避免重復(fù)數(shù)據(jù)的產(chǎn)生技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • java 遍歷Map及Map轉(zhuǎn)化為二維數(shù)組的實(shí)例

    java 遍歷Map及Map轉(zhuǎn)化為二維數(shù)組的實(shí)例

    這篇文章主要介紹了java 遍歷Map及Map轉(zhuǎn)化為二維數(shù)組的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-08-08
  • Java?@Validated遇到的大坑與處理

    Java?@Validated遇到的大坑與處理

    這篇文章主要介紹了Java?@Validated遇到的大坑與處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java開發(fā)HashMap?key必須實(shí)現(xiàn)hashCode?equals方法原理

    Java開發(fā)HashMap?key必須實(shí)現(xiàn)hashCode?equals方法原理

    這篇文章主要為大家介紹了Java開發(fā)HashMap?key必須實(shí)現(xiàn)hashCode?equals方法原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03

最新評(píng)論