Spring boot 整合Mybatis+dubble +maven单数据源的例子很多,但是多数据源配置需要将原本封装好的重写一遍,有点麻烦,对框架的理解要求比较高,

在这详细给大家介绍下如何配置使用:

单数据源的配置,有Mybatis和Spring boot 整合的jar包,在pom引入一下依赖transaction和datasource都是Spring自动配置的,但是如果是使用多数据源,就要分开重写,也就是说,第一个数据源和第二个都要重写并指定默认初始数据源;

我这里写的一个是mysql一个是Oracle,都是远程连接,

我这边采用的是路径分离的方法区分数据源,配置文件和Java代码都做了新的路径,方便映射,适合菜鸟使用;

1.首先,Spring boot扫描路径是固定的,所以我们加的第二数据源路径命名必须符合Spring boot的规范,

2.配置文件中,事务配置和拦截器配置都要分开写

spring-aop-transaction.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"default-autowire="byName" default-lazy-init="false"><!-- 需要事务拦截的路径 --><aop:config proxy-target-class="true"><aop:advisor pointcut="execution( * com.ai.rai.interests.*.service.impl.*.*(..))"advice-ref="oracleTxAdvice"/></aop:config><tx:advice id="oracleTxAdvice"><tx:attributes><tx:method name="select*" read-only="true" propagation="SUPPORTS" /><tx:method name="list*" read-only="true" propagation="SUPPORTS" /><tx:method name="query*" read-only="true" propagation="SUPPORTS" /><tx:method name="get*" read-only="true" propagation="SUPPORTS" /><tx:method name="detail*" read-only="true" propagation="SUPPORTS" /><tx:method name="count*" read-only="true" propagation="SUPPORTS" /><tx:method name="insert*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /><tx:method name="update*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /><tx:method name="delete*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /><tx:method name="save*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /><tx:method name="newTran*" read-only="false" propagation="REQUIRES_NEW" rollback-for="Throwable" /><tx:method name="xaTran*" read-only="false" propagation="REQUIRED" isolation="SERIALIZABLE" rollback-for="Throwable" /><tx:method name="*" timeout="30" /></tx:attributes></tx:advice></beans>

spring-aop-transaction-oracle.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"default-autowire="byName" default-lazy-init="false"><!-- 需要事务拦截的路径 --><aop:config proxy-target-class="true"><aop:advisor pointcut="execution( * com.ai.rai.interests.*.service.impl.*.*(..))"advice-ref="oracleTxAdvice"/></aop:config><tx:advice id="oracleTxAdvice"><tx:attributes><tx:method name="select*" read-only="true" propagation="SUPPORTS" /><tx:method name="list*" read-only="true" propagation="SUPPORTS" /><tx:method name="query*" read-only="true" propagation="SUPPORTS" /><tx:method name="get*" read-only="true" propagation="SUPPORTS" /><tx:method name="detail*" read-only="true" propagation="SUPPORTS" /><tx:method name="count*" read-only="true" propagation="SUPPORTS" /><tx:method name="insert*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /><tx:method name="update*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /><tx:method name="delete*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /><tx:method name="save*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /><tx:method name="newTran*" read-only="false" propagation="REQUIRES_NEW" rollback-for="Throwable" /><tx:method name="xaTran*" read-only="false" propagation="REQUIRED" isolation="SERIALIZABLE" rollback-for="Throwable" /><tx:method name="*" timeout="30" /></tx:attributes></tx:advice></beans>

3.数据源信息配置

这里用的是

