欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

037.[转] springboot 配置多个数据源

程序员文章站 2022-04-30 22:40:51
1、在application.properties文件 配置两个数据源 2、创建一个datasource包,新建DataSource1,DataSource2两个文件,通过注解来配置数据源 DataSource1 DataSource2: 3、测试 Service&Mapper https://ww ......

 

037.[转] springboot 配置多个数据源

 

1、在application.properties文件 配置两个数据源

#默认使用 tomcat-jdbc
spring.datasource.type=org.apache.tomcat.jdbc.pool.datasource

spring.datasource.data1.url=jdbc:mysql://127.0.0.1:3306/mysql-boot
spring.datasource.data1.username=root
spring.datasource.data1.password=123123
spring.datasource.data1.driver-class-name=com.mysql.jdbc.driver

spring.datasource.data2.url=jdbc:mysql://127.0.0.1:3306/mysql-confswh
spring.datasource.data2.username=root
spring.datasource.data2.password=123123
spring.datasource.data2.driver-class-name=com.mysql.jdbc.driver

 

 2、创建一个datasource包,新建datasource1,datasource2两个文件,通过注解来配置数据源

 datasource1

package com.springboot.datasource;

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.autoconfigure.jdbc.datasourcebuilder;
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.primary;
import org.springframework.jdbc.datasource.datasourcetransactionmanager;

import javax.sql.datasource;

@configuration//注解到spring容器中
@mapperscan(basepackages = "com.springboot.data1.mapper",sqlsessionfactoryref = "data1sqlsessionfactory")
public class datasource1 {

    /**
     * 返回data1数据库的数据源
     * @return
     */
    @bean(name="data1source")
    @primary//主数据源
    @configurationproperties(prefix = "spring.datasource.data1")
    public datasource datasource(){
        return datasourcebuilder.create().build();
    }

    /**
     * 返回data1数据库的会话工厂
     * @param ds
     * @return
     * @throws exception
     */
    @bean(name = "data1sqlsessionfactory")
    @primary
    public sqlsessionfactory sqlsessionfactory(@qualifier("data1source") datasource ds) throws exception{
        sqlsessionfactorybean bean = new sqlsessionfactorybean();
        bean.setdatasource(ds);
        return bean.getobject();
    }

    /**
     * 返回data1数据库的会话模板
     * @param sessionfactory
     * @return
     * @throws exception
     */
    @bean(name = "data1sqlsessiontemplate")
    @primary
    public sqlsessiontemplate sqlsessiontemplate(@qualifier("data1sqlsessionfactory") sqlsessionfactory sessionfactory) throws  exception{
        return  new sqlsessiontemplate(sessionfactory);
    }

    /**
     * 返回data1数据库的事务
     * @param ds
     * @return
     */
    @bean(name = "data1transactionmanager")
    @primary
    public datasourcetransactionmanager transactionmanager(@qualifier("data1source") datasource ds){
        return new datasourcetransactionmanager(ds);
    }
}

 

datasource2:

package com.springboot.datasource;

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.autoconfigure.jdbc.datasourcebuilder;
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.jdbc.datasource.datasourcetransactionmanager;

import javax.sql.datasource;

@configuration//注解到spring容器中
@mapperscan(basepackages = "com.springboot.data2.mapper",sqlsessionfactoryref = "data2sqlsessionfactory")
public class datasource2 {

    /**
     * 返回data2数据库的数据源
     * @return
     */
    @bean(name="data2source")
    @configurationproperties(prefix = "spring.datasource.data2")
    public datasource datasource(){
        return datasourcebuilder.create().build();
    }

    /**
     * 返回data2数据库的会话工厂
     * @param ds
     * @return
     * @throws exception
     */
    @bean(name = "data2sqlsessionfactory")
    public sqlsessionfactory sqlsessionfactory(@qualifier("data2source") datasource ds) throws exception{
        sqlsessionfactorybean bean = new sqlsessionfactorybean();
        bean.setdatasource(ds);
        return bean.getobject();
    }

    /**
     * 返回data2数据库的会话模板
     * @param sessionfactory
     * @return
     * @throws exception
     */
    @bean(name = "data2sqlsessiontemplate")
    public sqlsessiontemplate sqlsessiontemplate(@qualifier("data2sqlsessionfactory") sqlsessionfactory sessionfactory) throws  exception{
        return  new sqlsessiontemplate(sessionfactory);
    }

    /**
     * 返回data2数据库的事务
     * @param ds
     * @return
     */
    @bean(name = "data2transactionmanager")
    public datasourcetransactionmanager transactionmanager(@qualifier("data2source") datasource ds){
        return new datasourcetransactionmanager(ds);
    }
}

 

3、测试 service&mapper

    @requestmapping("gettopiclist")
    public list<map<string,object>> gettopiclist(){
        list<map<string,object>> list1 = userservicedata1.gettopiclistdata1();
        list<map<string,object>> list2 = userservicedata2.gettopiclistdata2();
        return list1;
    }

 

 

 

附: