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

使用SpringBoot配置多數(shù)據(jù)源的經(jīng)驗(yàn)分享

 更新時(shí)間:2022年04月11日 10:23:07   作者:碼拉松  
這篇文章主要介紹了使用SpringBoot配置多數(shù)據(jù)源的經(jīng)驗(yàn)分享,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1. 引入jar包

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.multi.datasource</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <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>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.8</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.22</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2. properties配置

分別準(zhǔn)備兩個(gè)數(shù)據(jù)源

server.port=18888
mybatis.mapper-locations=classpath:mapper/*.xml

my1.datasource.url=jdbc:mysql://10.0.0.125:3306/wyl?autoReconnect=true
my1.datasource.driverClassName=com.mysql.cj.jdbc.Driver
my1.datasource.username=root
my1.datasource.password=123456

my2.datasource.url=jdbc:mysql://10.0.0.160:3306/wyl?autoReconnect=true
my2.datasource.driverClassName=com.mysql.cj.jdbc.Driver
my2.datasource.username=root
my2.datasource.password=123456

3. 分別配置兩個(gè)數(shù)據(jù)源

第一個(gè)數(shù)據(jù)源

package com.multi.datasource.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = My1DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "my1SqlSessionFactory")
public class My1DataSourceConfig {

    static final String PACKAGE = "com.multi.datasource.dao.my1";
    static final String MAPPER_LOCATION = "classpath:mapper/*.xml";

    @Value("${my1.datasource.url}")
    private String url;

    @Value("${my1.datasource.username}")
    private String user;

    @Value("${my1.datasource.password}")
    private String password;

    @Value("${my1.datasource.driverClassName}")
    private String driverClass;

    @Bean(name = "my1DataSource")
    public DataSource my1DataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        dataSource.setMaxWait(Integer.MAX_VALUE);
        dataSource.setTestOnBorrow(true);
        dataSource.setTestOnReturn(true);
        dataSource.setTestWhileIdle(true);
        return dataSource;
    }

    @Bean(name = "my1TransactionManager")
    public DataSourceTransactionManager my1TransactionManager() {
        return new DataSourceTransactionManager(my1DataSource());
    }

    @Bean(name = "my1SqlSessionFactory")
    public SqlSessionFactory my1SqlSessionFactory(@Qualifier("my1DataSource") DataSource my1DataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(my1DataSource);
        sessionFactory.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources(My1DataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}

第二個(gè)數(shù)據(jù)源

package com.multi.datasource.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = My1DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "my1SqlSessionFactory")
public class My1DataSourceConfig {

    static final String PACKAGE = "com.multi.datasource.dao.my1";
    static final String MAPPER_LOCATION = "classpath:mapper/*.xml";

    @Value("${my1.datasource.url}")
    private String url;

    @Value("${my1.datasource.username}")
    private String user;

    @Value("${my1.datasource.password}")
    private String password;

    @Value("${my1.datasource.driverClassName}")
    private String driverClass;

    @Bean(name = "my1DataSource")
    public DataSource my1DataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        dataSource.setMaxWait(Integer.MAX_VALUE);
        dataSource.setTestOnBorrow(true);
        dataSource.setTestOnReturn(true);
        dataSource.setTestWhileIdle(true);
        return dataSource;
    }

    @Bean(name = "my1TransactionManager")
    public DataSourceTransactionManager my1TransactionManager() {
        return new DataSourceTransactionManager(my1DataSource());
    }

    @Bean(name = "my1SqlSessionFactory")
    public SqlSessionFactory my1SqlSessionFactory(@Qualifier("my1DataSource") DataSource my1DataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(my1DataSource);
        sessionFactory.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources(My1DataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}

4. Dao目錄

為了區(qū)分兩個(gè)數(shù)據(jù)源,分別設(shè)置了不同的目錄

package com.multi.datasource.dao.my1;

import com.multi.datasource.entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface Test1Mapper {
    
    UserEntity query();
    
}
package com.multi.datasource.dao.my2;

import com.multi.datasource.entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface Test2Mapper {
    
    UserEntity query();
    
}

5. Entity

package com.multi.datasource.entity;

import lombok.Data;

@Data
public class UserEntity {

    private String userName;

}

6. Mapper文件

從my1數(shù)據(jù)源查詢

<?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.multi.datasource.dao.my1.Test1Mapper">

<select id="query" resultType="com.multi.datasource.entity.UserEntity">
        select user_name as userName from t_user
    </select>

</mapper>

從my2數(shù)據(jù)源查詢

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.multi.datasource.dao.my2.Test2Mapper">
    
    <select id="query" resultType="com.multi.datasource.entity.UserEntity">
        select user_name as userName from t_user
    </select>
    
</mapper>

7. Controller測(cè)試

package com.multi.datasource.controller;

import com.multi.datasource.dao.my1.Test1Mapper;
import com.multi.datasource.dao.my2.Test2Mapper;
import com.multi.datasource.entity.UserEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class TestController {

    @Resource
    private Test1Mapper test1Mapper;

    @Resource
    private Test2Mapper test2Mapper;

    @RequestMapping("query")
    public void query() {
        UserEntity user1 = test1Mapper.query();
        System.out.println("my1 dataSource:" + user1);


        UserEntity user2 = test2Mapper.query();
        System.out.println("my2 dataSource:" + user2);
    }

}

兩個(gè)數(shù)據(jù)源,對(duì)應(yīng)的user_name分別是zhangsan和lisi

在這里插入圖片描述

在這里插入圖片描述

8. 結(jié)果驗(yàn)證

訪問 http://localhost:18888/query,結(jié)果如下

在這里插入圖片描述

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

相關(guān)文章

  • Idea 快速生成方法返回值的操作

    Idea 快速生成方法返回值的操作

    這篇文章主要介紹了Idea 快速生成方法返回值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • SpringBatch數(shù)據(jù)讀取的實(shí)現(xiàn)(ItemReader與自定義讀取邏輯)

    SpringBatch數(shù)據(jù)讀取的實(shí)現(xiàn)(ItemReader與自定義讀取邏輯)

    本文主要介紹了SpringBatch數(shù)據(jù)讀取的實(shí)現(xiàn), 文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-04-04
  • 多個(gè)JDK版本(Java 8、Java 17、Java 21)下載和切換

    多個(gè)JDK版本(Java 8、Java 17、Java 21)下載和切換

    為了在實(shí)際中可以任意選擇所需的JDK版本,需要將多個(gè)JDK版本進(jìn)行切換,本文主要介紹了多個(gè)JDK版本(Java 8、Java 17、Java 21)下載和切換,感興趣的可以了解一下
    2025-04-04
  • 新手場(chǎng)景Java線程相關(guān)問題及解決方案

    新手場(chǎng)景Java線程相關(guān)問題及解決方案

    這篇文章主要介紹了新手場(chǎng)景Java線程相關(guān)問題及解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • MybatisPlus實(shí)現(xiàn)insertBatchSomeColumn進(jìn)行批量增加

    MybatisPlus實(shí)現(xiàn)insertBatchSomeColumn進(jìn)行批量增加

    本文主要介紹了MybatisPlus實(shí)現(xiàn)insertBatchSomeColumn進(jìn)行批量增加,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • java后端請(qǐng)求過濾options方式

    java后端請(qǐng)求過濾options方式

    Optional項(xiàng)是一個(gè)容器對(duì)象,它可以包含非空值,也可以不包含非空值,它用于表示沒有值,而不是使用?null,引入Optional項(xiàng)是為了幫助開發(fā)人員編寫更簡(jiǎn)潔、更具表現(xiàn)力的代碼,并避免?NullPointerException
    2024-01-01
  • 新手初學(xué)Java基礎(chǔ)

    新手初學(xué)Java基礎(chǔ)

    這篇文章主要介紹了java基礎(chǔ)之方法詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-07-07
  • Mybatis一級(jí)緩存和結(jié)合Spring Framework后失效的源碼探究

    Mybatis一級(jí)緩存和結(jié)合Spring Framework后失效的源碼探究

    這篇文章主要介紹了Mybatis一級(jí)緩存和結(jié)合Spring Framework后失效的源碼探究,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • Java程序圖形用戶界面設(shè)計(jì)之按鈕與布局

    Java程序圖形用戶界面設(shè)計(jì)之按鈕與布局

    圖形界面(簡(jiǎn)稱GUI)是指采用圖形方式顯示的計(jì)算機(jī)操作用戶界面。與早期計(jì)算機(jī)使用的命令行界面相比,圖形界面對(duì)于用戶來說在視覺上更易于接受,本篇精講Java語言中關(guān)于圖形用戶界面的按鈕和布局部分
    2022-02-02
  • JAVA參數(shù)傳遞方式實(shí)例淺析【按值傳遞與引用傳遞區(qū)別】

    JAVA參數(shù)傳遞方式實(shí)例淺析【按值傳遞與引用傳遞區(qū)別】

    這篇文章主要介紹了JAVA參數(shù)傳遞方式,結(jié)合實(shí)例形式分析了java按值傳遞與引用傳遞區(qū)別及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2020-05-05

最新評(píng)論

龙川县| 建水县| 永泰县| 乐山市| 灵台县| 上犹县| 康乐县| 社会| 遂宁市| 宁化县| 时尚| 临泽县| 贡觉县| 盖州市| 万全县| 南京市| 顺平县| 泰顺县| 德钦县| 科尔| 本溪市| 沁源县| 临沭县| 堆龙德庆县| 岗巴县| 万宁市| 新竹市| 苗栗市| 潼关县| 云梦县| 浦县| 内丘县| 澎湖县| 辽中县| 郓城县| 独山县| 孟州市| 南汇区| 孟州市| 博野县| 娄烦县|