最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年05月23日 10:55:48   作者:Jay_Chou12580  
發(fā)現(xiàn)有很多小伙伴還不會用springboot配置多數(shù)據(jù)源,今天特地給大家整理了本篇文章,文中有非常詳細(xì)的圖文介紹及代碼示例,對正在學(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項目

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 # 啟動端口
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文件

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

在這里插入圖片描述

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配置文件

我個人是放在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ù)映射成為一個對象
    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形式文件位置必須要配置,不然將報錯:no statement (這種錯誤也可能是mapper的xml中,namespace與項目的路徑不一致導(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ù)映射成為一個對象
    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形式文件位置必須要配置,不然將報錯:no statement (這種錯誤也可能是mapper的xml中,namespace與項目的路徑不一致導(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);
    }
}

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

3.1 測試類中測試代碼

springboot項目中測試類進(jìn)行測試

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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Spring簡單容器中的Bean基本加載過程

    詳解Spring簡單容器中的Bean基本加載過程

    本篇將對定義在 XMl 文件中的 bean,從靜態(tài)的的定義到變成可以使用的對象的過程,即 bean 的加載和獲取的過程進(jìn)行一個整體的了解
    2017-05-05
  • redis redisson 集合的使用案例(RList、Rset、RMap)

    redis redisson 集合的使用案例(RList、Rset、RMap)

    這篇文章主要介紹了redis redisson 集合的使用案例(RList、Rset、RMap),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java的作業(yè)調(diào)度類庫Quartz基本使用指南

    Java的作業(yè)調(diào)度類庫Quartz基本使用指南

    這篇文章主要介紹了Java的作業(yè)調(diào)度類庫Quartz基本使用指南,Quartz能夠讓類按照指定的計劃順序執(zhí)行,需要的朋友可以參考下
    2016-03-03
  • 利用ssh實現(xiàn)服務(wù)器文件上傳下載

    利用ssh實現(xiàn)服務(wù)器文件上傳下載

    這篇文章主要為大家詳細(xì)介紹了如何利用ssh實現(xiàn)服務(wù)器文件上傳下載,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • springboot定時任務(wù)不起作用問題及解決

    springboot定時任務(wù)不起作用問題及解決

    文章主要介紹了Spring Boot中延遲加載bean的概念,并討論了如何解決定時任務(wù)不執(zhí)行的問題,通過設(shè)置`@Lazy(false)`注解,可以指定某些類不使用延遲加載,從而解決定時任務(wù)無法執(zhí)行的問題
    2024-11-11
  • 如何通過Java代碼實現(xiàn)KMP算法

    如何通過Java代碼實現(xiàn)KMP算法

    這篇文章主要介紹了如何通過Java代碼實現(xiàn)KMP算法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • SpringBoot基于Disruptor實現(xiàn)高效的消息隊列?

    SpringBoot基于Disruptor實現(xiàn)高效的消息隊列?

    Disruptor是一個開源的Java框架,它被設(shè)計用于在生產(chǎn)者-消費(fèi)者問題上獲得盡量高的吞吐量和盡量低的延遲,本文主要介紹了SpringBoot基于Disruptor實現(xiàn)高效的消息隊列?,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • 解決若依pageHelper在動態(tài)切換數(shù)據(jù)源問題

    解決若依pageHelper在動態(tài)切換數(shù)據(jù)源問題

    這篇文章主要介紹了解決pageHelper在動態(tài)切換數(shù)據(jù)源問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java之Error與Exception的區(qū)別案例詳解

    Java之Error與Exception的區(qū)別案例詳解

    這篇文章主要介紹了Java之Error與Exception的區(qū)別案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Java中創(chuàng)建線程的四種方法解析

    Java中創(chuàng)建線程的四種方法解析

    這篇文章主要介紹了Java中創(chuàng)建線程的四種方法解析,線程是Java編程語言中的一個重要概念,它允許程序在同一時間執(zhí)行多個任務(wù),線程是程序中的執(zhí)行路徑,可以同時執(zhí)行多個線程,每個線程都有自己的執(zhí)行流程,需要的朋友可以參考下
    2023-10-10

最新評論

屯昌县| 镇赉县| 凤山市| 凤山市| 宿松县| 西青区| 正定县| 卢龙县| 麻阳| 临桂县| 句容市| 淮南市| 西平县| 自治县| 高安市| 木兰县| 衡阳市| 抚州市| 双牌县| 邵东县| 渝北区| 天等县| 博乐市| 东光县| 随州市| 黄石市| 原平市| 尚义县| 弥渡县| 巴彦县| 台中市| 泊头市| 龙门县| 莲花县| 正安县| 兴隆县| 全州县| 股票| 万安县| 屯昌县| 民和|