Spring3+Hibernate3(Jpa) 配置多个数据源的解决方案(基于注解)
----------------配置文件--------------------------------
-------------------------------------META-INF/persistence.xml------------------------------------------------------
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence xmlns="http://java.sun.com/xml/ns/persistence"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
- version="1.0">
- <!--配置mysql数据源-->
- <persistence-unit name="mysql" transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.ejb.HibernatePersistence</provider>
- <non-jta-data-source>mysqlDataSource</non-jta-data-source>
- <!--这里的class是指要储存于本数据库的Entity-->
- <class>com.sunhope.model.core.ExternalEnvironment</class>
- <properties>
- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
- <property name="hibernate.archive.autodetection" value="false"/>
- <property name="hibernate.transaction.auto_close_session" value="false"/>
- <property name="hibernate.hbm2ddl.auto" value="update" />
- </properties>
- </persistence-unit>
- <!--配置postgre数据源-->
- <persistence-unit name="postgre" transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.ejb.HibernatePersistence</provider>
- <non-jta-data-source>postgreDataSource</non-jta-data-source>
- <class>com.sunhope.model.baseinfo.BdStorageCompany</class>
- <class>com.sunhope.model.baseinfo.BdHouseType</class>
- <class>com.sunhope.model.baseinfo.BdStorage</class>
- <class>com.sunhope.model.core.StoreEnvironment</class>
- <properties>
- <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
- <property name="hibernate.archive.autodetection" value="false"/>
- <property name="hibernate.transaction.auto_close_session" value="false"/>
- <property name="hibernate.hbm2ddl.auto" value="update" />
- </properties>
- </persistence-unit>
- </persistence>
-------------------------------------applicationContext.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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
- <bean id="defaultPersistenceUnitManager"
- class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
- <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
- <!-- comment dataSourceLooup to use jndi -->
- <property name="dataSourceLookup">
- <bean
- class="org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup" />
- </property>
- </bean>
- <!-- #########################PostgreSql####################### -->
- <tx:annotation-driven transaction-manager="postgreEntityManagerFactory"/>
- <bean id="postgreJpaVendor"
- class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
- <property name="showSql" value="true" />
- <property name="generateDdl" value="true" />
- </bean>
- <bean id="postgreDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="org.postgresql.Driver"/>
- <property name="url" value="..."/>
- <property name="username" value="..."/>
- <property name="password" value="..."/>
- <property name="initialSize" value="1"/>
- <property name="maxActive" value="100"/>
- <property name="maxIdle" value="8"/>
- <property name="minIdle" value="1"/>
- </bean>
- <bean id="postgreEntityManagerFactory"
- class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- <property name="persistenceUnitManager" ref="defaultPersistenceUnitManager" />
- <property name="persistenceUnitName" value="postgre" />
- <property name="jpaVendorAdapter" ref="postgreJpaVendor" />
- <property name="loadTimeWeaver">
- <bean
- class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
- </property>
- <property name="jpaDialect">
- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
- </bean>
- </property>
- </bean>
- <bean id="postgreTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="postgreEntityManagerFactory" />
- <qualifier value="postgreEM" />
- <property name="jpaDialect">
- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
- </property>
- </bean>
- <!--########################MySql################################# -->
- <tx:annotation-driven transaction-manager="mysqlEntityManagerFactory"/>
- <bean id="mysqlJpaVendor"
- class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
- <property name="showSql" value="true" />
- <property name="generateDdl" value="true" />
- </bean>
- <bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
- <property name="url" value="..."/>
- <property name="username" value="..."/>
- <property name="password" value="..."/>
- <property name="initialSize" value="1"/>
- <property name="maxActive" value="100"/>
- <property name="maxIdle" value="8"/>
- <property name="minIdle" value="1"/>
- </bean>
- <bean id="mysqlEntityManagerFactory"
- class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- <property name="persistenceUnitManager" ref="defaultPersistenceUnitManager" />
- <property name="persistenceUnitName" value="mysql" />
- <property name="jpaVendorAdapter" ref="mysqlJpaVendor" />
- <property name="loadTimeWeaver">
- <bean
- class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
- </property>
- <property name="jpaDialect">
- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
- </bean>
- </property>
- </bean>
- <bean id="mysqlTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="mysqlEntityManagerFactory" />
- <qualifier value="mysqlEM" />
- <property name="jpaDialect">
- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
- </property>
- </bean>
- </beans>
---------------------注解
-------------------------------------DAO-------------------------------------------------------------
------------MySqlBaseDaoImpl.java
- @Transactional(value="mysqlEM")
- public class MySqlBaseDaoImpl<T> implements IMySqlBaseDao<T>{
- @PersistenceContext(unitName="mysql")
- protected EntityManager em;
- //......
- }
------------PostgreSqlBaseDaoImpl.java
- @Transactional(value="postgreEM")
- public class PostgreSqlBaseDaoImpl<T> implements IPostgreSqlBaseDao<T>{
- @PersistenceContext(unitName="postgre")
- protected EntityManager em;
- //.......
- }
-------------------------------------Service-------------------------------------------------------------
---------------ExternalEnvironmentServiceImpl.java
- @Service
- @Transactional(value="mysqlEM")
- public class ExternalEnvironmentServiceImpl extends
- MySqlBaseDaoImpl<ExternalEnvironment> implements IExternalEnvironmentService {
- }
--------------------BdStorageCompanyServiceImpl.java
- @Service
- @Transactional(value="postgreEM")
- public class BdStorageCompanyServiceImpl extends PostgreSqlBaseDaoImpl<BdStorageCompany>
- implements IBdStorageCompanyService {
- }
-------------------------------------Model(Entity)-------------------------------------------------------------
-------------ExternalEnvironment.java---------------------------------------------------
- @Entity(name = "TBL_EXTERNALENVIRONMENT")
- public class ExternalEnvironment extends BaseModel{
- //......
- }
-----------------BdStorageCompany.java-------------------------------------------------
- @Entity(name = "TBL_BDSTORAGECOMPANY")
- public class BdStorageCompany extends BaseModel {
- //.......
- }
原文地址http://www.bieryun.com/2109.html