开发者社区> 推荐码发放> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

Spring3+Hibernate3(Jpa) 配置多个数据源的解决方案(基于注解)

简介: Spring3+Hibernate3(Jpa) 配置多个数据源的解决方案(基于注解) ----------------配置文件-------------------------------- -------------------------------------META-INF/persistence.
+关注继续查看

Spring3+Hibernate3(Jpa) 配置多个数据源的解决方案(基于注解)

----------------配置文件--------------------------------

-------------------------------------META-INF/persistence.xml------------------------------------------------------

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <persistence xmlns="http://java.sun.com/xml/ns/persistence"
  3.              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.              xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  5.              version="1.0">
  6.     <!--配置mysql数据源-->
  7.     <persistence-unit name="mysql" transaction-type="RESOURCE_LOCAL">
  8.         <provider>org.hibernate.ejb.HibernatePersistence</provider>
  9.         <non-jta-data-source>mysqlDataSource</non-jta-data-source>
  10.        <!--这里的class是指要储存于本数据库的Entity-->
  11.         <class>com.sunhope.model.core.ExternalEnvironment</class>
  12.         <properties>
  13.             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
  14.             <property name="hibernate.archive.autodetection" value="false"/>
  15.             <property name="hibernate.transaction.auto_close_session" value="false"/>
  16.             <property name="hibernate.hbm2ddl.auto" value="update" />
  17.         </properties>
  18.     </persistence-unit>
  19.     <!--配置postgre数据源-->
  20.     <persistence-unit name="postgre" transaction-type="RESOURCE_LOCAL">
  21.         <provider>org.hibernate.ejb.HibernatePersistence</provider>
  22.         <non-jta-data-source>postgreDataSource</non-jta-data-source>
  23.         <class>com.sunhope.model.baseinfo.BdStorageCompany</class>
  24.         <class>com.sunhope.model.baseinfo.BdHouseType</class>
  25.         <class>com.sunhope.model.baseinfo.BdStorage</class>
  26.         <class>com.sunhope.model.core.StoreEnvironment</class>
  27.         <properties>
  28.             <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
  29.             <property name="hibernate.archive.autodetection" value="false"/>
  30.             <property name="hibernate.transaction.auto_close_session" value="false"/>
  31.             <property name="hibernate.hbm2ddl.auto" value="update" />
  32.         </properties>
  33.     </persistence-unit>
  34. </persistence>

-------------------------------------applicationContext.xml---------------------------------------------------------

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:tx="http://www.springframework.org/schema/tx"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7.             http://www.springframework.org/schema/tx
  8.             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  9.     <bean id="defaultPersistenceUnitManager"
  10.         class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
  11.         <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
  12.         <!--  comment dataSourceLooup to use jndi -->
  13.         <property name="dataSourceLookup">
  14.             <bean
  15.                 class="org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup" />
  16.         </property>
  17.     </bean>
  18.     <!-- #########################PostgreSql####################### -->
  19.     <tx:annotation-driven transaction-manager="postgreEntityManagerFactory"/>
  20.     <bean id="postgreJpaVendor"
  21.         class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  22.          <property name="showSql" value="true" />
  23.          <property name="generateDdl" value="true" />
  24.     </bean>
  25.     <bean id="postgreDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  26.        <property name="driverClassName" value="org.postgresql.Driver"/>
  27.        <property name="url" value="..."/>
  28.        <property name="username" value="..."/>
  29.        <property name="password" value="..."/>
  30.         <property name="initialSize" value="1"/>
  31.         <property name="maxActive" value="100"/>
  32.         <property name="maxIdle" value="8"/>
  33.         <property name="minIdle" value="1"/>
  34.     </bean>
  35.     <bean id="postgreEntityManagerFactory"
  36.         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  37.         <property name="persistenceUnitManager" ref="defaultPersistenceUnitManager" />
  38.         <property name="persistenceUnitName" value="postgre" />
  39.         <property name="jpaVendorAdapter" ref="postgreJpaVendor" />
  40.         <property name="loadTimeWeaver">
  41.             <bean
  42.                 class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
  43.         </property>
  44.         <property name="jpaDialect">
  45.             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
  46.             </bean>
  47.         </property>
  48.     </bean>
  49.     <bean id="postgreTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  50.         <property name="entityManagerFactory" ref="postgreEntityManagerFactory" />
  51.         <qualifier value="postgreEM" />
  52.         <property name="jpaDialect">
  53.             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
  54.         </property>
  55.     </bean>
  56.     <!--########################MySql################################# -->
  57.     <tx:annotation-driven transaction-manager="mysqlEntityManagerFactory"/>
  58.     <bean id="mysqlJpaVendor"
  59.         class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  60.          <property name="showSql" value="true" />
  61.          <property name="generateDdl" value="true" />
  62.     </bean>
  63.     <bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  64.        <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
  65.        <property name="url" value="..."/>
  66.        <property name="username" value="..."/>
  67.        <property name="password" value="..."/>
  68.         <property name="initialSize" value="1"/>
  69.         <property name="maxActive" value="100"/>
  70.         <property name="maxIdle" value="8"/>
  71.         <property name="minIdle" value="1"/>
  72.     </bean>
  73.     <bean id="mysqlEntityManagerFactory"
  74.         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  75.         <property name="persistenceUnitManager" ref="defaultPersistenceUnitManager" />
  76.         <property name="persistenceUnitName" value="mysql" />
  77.         <property name="jpaVendorAdapter" ref="mysqlJpaVendor" />
  78.         <property name="loadTimeWeaver">
  79.             <bean
  80.                 class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
  81.         </property>
  82.         <property name="jpaDialect">
  83.             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
  84.             </bean>
  85.         </property>
  86.     </bean>
  87.     <bean id="mysqlTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  88.         <property name="entityManagerFactory" ref="mysqlEntityManagerFactory" />
  89.         <qualifier value="mysqlEM" />
  90.         <property name="jpaDialect">
  91.             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
  92.         </property>
  93.     </bean>
  94. </beans>

 

