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);
} }

项目结构

springboot mybatis 多数据源整合的更多相关文章

  1. spring-boot (四) springboot+mybatis多数据源最简解决方案

    学习文章来自:http://www.ityouknow.com/spring-boot.html 配置文件 pom包就不贴了比较简单该依赖的就依赖,主要是数据库这边的配置: mybatis.confi ...

  2. springboot + mybatis + 多数据源

    此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1) ...

  3. 第九章 springboot + mybatis + 多数据源 (AOP实现)

    在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...

  4. 【第九章】 springboot + mybatis + 多数据源 (AOP实现)

    在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...

  5. springboot+mybatis多数据源的事务问题

    1.springboot+mybatis实现多数据源后,针对单个数据源我们可以使用@Transactional(name="xxxTransactionManager") 来指定使 ...

  6. springboot mybatis 多数据源配置

    首先导入mybatis等包,这里就不多说. 下面是配置多数据源和mybatis,每个数据源对应一套mybatis模板 数据源1: package com.aaaaaaa.config.datasour ...

  7. 第八章 springboot + mybatis + 多数据源(转载)

    本篇博客转发自:http://www.cnblogs.com/java-zhao/p/5413845.html 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构 ...

  8. 【第八章】 springboot + mybatis + 多数据源

    在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1)DatabaseType列出所有的数据源的key---key 2)DatabaseConte ...

  9. 第八章 springboot + mybatis + 多数据源2(解决循环引用)

    解决了循环引用 1.application.properties #the first datasource jdbc.names:1,2 jdbc1.driverClassName = com.my ...

  10. spring boot(七):springboot+mybatis多数据源最简解决方案

    说起多数据源,一般都来解决那些问题呢,主从模式或者业务比较复杂需要连接不同的分库来支持业务.我们项目是后者的模式,网上找了很多,大都是根据jpa来做多数据源解决方案,要不就是老的spring多数据源解 ...

随机推荐

  1. win32 - DIB 与 DDB

    设备相关位图(DDB): DDB不包含颜色值,因为每个设备可以具有自己的一组颜色,所以为一个设备创建的DDB可能无法在其他设备上很好地显示. DDB通常被称为兼容位图,并且它通常比DIB具有更好的GD ...

  2. win32 - ListView_GetItemPosition的使用

    ListView_GetItemPosition : Gets the position of a list-view item 理论上获得桌面图标的正确方法是使用shell项,=> IFold ...

  3. 关于谷歌浏览器出现“错误代码:net::ERR_UNSAFE_PORT”的解决办法

    搭建项目时需要自己配置端口信息,但是有人搭建之后会出现如下情况 但是换用edge等浏览器没有问题,这是因为chorme浏览器有自己的默认非安全端口, 若访问这些端口就会出现这个错误,并且所有采用cho ...

  4. Java JVM——7.本地方法栈

    本地方法栈 Java虚拟机栈于管理Java方法的调用,而本地方法栈用于管理本地方法的调用. 本地方法栈,也是线程私有的. 允许被实现成固定或者是可动态扩展的内存大小.(在内存溢出方面是相同的) ✎ 如 ...

  5. postgresql表结构查询sql

    数据库表结构查询sql SELECT t1.attnum as "序号", t1.attname as "字段名", concat_ws ( '', t2.ty ...

  6. 【Azure 应用服务】使用Python Azure SDK 来获取 App Service的访问限制信息(Access Restrictions)

    问题描述 为Azure App Service添加访问限制,需要Python Azure SDK来实现的示例代码. 问题解答 查阅Azure App Service的官方资料,使用Python SDK ...

  7. 【Azure 应用服务】azure function powershell 调用 New-AzADServicePrincipal -DisplayName $sp -PasswordCredential $spCred

    问题描述 powershell 调用New-AzADServicePrincipal -DisplayName $sp -PasswordCredential $spCred,出现如下错误: Reso ...

  8. linux服务器文件实时同步

    linux服务器文件实时同步 1 背景说明 在做系统集群部署时,涉及到两个或多个服务器之间文件同步.在软件层面linux服务环境找到以下两种同步方式 利用linux NFS功能将网络共享文件挂载成本地 ...

  9. spring注解版 图文教程

    注解方式,需要配置contextp空间,@component若无参数,那就是只能类方式加载 注解开发不用set 构造器 注入函数 注解注入属性 管理第三方bean 示例: 数据库的类写在一个文件,文件 ...

  10. MSYS2使用记录——win10系统64位安装msys2最新版(msys2-x86_64-20190524.exe)

    MSYS2使用记录--win10系统64位安装msys2最新版(msys2-x86_64-20190524.exe) 安装 测试系统: windows 10 首先需要msys2的安装包,可以去官网下载 ...