SpringBoot配置多数据源
原文:https://www.jianshu.com/p/033e0ebeb617
项目中用到了两个数据库,分别是Oracle和Mysql,涉及到了多数据源问题,这里做下记录
官方讲解:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
日志JDBC配置:https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_LogFilter
Druid常见问题汇总:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
配置
- 首先配置application.yml文件,这里利用阿里的Druid作为连接池,base和follow分别代表主库和从库
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
base:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
initialize: true #指定初始化数据源,是否用data.sql来初始化,默认: true
name: base
jdbc-url: jdbc:oracle:thin:@189.126.156.396:9522:oratest
username: 用户名
password: 密码
follow:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
initialize: true
name: follow
url: jdbc:mysql://162.66.66.66:3666/paygateway
username: 你的歌用户名
password: 你的密码
---
#下面是分页和日志相关内容,不需要可以删掉
pagehelper:
reasonable: true
helperDialect: oracle
support-methods-arguments: true
params: count=countSql
mybatis:
configuration:
mapUnderscoreToCamelCase: true
logging:
level:
com:
xxx:
paygateway:
dao: DEBUG
org:
spring:
springboot:
dao: DEBUG
springframework: WARN
- 配置主库Configuration,注意prefix前缀和@Primary注解(表示主库)和下划线转驼峰方法的配置
@Configuration
@MapperScan(basePackages = "com.xxx.paygateway.dao.db1", sqlSessionTemplateRef = "baseSqlSessionTemplate")
public class BaseDataSourceConfig {
//这里配置数据源
@Bean(name = "baseDataSource")
@ConfigurationProperties(prefix = "spring.datasource.base")
@Primary
public DataSource setDataSource() {
return DataSourceBuilder.create().build();
}
//这里配置事务管理器
@Bean(name = "baseTransactionManager")
@Primary
public DataSourceTransactionManager setTransactionManager(@Qualifier("baseDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
//这里配置SqlSessionFactory,连接工厂
@Bean(name = "baseSqlSessionFactory")
@Primary
public SqlSessionFactory setSqlSessionFactory(@Qualifier("baseDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//此处配置下划线转驼峰
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
bean.setDataSource(dataSource);
//此处配置mybatis扫描路径
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/base/*.xml"));
return bean.getObject();
}
//这里配置SqlSessionTemplate,标准操作模板
@Bean(name = "baseSqlSessionTemplate")
@Primary
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
- 配置从库Configuration,与从库基本一致,注意注解和前缀就可以了
@Configuration
@MapperScan(basePackages = "com.xxx.paygateway.dao.db2", sqlSessionTemplateRef = "fSqlSessionTemplate")
public class FollowDataSourceConfig {
@Bean(name = "fDataSource")
@ConfigurationProperties(prefix = "spring.datasource.follow")
public DataSource setDataSource() {
return new DruidDataSource();
}
@Bean(name = "fTransactionManager")
public DataSourceTransactionManager setTransactionManager(@Qualifier("fDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "fSqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("fDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/follow/*.xml"));
return bean.getObject();
}
@Bean(name = "fSqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("fSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
把两个数据库的dao层和mapper.xml文件分到不同的文件夹下面,1归1,2归2。service层正常调用即可!
image.pngimage.png
作者:后厂村老司机
链接:https://www.jianshu.com/p/033e0ebeb617
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
SpringBoot配置多数据源的更多相关文章
- springboot配置Druid数据源
springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...
- SpringBoot配置多数据源时遇到的问题
SpringBoot配置多数据源 参考代码:Spring Boot 1.5.8.RELEASE同时配置Oracle和MySQL 原作者用的是1.5.8版本的SpringBoot,在升级到2.0.*之后 ...
- springboot 配置多数据源
1.首先在创建应用对象时引入autoConfig package com; import org.springframework.boot.SpringApplication; import org. ...
- springboot 配置多数据源 good
1.首先在创建应用对象时引入autoConfig package com; import org.springframework.boot.SpringApplication; import org. ...
- springboot配置多数据源mybatis配置失效问题
mybatis配置 #开启驼峰映射 mybatis.configuration.map-underscore-to-camel-case=true #开启打印sql mybatis.configura ...
- springboot配置多数据源(JdbcTemplate方式)
在实际开发中可能会遇到需要配置多个数据源的情况,比如:需要使用多个host.需要使用多种数据库(MySql.Oracle.SqlServer…) 如果使用springboot开发,可做如下配置: Co ...
- SpringBoot配置多数据源Mysql+Sqlite
配置了一下druid的多数据源配置,尝试了很多方法,Spring boot关于对Mysql和Sqlite多数据源的配置,记录下来: 涉及技术点: Springboot + Druid + Mysq ...
- Springboot配置多数据源(Mysql和Orcale)--(Idea Maven JDBCTemplate支持下的)
1.配置 orcale jdbc 对于一个Maven项目,使用Mysql时,可直接添加如下依赖: <dependency> <groupId>mysql</groupId ...
- springboot配置双数据源 MySQL和SqlServer
1. pom文件的驱动jar包加上去, compile 'com.microsoft.sqlserver:mssql-jdbc:6.2.2.jre8' 2. application.yml sprin ...
随机推荐
- 如何将IOS版本的更新下载文件指向到自己的服务器
针对那些使用企业签名但是没有发布到AppSotre的IOS版本APP自动更新问题解决方案: 在apicloud中是这样说明的: 因为要填写plist地址所以不能向安卓那样直接填写服务器文件地址,但是直 ...
- Java与.NET的WebServices相互调用
一:简介 本文介绍了Java与.NET开发的Web Services相互调用的技术.本文包括两个部分,第一部分介绍了如何用.NET做客户端调用Java写的Web Services,第二部分介绍了如何用 ...
- 【转】java comparator 升序、降序、倒序从源码角度理解
原文链接:https://blog.csdn.net/u013066244/article/details/78997869 环境jdk:1.7+ 前言之前我写过关于comparator的理解,但是都 ...
- Ibatis.Net 数据库操作学习(四)
一.查询select 还记得第一篇示例中是如何读出数据库里3条数据的吗?就是调用了一个QueryForList方法,从方法名就知道,查询返回列表. 1.QueryForList 返回List< ...
- 和为k的最长子数组及其延伸
问题1: /** * 问题描述: * 给定一个无序数组arr,其中元素可正.可负.可0, * 求arr所有的子数组中正数与负数个数相等的最长子数组长度 * * 解题思路:对数组进行处理,正数为1,负数 ...
- 【LOJ】#2108. 「JLOI2015」装备购买
题解 换成long double才过--出题人丧心病狂卡精度 只要按照费用排序从小到大排序,一个个插入线性基,插入的时候加上费用即可 代码 #include <bits/stdc++.h> ...
- 使用Sublime Text 3的HTML-CSS-JS Prettify插件格式化代码
很多时候,我们想要格式化HTML-CSS-JS代码,网站上有很多实现此功能的小工具,当然,我的网站首页也有.但是,如果我们的代码编辑器上面也有这样的功能,那不是更加快速便捷?So,接下来,我们看看Su ...
- 2018 Arab Collegiate Programming Contest (ACPC 2018) H - Hawawshi Decryption 数学 + BSGS
H - Hawawshi Decryption 对于一个给定的生成数列 R[ 0 ] 已知, (R[ i - 1 ] * a + b) % p = R[ i ] (p 是 质数), 求最小的 x 使得 ...
- 日志收集框架flume的安装及简单使用
flume介绍 Flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统. Flume可以采集文件,socket数据包等各种形式源数据,又可以将采集到的数据输出到HDFS.hbase.h ...
- 018 jquery中的事件
一:事件 1.Dom的两种加载方式 2.程序 略 二:事件绑定 1.事件绑定介绍 2.程序一(使用click的原始方式) <!DOCTYPE html> <html> < ...