您的位置:首页 > 编程语言 > Java开发

在Spring MVC框架下配置双(多)数据源

2016-12-19 20:57 411 查看
一,环境

应用框架:Spring MVC(3.x)+Hibernate(4.x)+Spring Data JPA(1.5.x)

应用程序服务器(容器):Tomcat 6.x

数据源:MYSQL(系统框架,角色定义,权限)、ORACLE(业务数据)

开发环境:Eclipse Java EE IDE for Web Developers(Indigo)

二,工程结构

Controller、Entity、Repository和Service都分为系统(sys)和业务(biz)两部分。

其中Repository下面通过包来区分数据源(可以设置2个甚至多个数据源,对应不同的包即可);

Entities则不存在划分数据源,有多少个Entitiy(POJO)则会在所有的数据源下面建立多少对应的数据表。



三,关键配置

(1)web.xml配置

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/jpa.xml</param-value>
</context-param>


(2)jpa.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd"> 
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="fileEncoding" value="UTF-8"/>
<property name="locations">
<list>
<value>classpath:proj.properties</value>
</list>
</property>
</bean>

<!--/////////////////////////++++++++++++++++++++////////////////////////-->
<!--/////////////////////// 框架数据源(MYSQL)配置  //////////////////////-->
<!--/////////////////////////____________________////////////////////////-->

<bean id ="fooDataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
p:driverClassName="${foo.db.driverClass}"
p:url="${foo.db.url}"
p:username="${foo.db.username}"
p:password="${foo.db.password}"
p:maxActive="${foo.db.maxActive}"
p:maxIdle="${foo.db.maxIdle}"
destroy-method="close">
<property name="defaultAutoCommit" value="true" />
</bean>

<bean id="fooEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="fooJPA" />
<property name="dataSource" ref="fooDataSource" />
</bean>

<bean id="fooTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="fooEntityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="fooTransactionManager" />

<!-- ### 当代码结构改变时需修改此处类路径  ###-->
<jpa:repositories base-package="com.xxx.xxx.repositories.sys"
entity-manager-factory-ref="fooEntityManagerFactory"
transaction-manager-ref="fooTransactionManager" />

<!--//////////////////////// 业务数据源(ORACLE)配置 /////////////////////-->
<!--////////////////////////________________________/////////////////////-->

<bean id ="bizDataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
p:driverClassName="${biz.db.driverClass}"
p:url="${biz.db.url}"
p:username="${biz.db.username}"
p:password="${biz.db.password}"
p:maxActive="${biz.db.maxActive}"
p:maxIdle="${biz.db.maxIdle}"
destroy-method="close">
<property name="defaultAutoCommit" value="true" />
</bean>

<bean id="bizEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="bizJPA" />
<property name="dataSource" ref="bizDataSource" />
</bean>

<bean id="bizTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="bizEntityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="bizTransactionManager" />

<!-- ### 当代码结构改变时需修改此处类路径  ###-->
<jpa:repositories base-package="com.xxxx.xxxx.repositories.biz"
entity-manager-factory-ref="bizEntityManagerFactory"
transaction-manager-ref="bizTransactionManager" />

<!-- /////////////////////////// 数据源配置结束 //////////////////////// -->
<!--/////////////////////////____________________////////////////////////-->

<!-- ### 当代码结构改变时需修改此处类路径  ###-->
<bean id="appProperties" class="com.xxx.xxx.util.AppProperties">
<property name="filename" value="proj.properties" />
</bean>

<import resource="secure.xml"/>
<import resource="mq.xml"/>
<import resource="quartz.xml"/>

</beans>
其中:

<!-- ### 当代码结构改变时需修改此处类路径  ###-->
<jpa:repositories base-package="com.xxx.xxx.repositories.sys"
entity-manager-factory-ref="fooEntityManagerFactory"
transaction-manager-ref="fooTransactionManager" />




<!-- ### 当代码结构改变时需修改此处类路径  ###-->
<jpa:repositories base-package="com.xxxx.xxxx.repositories.biz"
entity-manager-factory-ref="bizEntityManagerFactory"
transaction-manager-ref="bizTransactionManager" />
用于通过repository所在的包名来绑定不同的数据源。

也就是说com.xxx.xxx.repositories.biz下面所有的repository用于访问业务数据源;

com.xxxx.xxxx.repositories.sys下面所有的repository用于访问系统数据源。

四,repository



其中SysUserRepository用于存取系统用户表,对应的pojo为SysUser,对应的数据表为tab_sys_user,数据库系统为MYSQL;

PaymentLogRepository用于存取业务系统表,对应的pojo为PaymentLog,对应的数据表为TAB_PAYMENT_LOG,数据系统为ORACLE。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: