可以使用切片,实现AbstractRoutingDataSource,这样的做法可以做更多的有想象的事,因为完全自己操作数据源的选择,可参考链接来实现

基于spring的aop实现多数据源动态切换

多数据源的另一种就是,在SpringApplicationContext.xml中配置多个datasource,并指定不同的mapper。这样存在的问题就是在运行中会提示事务非spring管理。新增事务部分切片配置。默认的Spring只能管理一个事务。

1
2
[2017-06-15 17:02:37.424] [DEBUG] [http-nio-9100-exec-1] org.mybatis.spring.SqlSessionUtils [SqlSessionUtils.java:148] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4d86a13] was not registered for synchronization because synchronization is not active
[2017-06-15 17:02:37.425] [DEBUG] [http-nio-9100-exec-1] o.m.s.t.SpringManagedTransaction [SpringManagedTransaction.java:87] - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b6caaa2] will not be managed by Spring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!-- 事务传播特性配置 -->
<tx:advice id="infoTransactionAdvice" transaction-manager="infoTxManager">
<tx:attributes>
<tx:method name="insert" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置哪些方法参与事务 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="infoTransactionxPointcut" expression="execution(* com.chinawayltd.sms.schedule.mapper.info.*.*(..))"/>
<aop:advisor advice-ref="infoTransactionAdvice" pointcut-ref="infoTransactionxPointcut"/>
</aop:config>
<!-- 事务传播特性配置 -->
<tx:advice id="transactionAdvice" transaction-manager="smsTxManager">
<tx:attributes>
<tx:method name="insert" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置哪些方法参与事务 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="transactionxPointcut" expression="execution(* com.chinawayltd.sms.schedule.mapper.sms.*.*(..))"/>
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionxPointcut"/>
</aop:config>