使用MapperScannerConfigurer配置多数据源

使用MapperScannerConfigurer的annotationClass设置多数据库

使用xml进行配置,dao与domain与mapper不进行隔离

1. 文件目录,只包含了本文相关的内容

1
2
3
4
5
6
java.in.xjp
- domain // 数据结构
- dao // 与mapper绑定进行数据操作
- repository // 数据源选择用注解类,设置在dao上
resources
- mapper // sql语句以及与dao、domain进行绑定

2. 设置数据源相关的配置

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!-- 设置数据源 -->
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driverclass}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<!-- else config -->
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- .... -->
</bean>
<!-- 设置SqlSessionFactory, 绑定dataSource,设置domain与mapper路径 -->
<bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource1" />
<property name="typeAliasesPackage" value="in.xjp.domain" />
<property name="mapperLocations" value="classpath:mapper/**/*.xml" />
</bean>
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="typeAliasesPackage" value="in.xjp.domain" />
<property name="mapperLocations" value="classpath:mapper/**/*.xml" />
</bean>
<!-- 设置mapper相关的dao的路径,绑定SqlSessionFactory,以及各数据源选择时对应的注解类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="in.xjp.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1" />
<property name="annotationClass" value="in.xjp.repository.DbRepo1" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="in.xjp.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2" />
<property name="annotationClass" value="in.xjp.repository.DbRepo2" />
</bean>
<!-- 事务管理配置 -->
<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource1" />
</bean>
<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourcee2" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager1" />
<tx:annotation-driven transaction-manager="transactionManager2" />

3. java代码

多数据源选择用注解类

DbRepo1.java

1
2
@Repository
public @interface DbRepo1 {}

DbRepo2.java

1
2
@Repository
public @interface DbRepo2 {}

RepositoryDao1.java, 具体的mapper的可以参考mybatis文档

1
2
3
4
@DbRepo1
public interface RepositoryDao1 {
Integer countAll();
}

RepositoryDao2.java

1
2
3
4
@DbRepo2
public interface RepositoryDao2 {
Integer countAll();
}

4. 完成

1
2
3
4
5
6
7
8
9
10
11
12
13
@Autowired
private RepositoryDao1 dao1;
@Autowired
private RepositoryDao2 dao2;
public void whateverFn() {
// 于是就分别请求到了不同的数据库
Inteber count1 = dao1.countAll();
Inteber count2 = dao2.countAll();
}