比尔云BierYun--阿里云最新优惠活动
阿里云优惠码丨阿里云代金券

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

Spring3+Hibernate3(Jpa) 配置多个数据源的解决方案(基于注解)http://www.bieryun.com/2109.html

—————-配置文件——————————–

————————————-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. }

 

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

强烈推荐

高性能SSD云服务器ECS抗攻击,高可用云数据库RDS