一、概述
基本項目搭建
技術框架:spring web mvc 、日志【slf4j、log4j2】、mybatis、druid、jetty插件啟動、mybatis-generator逆向配置生產dao、分頁插件pagehelper
項目地址:https://github.com/bjlhx15/mybatis.git 中的mybatis-readwrite-split 基礎項目
二、Spring+MyBatis實現讀寫整理
2.1、方案一、【讀寫mapper分開寫】
通過MyBatis配置文件創建讀寫分離兩個DataSource,每個SqlSessionFactoryBean對象的mapperLocations屬性制定兩個讀寫數據源的配置文件。將所有讀的操作配置在讀文件中,所有寫的操作配置在寫文件
- 優點:實現簡單
- 缺點:維護麻煩,需要對原有的xml文件進行重新修改,不支持多讀,不易擴展
實現方式
項目地址:https://github.com/bjlhx15/mybatis.git 中的mybatis-readwrite-split-001 基礎項目
核心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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 連接池基本 父類 --> <bean id="abstractDataSource" abstract="true" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <!-- 配置獲取連接等待超時的時間 --> <property name="maxWait" value="60000"/> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000"/> <property name="validationQuery" value="SELECT 'x'"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <!-- 打開PSCache,並且指定每個連接上PSCache的大小 --> <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> <!-- <property name="filters" value="config"/>--> <!-- <property name="connectionProperties" value="config.decrypt=true"/>--> </bean> <!--讀連接池--> <bean id="readDataSource" parent="abstractDataSource"> <!-- 基本屬性 url、user、password --> <property name="url" value="jdbc:mysql://192.168.1.1:3358/test"/> <property name="username" value="root"/> <property name="password" value="root"/> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="10"/> <property name="minIdle" value="10"/> <property name="maxActive" value="10"/> </bean> <!--讀寫連接池--> <bean id="writeDataSource" parent="abstractDataSource"> <!-- 基本屬性 url、user、password --> <property name="url" value="jdbc:mysql://192.168.1.1:3358/test"/> <property name="username" value="root"/> <property name="password" value="root"/> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="10"/> <property name="minIdle" value="10"/> <property name="maxActive" value="10"/> </bean> <!-- 實例化sqlSessionFactory時需要使用上述配置好的數據源以及SQL映射文件 --> <bean id="readSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 實例化sqlSessionFactory時需要使用上述配置好的數據源以及SQL映射文件 --> <property name="dataSource" ref="readDataSource"/> <property name="mapperLocations" value="classpath:mapper/read/*.xml"/> <!-- mybatis的全局配置文件 如沒有特需 可以不配置 --> <property name="configLocation" value="classpath:mybatis.xml"/> <!-- 配置分頁插件 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> helperDialect=mysql reasonable=true </value> </property> </bean> </array> </property> </bean> <!-- 實例化sqlSessionFactory時需要使用上述配置好的數據源以及SQL映射文件 --> <bean id="writeSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 實例化sqlSessionFactory時需要使用上述配置好的數據源以及SQL映射文件 --> <property name="dataSource" ref="writeDataSource"/> <property name="mapperLocations" value="classpath:mapper/write/*.xml"/> <!-- mybatis的全局配置文件 如沒有特需 可以不配置 --> <property name="configLocation" value="classpath:mybatis.xml"/> </bean> <!-- 必須添加 mapper 得掃描 因為mybatis 生成的mapper沒有注解--> <bean id="mapperScannerConfigurer1" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.github.bjlhx15.mybatis.readwrite.split.repository.read"/> <property name="sqlSessionFactoryBeanName" value="readSqlSessionFactory"/> </bean> <!-- 必須添加 mapper 得掃描 因為mybatis 生成的mapper沒有注解--> <bean id="mapperScannerConfigurer2" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.github.bjlhx15.mybatis.readwrite.split.repository.write"/> <property name="sqlSessionFactoryBeanName" value="writeSqlSessionFactory"/> </bean> </beans>
2.2、方案二、【AOP,DAO方法加注解】
通過Spring AOP在業務層實現讀寫分離,在DAO層調用前定義切面,利用Spring的AbstractRoutingDataSource解決多數據源的問題,實現動態選擇數據源
- 優點:通過注解的方法在DAO每個方法上配置數據源,原有代碼改動量少,易擴展,支持多讀
- 缺點:需要在DAO每個方法上配置注解,人工管理,容易出錯
實現方式:
項目地址:https://github.com/bjlhx15/mybatis.git 中的mybatis-readwrite-split-002-aop 基礎項目
定義如下工具類
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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- 連接池基本 父類 --> <bean id="abstractDataSource" abstract="true" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <!-- 配置獲取連接等待超時的時間 --> <property name="maxWait" value="60000"/> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000"/> <property name="validationQuery" value="SELECT 'x'"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <!-- 打開PSCache,並且指定每個連接上PSCache的大小 --> <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> <!-- <property name="filters" value="config"/>--> <!-- <property name="connectionProperties" value="config.decrypt=true"/>--> </bean> <!--讀1連接池--> <bean id="dataSourceRead1" parent="abstractDataSource"> <!-- 基本屬性 url、user、password --> <property name="url" value="jdbc:mysql://192.168.1.2:3358/test"/> <property name="username" value="read"/> <property name="password" value="read"/> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="10"/> <property name="minIdle" value="10"/> <property name="maxActive" value="10"/> </bean> <!--讀2連接池--> <bean id="dataSourceRead2" parent="abstractDataSource"> <!-- 基本屬性 url、user、password --> <property name="url" value="jdbc:mysql://192.168.1.2:3358/test"/> <property name="username" value="read"/> <property name="password" value="read"/> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="10"/> <property name="minIdle" value="10"/> <property name="maxActive" value="10"/> </bean> <!--寫連接池--> <bean id="dataSourceWrite" parent="abstractDataSource"> <!-- 基本屬性 url、user、password --> <property name="url" value="jdbc:mysql://192.168.1.1:3358/test"/> <property name="username" value="root"/> <property name="password" value="root"/> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="10"/> <property name="minIdle" value="10"/> <property name="maxActive" value="10"/> </bean> <bean id="dataSource" class="com.github.bjlhx15.mybatis.readwrite.split.datasource.DynamicDataSource"> <property name="writeDataSource" ref="dataSourceWrite"/> <property name="readDataSources"> <list> <ref bean="dataSourceRead1"/> <ref bean="dataSourceRead2"/> </list> </property> <!--輪詢方式--> <property name="readDataSourcePollPattern" value="1"/> <!-- 什么都沒有的注釋 使用的數據源--> <property name="defaultTargetDataSource" ref="dataSourceWrite"/> </bean> <!-- 事務--> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 針對myBatis的配置項 --> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 實例化sqlSessionFactory時需要使用上述配置好的數據源以及SQL映射文件 --> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/auto/**/*.xml"/> <!-- mybatis的全局配置文件 如沒有特需 可以不配置 --> <property name="configLocation" value="classpath:mybatis.xml"/> <!-- 配置分頁插件 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> helperDialect=mysql reasonable=true </value> </property> </bean> </array> </property> </bean> <!-- 配置掃描器 必須添加 mapper 得掃描 因為mybatis 生成的mapper沒有注解--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.github.bjlhx15.mybatis.readwrite.split.repository"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 配置數據庫注解aop --> <bean id="dynamicDataSourceAspect" class="com.github.bjlhx15.mybatis.readwrite.split.datasource.DynamicDataSourceAspect"/> <aop:config> <aop:aspect id="c" ref="dynamicDataSourceAspect"> <aop:pointcut id="tx" expression="execution(* com.github.bjlhx15.mybatis.readwrite.split.repository.auto..*.*(..))"/> <aop:before pointcut-ref="tx" method="before"/> <aop:after pointcut-ref="tx" method="after"/> </aop:aspect> </aop:config> <!-- 配置數據庫注解aop --> </beans>
需要在使用的方法上添加注解
@DataSource(DynamicDataSourceGlobal.READ)
long countByExample(AccountBalanceExample example);
2.3、方案三、【動態代理,mybatis攔截器】【調試中】
通過Mybatis的Plugin在業務層實現數據庫讀寫分離,在MyBatis創建Statement對象前通過攔截器選擇真正的數據源,在攔截器中根據方法名稱不同(select、update、insert、delete)選擇數據源。
- 優點:原有代碼不變,支持多讀,易擴展
- 缺點:
實現方式:
項目地址:https://github.com/bjlhx15/mybatis.git 中的mybatis-readwrite-split-003 基礎項目
定義如下工具類
核心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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- 連接池基本 父類 --> <bean id="abstractDataSource" abstract="true" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <!-- 配置獲取連接等待超時的時間 --> <property name="maxWait" value="60000"/> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000"/> <property name="validationQuery" value="SELECT 'x'"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <!-- 打開PSCache,並且指定每個連接上PSCache的大小 --> <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> <!-- <property name="filters" value="config"/>--> <!-- <property name="connectionProperties" value="config.decrypt=true"/>--> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="10"/> <property name="minIdle" value="10"/> <property name="maxActive" value="10"/> </bean> <!--讀1連接池--> <bean id="dataSourceRead1" parent="abstractDataSource"> <!-- 基本屬性 url、user、password --> <property name="url" value="jdbc:mysql://127.0.0.1:3358/test?useSSL=false&characterEncoding=utf8"/> <property name="username" value="read"/> <property name="password" value="read"/> </bean> <!--讀2連接池--> <bean id="dataSourceRead2" parent="abstractDataSource"> <!-- 基本屬性 url、user、password --> <property name="url" value="jdbc:mysql://127.0.0.1:3358/test?useSSL=false&characterEncoding=utf8"/> <property name="username" value="read"/> <property name="password" value="read"/> </bean> <!--寫連接池--> <bean id="dataSourceWrite" parent="abstractDataSource"> <!-- 基本屬性 url、user、password --> <property name="url" value="jdbc:mysql://127.0.0.1:3358/test?useSSL=false&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean id="dataSource" class="com.github.bjlhx15.mybatis.readwrite.split.datasource.DynamicRoutingDataSourceProxy"> <property name="writeDataSource" ref="dataSourceWrite"/> <property name="readDataSources"> <list> <ref bean="dataSourceRead1"/> <ref bean="dataSourceRead2"/> </list> </property> <!--輪詢方式--> <property name="readDataSourcePollPattern" value="1"/> </bean> <!-- 事務--> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 針對myBatis的配置項 --> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 實例化sqlSessionFactory時需要使用上述配置好的數據源以及SQL映射文件 --> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/auto/**/*.xml"/> <!-- mybatis的全局配置文件 如沒有特需 可以不配置 --> <property name="configLocation" value="classpath:mybatis.xml"/> <!-- 配置分頁插件 --> <!-- <property name="plugins">--> <!-- <array>--> <!-- <bean class="com.github.pagehelper.PageInterceptor">--> <!-- <property name="properties">--> <!-- <value>--> <!-- helperDialect=mysql--> <!-- reasonable=true--> <!-- </value>--> <!-- </property>--> <!-- </bean>--> <!-- </array>--> <!-- </property>--> </bean> <!-- 配置掃描器 必須添加 mapper 得掃描 因為mybatis 生成的mapper沒有注解--> <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">--> <!-- <property name="basePackage" value="com.github.bjlhx15.mybatis.readwrite.split.repository"/>--> <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>--> <!-- </bean>--> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg ref="sqlSessionFactory" /> </bean> <!-- 通過掃描的模式,掃描目錄下所有的mapper, 根據對應的mapper.xml為其生成代理類--> <bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.github.bjlhx15.mybatis.readwrite.split.repository" /> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property> </bean> </beans>
方案四、AbstractRoutingDataSource和mybatis攔截器【推薦】
后台結構是spring+mybatis,可以通過spring的AbstractRoutingDataSource和mybatis Plugin攔截器實現非常友好的讀寫分離,原有代碼不需要任何改變。推薦第四種方案
實現的重點是下面這兩個:
1、org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
spring提供的這個類能夠讓我們實現運行時多數據源的動態切換,但是數據源是需要事先配置好的,無法動態的增加數據源。但是對於小項目來說已經足夠了。
2、MyBatis提供的@Intercepts、@Signature注解和org.apache.ibatis.plugin.Interceptor接口。
注:另外還有一個就是ThreadLocal類,用於保存每個線程正在使用的數據源。ThreadLocal的原理之前已經分析過了。
運行流程:
1、我們自己寫的MyBatis的Interceptor按照@Signature的規則攔截下Executor.class的update和query方法
2、判斷是讀還是寫方法,然后在ThreadLocal里保存一個讀或者寫的變量
3、線程再根據這個變量作為key從全局靜態的HashMap中取出當前要用的讀或者寫數據源
4、返回對應數據源的connection去做相應的數據庫操作
實現方案
項目地址:https://github.com/bjlhx15/mybatis.git 中的mybatis-readwrite-split-004 基礎項目
定義如下工具類
核心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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- 連接池基本 父類 --> <bean id="abstractDataSource" abstract="true" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <!-- 配置獲取連接等待超時的時間 --> <property name="maxWait" value="60000"/> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000"/> <property name="validationQuery" value="SELECT 'x'"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <!-- 打開PSCache,並且指定每個連接上PSCache的大小 --> <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="10"/> <property name="minIdle" value="10"/> <property name="maxActive" value="10"/> <!-- <property name="filters" value="config"/>--> <!-- <property name="connectionProperties" value="config.decrypt=true"/>--> </bean> <!--寫連接池--> <bean id="autoSuperDataSourceWrite" parent="abstractDataSource"> <!-- 基本屬性 url、user、password --> <property name="url" value="jdbc:mysql://127.0.0.1:3358/test?useSSL=false&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean id="autoSuperDataSourceRead" parent="abstractDataSource"> <!-- 基本屬性 url、user、password --> <property name="url" value="jdbc:mysql://127.0.0.1:3358/test?useSSL=false&characterEncoding=utf8"/> <property name="username" value="read"/> <property name="password" value="read1"/> </bean> <bean id="autoSuperDataSource" class="com.github.bjlhx15.mybatis.readwrite.split.datasource.DynamicDataSource"> <property name="writeDataSource" ref="autoSuperDataSourceWrite"></property> <property name="readDataSource" ref="autoSuperDataSourceRead"></property> </bean> <tx:annotation-driven transaction-manager="autoSuperTransactionManager"/> <bean id="autoSuperTransactionManager" class="com.github.bjlhx15.mybatis.readwrite.split.datasource.DynamicDataSourceTransactionManager"> <property name="dataSource" ref="autoSuperDataSource"/> </bean> <!-- 針對myBatis的配置項 --> <bean id="autoSuperSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 實例化sqlSessionFactory時需要使用上述配置好的數據源以及SQL映射文件 --> <property name="dataSource" ref="autoSuperDataSource"/> <property name="mapperLocations" value="classpath:mapper/auto/**/*.xml"/> <!-- mybatis的全局配置文件 如沒有特需 可以不配置 --> <property name="configLocation" value="classpath:mybatis.xml"/> <property name="typeAliasesPackage" value="com.github.bjlhx15.mybatis.readwrite.split.model"></property> <!-- 配置分頁插件 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> helperDialect=mysql reasonable=true </value> </property> </bean> </array> </property> </bean> <!-- 配置掃描器 --> <!-- 必須添加 mapper 得掃描 因為mybatis 生成的mapper沒有注解--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.github.bjlhx15.mybatis.readwrite.split.repository"/> <property name="sqlSessionFactoryBeanName" value="autoSuperSqlSessionFactory"/> </bean> </beans>
參看地址:https://www.jianshu.com/p/2222257f96d3
數據權限管理中心:https://my.oschina.net/gmarshal/blog/1797026
注事務配置
有兩種方式,上述情況均可使用
上述是使用注解方式,下述是xml方式
測試一、配置txmethod中不成功
<!-- tx:annotation-driven 注解事務 tx:advice配置事務 二選一 --> <!-- 支持 @Transactional 標記 --> <!-- <tx:annotation-driven transaction-manager="autoSuperTransactionManager"/>--> <tx:advice id="txAdvice" transaction-manager="autoSuperTransactionManager"> <tx:attributes> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="execute*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="doCreate*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> <tx:method name="selectByPrimaryKeyOne" propagation="NOT_SUPPORTED" read-only="true"/> <tx:method name="dynamicsSqlSkuTraceData" propagation="NOT_SUPPORTED" read-only="true"/> <tx:method name="query*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <aop:config expose-proxy="true" proxy-target-class="true"> <aop:pointcut id="pc" expression="execution(* com.jd.bt.middle.data.service..*.*(..))"/> <aop:advisor pointcut-ref="pc" advice-ref="txAdvice"/> </aop:config>
注意aop攔截的是 selectByPrimaryKeyOne, 沒有加事務屬性 propagation ,如果添加可能被事務傳播影響變成 寫數據源 沒起到作用 注意需要關閉事務支持 從庫不支持事務 propagation="NOT_SUPPORTED"
測試二、配置到aop切點中【成功】
<tx:advice id="txAdvice" transaction-manager="autoSuperTransactionManager"> <tx:attributes> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="execute*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="doCreate*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <!-- 下面aop排除即可,此處不用寫--> <!-- <tx:method name="selectByPrimaryKeyOne" propagation="NOT_SUPPORTED" read-only="true"/>--> <!-- <tx:method name="dynamicsSqlSkuTraceData" propagation="NOT_SUPPORTED" read-only="true"/>--> <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> <tx:method name="query*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <aop:config expose-proxy="true" proxy-target-class="true"> <aop:pointcut id="pc" expression="execution(* com.jd.bt.middle.data.service..*.*(..)) and !execution(* com.jd.bt.middle.data.service..*.selectByPrimaryKeyOne(..)) and !execution(* com.jd.bt.middle.data.service..*.dynamicsSqlSkuTraceData(..))"/> <aop:advisor pointcut-ref="pc" advice-ref="txAdvice"/> </aop:config>