zoukankan      html  css  js  c++  java
  • jpa+spring配置多数据源

    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对象,表示多数据源配置成功,如下所示

  • 相关阅读:
    LODOP在页面不同位置输出页眉页脚
    【gridview增删改查】数据库查询后lodop打印
    LODOP中平铺图片 文本项Repeat
    Lodop设置文本项行间距、字间距
    Lodop打印条码二维码的一些设置
    LODOOP中的各种边距 打印项、整体偏移、可打区域、内部边距
    Lodop设置打印维护返回打印语句代码
    Lodop导出图片,导出单页内容的图片
    Lodop如何设置预览后导出带背景的图,打印不带背景图
    Lodop打印语句最基本结构介绍(什么是一个任务)
  • 原文地址:https://www.cnblogs.com/tian830937/p/5143543.html
Copyright © 2011-2022 走看看