druid的配置方式
spring:datasource:druid:url: jdbc:mysql://xx.xx.xx.xx:3306/operation?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=falseusername: xxpassword: xxvalidationQuery: select 1maxActive: 20minIdle: 1initialSize: 4testOnReturn: falsetestWhileIdle: truetestOnBorrow: truelogAbandoned: falsetimeBetweenEvictionRunsMillis: 600000minEvictableIdleTimeMillis: 600000poolPreparedStatements: trueremoveAbandoned: truedriver-class-name: com.mysql.jdbc.Drivername: operation---
spring:datasource:druidOracle:url: jdbc:oracle:thin:@xx.xx.xx.xx:1521:ddzxcsdbusername: xxpassword: xxvalidationQuery: select 1 from dualmaxActive: 20minIdle: 1initialSize: 4testOnReturn: falsetestWhileIdle: truetestOnBorrow: truelogAbandoned: falsetimeBetweenEvictionRunsMillis: 600000minEvictableIdleTimeMillis: 600000poolPreparedStatements: trueremoveAbandoned: truedriver-class-name: oracle.jdbc.OracleDrivername: ds_o2o_co

4.然后是两个数据源信息的整合

DataSource1Config.java

package com.ai.rai.interests.operation.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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.ai.rai.interests.operation.dao",sqlSessionTemplateRef = "mySqlSessionTemplate")
public class DataSource1Config {@Bean(name = "myDataSource")@ConfigurationProperties(prefix = "spring.datasource.druid")@Primarypublic DataSource myDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "mySqlSessionFactory")@Primarypublic SqlSessionFactory sqlSessionFactory(@Qualifier("myDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/*.xml"));return bean.getObject();}@Bean(name = "transactionManager")@Primarypublic DataSourceTransactionManager transactionManager(@Qualifier("myDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "mySqlSessionTemplate")@Primarypublic SqlSessionTemplate sqlSessionTemplate(@Qualifier("mySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}}

DataSource2Config.java

