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/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://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="Thro