JPA 配置多数据源

1. 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_2_0.xsd" version="2.0">
  <persistence-unit name="localdb" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
         <property name="hibernate.connection.driver_class" value="net.sourceforge.jtds.jdbc.Driver"/>
         <property name="hibernate.connection.username" value="sa"/>
         <property name="hibernate.connection.password" value="123"/>
         <property name="hibernate.connection.url" value="jdbc:jtds:sqlserver://localhost:1433/CollectionDB;instance="/>
         <property name="hibernate.max_fetch_depth" value="3"/>
         <property name="hibernate.format_sql" value="false" />
         <property name="hibernate.hbm2ddl.auto" value="none"/>
         <property name="javax.persistence.validation.mode" value="none"/>
         <!-- validate -->
      </properties>     
  </persistence-unit>
  <persistence-unit name="remotedb" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.password" value="123"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/futures?useUnicode=true&amp;characterEncoding=UTF-8"/>
         <property name="hibernate.max_fetch_depth" value="3"/>
         <property name="hibernate.format_sql" value="false" />
         <property name="hibernate.hbm2ddl.auto" value="none"/>
         <property name="javax.persistence.validation.mode" value="none"/>
      </properties>
  </persistence-unit>
</persistence>

2. spring-jpa-hibernate.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

	<!-- scan all beans and inject dependence -->
	<context:component-scan base-package="com.myproject" />
	<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>
	
	<!-- 整合localjpa -->
	<bean id="localEntityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceUnitManager" ref="defaultPersistenceUnitManager"></property>
		<property name="persistenceUnitName" value="localdb"></property>
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="showSql" value="false"></property>
				<property name="database" value="SQL_SERVER"></property>
			</bean>
		</property>
	</bean>
	<bean id="localtransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="localEntityManagerFactory" />
		<qualifier value="localEM" />
	</bean>
	<tx:annotation-driven transaction-manager="localtransactionManager"
		proxy-target-class="false" />
	
	<!-- 整合remotejpa -->
	<bean id="remoteEntityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceUnitManager" ref="defaultPersistenceUnitManager"></property>
		<property name="persistenceUnitName" value="remotedb"></property>
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="showSql" value="false"></property>
				<property name="database" value="MYSQL"></property>
			</bean>
		</property>
	</bean>
	<bean id="remotetransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="remoteEntityManagerFactory" />
		<qualifier value="remoteEM" />
	</bean>
	<tx:annotation-driven transaction-manager="remotetransactionManager"
		proxy-target-class="false" />
</beans>

3. StockAccountEntity.java

package com.myproject.example.vo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "stockaccount", schema = "")
@SuppressWarnings("serial")

public class StockAccountEntity implements java.io.Serializable {
	@Id
	@Column(name ="ID",nullable=false,length=40)
	private java.lang.String id;
	
	@Column(name ="USERNAME",nullable=true,length=40)
	private java.lang.String username;
	

	public java.lang.String getId(){
		return this.id;
	}


	public void setId(java.lang.String id){
		this.id = id;
	}

	
	public java.lang.String getUsername(){
		return this.username;
	}


	public void setUsername(java.lang.String username){
		this.username = username;
	}
}
扫描二维码关注公众号,回复: 827354 查看本文章

4. StockAccountDao.java

package com.myproject.example.dao;

import java.util.List;

import com.myproject.example.dao.common.MyCriteriaBuilder;
import com.myproject.example.vo.StockAccountEntity;

public interface StockAccountDao {
	public List<StockAccountEntity> query(MyCriteriaBuilder cb, int maxResult);
	
	public void update(StockAccountEntity entity);
	
	public void insert(StockAccountEntity entity);
	
	public void delete(StockAccountEntity entity);

	public StockAccountEntity queryById(int id);
}

5.StockAccountDaoBean.java

package com.myproject.example.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.myproject.example.dao.StockAccountDao;
import com.myproject.example.dao.common.MyCriteriaBuilder;
import com.myproject.example.vo.StockAccountEntity;


@Repository
public class StockAccountDaoBean implements StockAccountDao{
	
	@PersistenceContext(unitName="localdb")
	EntityManager em;
	
	@Override
	public List query(MyCriteriaBuilder cb, int maxResult) {
		String hql = "select a from StockAccountEntity a" + cb.getCriteriaString();
		Query q = em.createQuery(hql);
		for(String key : cb.getCriteriaParams().keySet()){
			q.setParameter(key, cb.getCriteriaParams().get(key));
		}
		if(maxResult!=0) 
			q.setMaxResults(maxResult);
		return q.getResultList();
	}
	
	@Override
	public void update(StockAccountEntity entity) {
		em.merge(entity);
	}
	
	@Override
	public void insert(StockAccountEntity entity) {
		em.persist(entity);
	}
	
	@Override
	public void delete(StockAccountEntity entity) {
		em.remove(entity);
	}

	@Override
	public StockAccountEntity queryById(int id) {
		return em.find(StockAccountEntity.class, id);
	}
}

6.StockAccountService.java

package com.myproject.example.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.myproject.example.dao.StockCashFlowDao;
import com.myproject.example.dao.common.MyCriteria;
import com.myproject.example.dao.common.MyCriteriaBuilder;
import com.myproject.example.vo.StockCashFlowEntity;

@Service
@Transactional(value="localEM")
public class StockAccountService {

	@Resource
	private StockAccountDao stockAccountDaoBean; 
	
	public List<StockAccountEntity> queryByTradecodeTradedate(String tradeCode, Date lastTradeDate, Date tradeDate){
		MyCriteriaBuilder accountCB = new MyCriteriaBuilder();
		cashFlowCB.put(MyCriteria.Type_Equil, "tradecode", tradeCode);
		cashFlowCB.put(MyCriteria.Type_Bigger, "tradedate", "tradedate1", lastTradeDate);
		cashFlowCB.put(MyCriteria.Type_SmallerEquil, "tradedate", "tradedate2", tradeDate);
		List<StockAccountEntity> cashFlowList =  stockAccountDaoBean.query(accountCB, 0);
		return accountList;
	}
}

猜你喜欢

转载自oracle-api.iteye.com/blog/2213667