package com.ai.rai.interests.operation.datasource;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.ai.rai.interests.operation.daoOracle",sqlSessionTemplateRef = "oracleSqlSessionTemplate")
public class DataSource2Config {@Bean(name = "oracleDataSource")@ConfigurationProperties(prefix = "spring.datasource.druidOracle")public DataSource oracleDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "oracleSqlSessionFactory")public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapperOracle/*.xml"));return bean.getObject();}@Bean(name = "oracleTransactionManager")public DataSourceTransactionManager oracleTransactionManager(@Qualifier("oracleDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "oracleSqlSessionTemplate")public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}
}

5.请注意目录的分布和变量名称的对应

目录分级很重要,一定要写对,否则扫描不到;

6.扫描目录配置

在Spring boot启动类里面加上新增的配置文件路径

注意boot配置的语法

7.mapper里面配置的路径一定要确认是对应的数据源,

否则会报:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):这个错,

解决方案:https://blog.csdn.net/select_bin/article/details/80598699

尤其是对于用工具生成Mybatis文件的兄弟们;

8.如果使用框架整合了配置,尤其是很多工程集成的时候,比如:

如果数据源里面有Oracle的数据库,有时候可能会报这个错:

获取jdbc连接失败,加载数据库驱动失败, Failed to obtain JDBC Connection; nested exception is java.sql.SQLException:

解决方案:https://blog.csdn.net/select_bin/article/details/80608543

Example_Where_Clause

这个时候要设置扫描路径为本工程下面,然后从公共文件里面把配置文件复制一份到本工程,然后大功告成;

然后涉及Oracle和maven3,在加载包依赖的时候,maven3以后因为版权问题,不支持自动下载Oracle的jar包

简单粗暴的解决方案,在依赖里面自己配,然后把jar包放在maven的作用路径下面:

//手动导入jar包并在maven中配置依赖

mvn install:install-file -Dfile=D:\works\ojdbc7.jar -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=11.2.0.1.0 -Dpackaging=jar

记得把这个路径改为自己的

可以直接在IDEA的控制台操作,

然后再pom文件里面加一下依赖:

<dependency><groupId>com.oracle</groupId><artifactId>ojdbc7</artifactId><version>11.2.0.1.0</version>
</dependency>

jar包下载:https://download.csdn.net/download/select_bin/10464723

参考资料:

https://blog.csdn.net/erlian1992/article/details/74279106

9.参考的资料:

https://blog.csdn.net/select_bin/article/details/80438973

http://www.cnblogs.com/ityouknow/p/6102399.html

http://www.cnblogs.com/winner-0715/p/6687247.html

https://github.com/heikehuan/springboot-multiple-dataSources

Spring boot 加Mybatis加dubble多数据源配置相关推荐

  1. Spring Boot 整合 MyBatis Plus实现多数据源的两种方式

    第一种:使用配置类的方式: 项目结构 xml依赖: <?xml version="1.0" encoding="UTF-8"?> <proje ...

  2. Spring Boot使用MyBatis 3打印SQL的配置

    普通Spring下的XML文件配置: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE co ...

  3. spring boot 教程(六)多数据源配置与使用

    之前在介绍使用JdbcTemplate的时候使用了单数据源,在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.properties 文件中配置连接参数即可.但是 ...

  4. Spring Boot+JOOQ(六)多数据源配置

    在我们日常的开发中,通常需要连接同一个数据库的不同模式,或者连接到不同的数据库,那我们在使用Jooq时应该怎样解决多数据源的配置呢? 直接上代码: application.properties joo ...

  5. Spring Boot 入门系列(二十三)整合Mybatis,实现多数据源配置!

    d之前介绍了Spring Boot 整合mybatis 使用注解方式配置的方式实现增删改查以及一些复杂自定义的sql 语句 .想必大家对spring boot 项目中,如何使用mybatis 有了一定 ...

  6. Spring Boot不允许加载iframe问题解决

    2019独角兽企业重金招聘Python工程师标准>>> 在spring boot项目中出现不能加载iframe 页面空白,或者ie显示不允许加载页面 报一个"Refused ...

  7. 【spring boot】 mybatis配置双数据源/多数据源

    前言 spring boot 2.0.0.RELEASE maven 3.5 eclipse 4.9.0 mybatis 1.3.2 mybatis generator 1.3.2 pagehelpe ...

  8. spring boot 会默认加载DataSourceAutoConfiguration这个类

    spring boot 会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration这个类, 而DataSo ...

  9. Spring Boot 集成 Mybatis 实现双数据源

    转载自   Spring Boot 集成 Mybatis 实现双数据源 这里用到了Spring Boot + Mybatis + DynamicDataSource配置动态双数据源,可以动态切换数据源 ...

最新文章

  1. BZOJ 4827 [Hnoi2017]礼物 ——FFT
  2. java graphics透明_如何使用Graphics对象g绘制透明形状?
  3. Condition接口详解
  4. Python Django模板templates渲染及配置
  5. Security:CSRF
  6. jconsole命令(Java Monitoring and Management Console)
  7. 辉哥给rockchip修复了一个内存溢出问题
  8. 程序员必知的mysql插件_程序员必知的技术官网系列--mysql篇
  9. JS判断当前手机类型
  10. 程序员的福利:使用WebCollector爬取某美女网站上的图片
  11. 6.11编写计算正方体、圆柱体、球体的表面积和体积的类。
  12. 计算机本地连接没有有效ip配置,本地连接没有有效的ip配置,详细教您本地连接没有有效的ip配置怎么解决...
  13. C/C++ 由int (*p)[5]与int *p[5]引发的学习与思考
  14. js中sort()字母排序和自定义多列排序
  15. 如何让自己专注于工作
  16. Allwinner Camlinux的3g驱动移植
  17. 大数据专题(一)- 简介
  18. 斯坦福php项目怎么样,科学网—发现自己的论文居然成了斯坦福大学的课程项目 - 杨双的博文...
  19. 周易六十四卦——坎卦
  20. Cantor三分集为啥不能在Myeclipse上完美实现呢?

热门文章

  1. 微信小程序通过云函数下载视频
  2. 极客战纪——KITHMAZE最终历险攻略(JavaScript)
  3. LaTeX新手入门教程
  4. 异数OS 织梦师-水母(一)--消息队列篇 1
  5. M1 Macbook上手测评:这5件事儿你需要知道
  6. HTML基础标签:标题段落空格链接图像强调
  7. python 3.10出现的问题以及卸载python
  8. 分享Python开发的13个小游戏(附源码)
  9. TypeScript - 浏览器中运行TS
  10. Deterctron2 训练自己的数据集