jpa+spring配置多数据源
来源:互联网 发布:多媒体排序播放软件 编辑:程序博客网 时间:2024/06/07 09:46
采用jpa+spring技术配置多数据源
persistence.xml配置多数据源
<?xml version="1.0"?><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> <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="mysql" /> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit> <!--配置oracle数据源 --> <persistence-unit name="oracle" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" /> <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" /> <property name="hibernate.connection.username" value="test" /> <property name="hibernate.connection.password" value="test" /> <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit></persistence>
beans.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="mysql" /> </bean> <bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="oracle" /> </bean> <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager" /></beans>
PersonServiceTest测试类
package junit.test;import javax.annotation.Resource;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.Persistence;import javax.persistence.PersistenceContext;import org.junit.Test;/** * 测试多数据源配置是否成功 * * @author jiqinlin * */public class PersonServiceTest { //@PersistenceContext(unitName="mysql") //private EntityManager mysqlEm; //@PersistenceContext(unitName="oracle") //private EntityManager oracleEm; @Test public void test() { EntityManagerFactory mysql = Persistence.createEntityManagerFactory("mysql"); EntityManagerFactory oracle = Persistence.createEntityManagerFactory("oracle"); System.out.println(mysql + " : " + oracle); }}
运行时如果能打印出factory对象,表示多数据源配置成功,如下所示
0 0
- jpa+spring配置多数据源
- Spring JPA多数据源配置
- jpa+spring配置多数据源
- jpa+spring配置多数据源
- jpa+spring配置多数据源
- spring data jpa 配置多数据源
- Spring Boot + Spring Data JPA项目配置多数据源
- spring-boot 使用 spring-data-jpa多数据源配置
- Spring Boot,Spring Data JPA多数据源支持配置
- JPA多数据源配置
- JPA 配置多数据源
- Spring JPA Data 多数据源
- spring data jpa 多数据源
- spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置(二)+Druid连接池
- 基于Spring Boot与Spring Data JPA的多数据源配置
- Spring Boot学习(七)之Web应用使用Spring-data-jpa多数据源配置
- Spring Boot多数据源配置(一)durid、mysql、jpa整合
- Spring JPA 使用EntityManager时如何配置多数据源的事物管理
- 【android】onPrepareOptionsMenu 和onCreateOptionsMenu 的区别
- linux查看虚拟内存和cpu占用率
- RabbitMQ 安装配置与管理
- Java基础----Scanner
- Wax框架简明教程(1) 简介
- jpa+spring配置多数据源
- 大数据的四个典型特征
- 针对测试行业,新人的一些建议:我适合做测试吗?
- Xcode基本操作和一些快捷键(在CocoaChina上面找的)!供大家学习
- Wax框架简明教程(2)安装
- Java中this关键字的几种用法
- 浅述SATA接口Raid、AHCI、IDE三种模式
- 提示pcntl没找到
- DDR3内存技术参数