Spring boot 加Mybatis加dubble多数据源配置

本文详细介绍了如何在Spring Boot项目中配置Mybatis与多数据源,包括MySQL和Oracle的远程连接。重点在于数据源的配置、事务和拦截器的分离、目录结构的设定以及Mapper的正确映射。文章提供了遇到问题时的解决方案和参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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="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="Throwa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值