springboot mybatis 多数据源整合

1、在application.properties中配置两个数据库:

# db01 database
spring.datasource.db01.jdbc-url=jdbc:oracle:thin:@127.0.0.1:1521/orcl
spring.datasource.db01.username=lxw
spring.datasource.db01.password=lxw@123
spring.datasource.db01.driverClassName=oracle.jdbc.driver.OracleDriver

# db02 database
spring.datasource.db02.jdbc-url=jdbc:oracle:thin:@127.0.0.1:1521/orcl
spring.datasource.db02.username=lxw02
spring.datasource.db02.password=lxw02@123
spring.datasource.db02.driverClassName=oracle.jdbc.driver.OracleDriver

2、建立连个数据源的配置文件:

第一个配置文件:

package com.lxw.lxwDemo.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.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

//表示这个类为一个配置类
@SpringBootConfiguration
//配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.lxw.lxwDemo.mapper.db01", sqlSessionFactoryRef = "db01SqlSessionFactory")
public class db01DataSourceConfig {

    //将这个对象放入Spring容器中
    @Bean(name = "db01DataSource")
    //表示这个数据源是默认数据源
    @Primary
    //读取application.properties中的配置参数映射成为一个对象
    //prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.db01")
    public DataSource getDateSource1() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "db01SqlSessionFactory")
    //表示这个数据源是默认数据源
    @Primary
    //@Qualifier表示查找Spring容器中名字为test1DataSource的对象
    public SqlSessionFactory db01SqlSessionFactory(@Qualifier("db01DataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                //设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db01/*Mapper.xml"));
        return bean.getObject();
    }

    @Bean("db01SqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate db01sqlsessiontemplate(
            @Qualifier("db01SqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }


}

第二个配置文件:

package com.lxw.lxwDemo.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.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

//表示这个类为一个配置类
@SpringBootConfiguration
//配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.lxw.lxwDemo.mapper.db02", sqlSessionFactoryRef = "db02SqlSessionFactory")
public class db02DataSourceConfig {

    @Bean(name = "db02DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db02")
    public DataSource getDateSource2() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "db02SqlSessionFactory")
    public SqlSessionFactory db02SqlSessionFactory(@Qualifier("db02DataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db02/*Mapper.xml"));
        return bean.getObject();
    }

    @Bean("db02SqlSessionTemplate")
    public SqlSessionTemplate db02sqlsessiontemplate(
            @Qualifier("db02SqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }

}

项目结构

 

posted @   天天代码码天天  阅读(5)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示