---------------------注解

-------------------------------------DAO-------------------------------------------------------------

------------MySqlBaseDaoImpl.java

[java] view plain copy

  1. @Transactional(value="mysqlEM")
  2. public class MySqlBaseDaoImpl<T> implements IMySqlBaseDao<T>{
  3.     @PersistenceContext(unitName="mysql")
  4.     protected EntityManager em;
  5.     //......
  6. }

------------PostgreSqlBaseDaoImpl.java

[java] view plain copy

  1. @Transactional(value="postgreEM")
  2. public class PostgreSqlBaseDaoImpl<T> implements IPostgreSqlBaseDao<T>{
  3.     @PersistenceContext(unitName="postgre")
  4.     protected EntityManager em;
  5.     //.......
  6. }

 

-------------------------------------Service-------------------------------------------------------------

---------------ExternalEnvironmentServiceImpl.java

[java] view plain copy

  1. @Service
  2. @Transactional(value="mysqlEM")
  3. public class ExternalEnvironmentServiceImpl extends
  4. MySqlBaseDaoImpl<ExternalEnvironment> implements IExternalEnvironmentService {
  5. }

--------------------BdStorageCompanyServiceImpl.java

[java] view plain copy

  1. @Service
  2. @Transactional(value="postgreEM")
  3. public class BdStorageCompanyServiceImpl extends PostgreSqlBaseDaoImpl<BdStorageCompany>
  4.         implements IBdStorageCompanyService {
  5. }

 

-------------------------------------Model(Entity)-------------------------------------------------------------

-------------ExternalEnvironment.java---------------------------------------------------

[java] view plain copy

  1. @Entity(name = "TBL_EXTERNALENVIRONMENT")
  2. public class ExternalEnvironment extends BaseModel{
  3.     //......
  4. }

-----------------BdStorageCompany.java-------------------------------------------------

[java] view plain copy

  1. @Entity(name = "TBL_BDSTORAGECOMPANY")
  2. public class BdStorageCompany extends BaseModel {
  3.    //.......
  4. }

 原文地址http://www.bieryun.com/2109.html

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Hibernate 一对多配置 级联操作(级联失败问题分析解决)
一方: package com.xdfstar.domain; import java.io.Serializable;import java.util.Date;import java.util.HashSet;import java.
959 0
基于IDEA 最新Spirng3.2+hibernate4+struts2.3 全注解配置 登录
原文 基于IDEA 最新Spirng3.2+hibernate4+struts2.3 全注解配置 登录 首先说说 IDEA 12,由于myeclipse越来越卡,我改用idea12 了,发现其功能强悍到eclipse无法比拟,此款ide理解上下文是一等一的,不用什么jquery插件,extjs插件,都可以实现全智能的代码提示。
1355 0
hibernate3 配置log4j
hibernate3 自带的默认的日志框架是slf4j,hibernate3的slf只是一个日志的接口,而hibernate3 自带默认的日志框架,在实际开发中很少有公司或者是项目中用到,这里记录一种使用log4j的日志框架来代替slf4j日志框架的实现,具体配置以及实现如下: 所用的jar包下载地址  http://download.csdn.net/detail/u013378306
1150 0
O/R MAPPING (HIBERNATE)方法小结
(一)对象之间的关系: 1.依赖: 依赖对象通过调用被依赖对象的方法来获得服务。一种比较松散的关系,并且是短期的。我们的过程与对象往往依赖于我们的实体域对象。如在struts 的 action中调用模型层的方法。
726 0
+关注
推荐码发放
阿里云优惠码阿里云推荐券bieryun.com
381
文章
5
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载