spring mvc 多数据源配置(一):根据package配置不同的数据源

背景

有时候项目中可以需要连接两个数据库。因此就需要用到多数据源配置。

原理就是配置两个dataSource,两个entityManagerFactory分别使用不同的dataSource,并设置不同的package扫描路径。

配置文件

applicationContext.xml文件

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
52
53
54
<!-- 多数据源配置 -->
<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/data"></jee:jndi-lookup>
<jee:jndi-lookup id="dataSource2" jndi-name="java:jboss/datasources/data2"></jee:jndi-lookup>

<!-- entityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jtaDataSource" ref="dataSource" />
<property name="packagesToScan" value="com.telehot" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<bean class="com.telehot.platform.app.jpa.hibernate.JpaPropertiesFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="properties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
</property>
<property name="persistenceUnitName" value="platform-times" />
</bean>

<bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jtaDataSource" ref="dataSource2" />
<property name="packagesToScan" value="com.test" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<bean class="com.telehot.platform.app.jpa.hibernate.JpaPropertiesFactoryBean">
<property name="dataSource" ref="dataSource2"></property>
<property name="properties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
</property>
<property name="persistenceUnitName" value="platform-times" />
</bean>

<jpa:repositories base-package="com.telehot"
transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactory"
factory-class="com.telehot.platform.app.jpa.spring.MyRepositoryFactoryBean" />

<jpa:repositories base-package="com.test"
transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactory2"
factory-class="com.telehot.platform.app.jpa.spring.MyRepositoryFactoryBean" />

可以看到,上面配置了两个dataSource,两个entityManagerFactory,其中一个扫描com.telehot目录,另一个扫描com.test目录,从而实现了多数据源的自动配置,只要将代码放到相应的package下面即可,不需要手动切换数据源。

当然,jdbcTemplate也可以配置两个,使用的时候注入对应的bean就行

1
2
3
4
5
6
7
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource2"></property>
</bean>

1
2
3
@Qualifier("jdbcTemplate2")
@Autowired
JdbcTemplate jdbcTemplate;