Java Spring JPA 應用在多資料庫

在過年前, 終於搞定了JPA 的多資料庫設定,
不曉得是我的 查詢 Keyword不對, 還是 SPring JAP 的文章本來就屬於異纇,
請出 Google總是沒找到想找到的, 幾乎每篇都過濾後, 終於看到了有一篇 而且還不是英文的!…Orz
http://www.guj.com.br/posts/list/64311.java  放心不是火星文!!=.=|||
我整理之後, Post 在以下部分, 方便我的粉絲們參考….@_@
 

applicationContext.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans&quot;

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;

        xmlns:aop="http://www.springframework.org/schema/aop&quot;

        xmlns:tx="http://www.springframework.org/schema/tx&quot;

        xsi:schemaLocation="

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"&gt;

 

        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

 

        <!– DataSource MySQL –>

        <bean id="dataSourceMySQL"

                class="org.apache.commons.dbcp.BasicDataSource"

                destroy-method="close"

                scope="singleton">

                <property name="driverClassName" value="com.mysql.jdbc.Driver" />

                <property name="url" value="jdbc:mysql://localhost/banco1" />

                <property name="username" value="root" />

                <property name="password" value="senha" />

        </bean>

 

        <!– DataSource PostgreSQL –>

        <bean id="dataSourcePgSQL"

                class="org.apache.commons.dbcp.BasicDataSource"

                destroy-method="close"

                scope="singleton">

                <property name="driverClassName" value="org.postgresql.Driver" />

                <property name="url" value="jdbc:postgresql://localhost/banco2" />

                <property name="username" value="postgres" />

                <property name="password" value="senha" />

        </bean>

 

        <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">

                <property name="persistenceXmlLocations">

                        <list>

                                <value>classpath*:META-INF/persistence-mysql.xml</value>

                                <value>classpath*:META-INF/persistence-pgsql.xml</value>

                        </list>

                </property>

                <property name="dataSources">

                        <map>

                                <entry key="localDataSource" value-ref="dataSourceMySQL"/>

                                <entry key="remoteDataSource" value-ref="dataSourcePgSQL"/>

                        </map>

                </property>

                <property name="defaultDataSource" ref="dataSourceMySQL"/>

        </bean>

 

        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

                <property name="persistenceUnitManager" ref="persistenceUnitManager"/>

                <property name="persistenceUnitName" value="puMySQL"/>

        </bean>

 

        <bean id="transactionManager"

                class="org.springframework.orm.jpa.JpaTransactionManager">

                <property name="entityManagerFactory" ref="entityManagerFactory" />

        </bean>

 

        <tx:annotation-driven transaction-manager="transactionManager" />

 

        <bean id="userDao" class="modelo.dao.UserDao" />

        <bean id="mapaDao" class="modelo.dao.MapaDao" />

</beans>

 

 

persistence-mysql.xml

 

<persistence xmlns="http://java.sun.com/xml/ns/persistence&quot;

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;

        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd&quot;

        version="1.0">

        <persistence-unit name="puMySQL">

                <provider>org.hibernate.ejb.HibernatePersistence</provider>

                <non-jta-data-source>localDataSource</non-jta-data-source>

                <properties>

                        <property name="default" value="true" />

                        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

                </properties>

        </persistence-unit>

</persistence>

 

persistence-pgsql.xml

 

<persistence xmlns="http://java.sun.com/xml/ns/persistence&quot;

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;

        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd&quot;

        version="1.0">

        <persistence-unit name="puPgSQL">

                <provider>org.hibernate.ejb.HibernatePersistence</provider>

                <non-jta-data-source>remoteDataSource</non-jta-data-source>

                <properties>

                        <property name="default" value="false" />

                        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>

                </properties>

        </persistence-unit>

</persistence>

 

UserDao.java

 

@Repository

@Transactional

public abstract class UserDao implements Dao<User> {

       

        private static final Log logger = LogFactory.getLog(GenericDaoImpl.class);

       

        private EntityManager em;

 

    @PersistenceContext(name = "puMySQL")

    public void setEntityManager(EntityManager em) {

        this.em = em;

    }

 

        // outros metodos

}

 

MapaDao.java

 

@Repository

@Transactional

public abstract class MapaDao implements Dao<Mapa> {

       

        private static final Log logger = LogFactory.getLog(GenericDaoPgImpl.class);

       

        private EntityManager em;

 

    @PersistenceContext(name = "puPgSQL")

    public void setEntityManager(EntityManager em) {

        this.em = em;

    }

 

        // outros metodos

}

 

PersistenceUnitsTest.java

 

public class PersistenceUnitsTest extends AbstractTransactionalSpringContextTests {

       

        protected UserDao userDao;

       

        protected MapaDao mapaDao;

       

        public void setUserDao(UserDao userDao) {

                this.userDao = userDao;

        }

 

        public void setMapaDao(MapaDao mapaDao) {

                this.mapaDao = mapaDao;

        }

 

        @Override

        protected String[] getConfigLocations() {

                setDependencyCheck(false);

                setAutowireMode(AUTOWIRE_BY_NAME);

                return new String[] {"applicationContext.xml"};

        }

 

        @Test

        public void testPersistenceUnits() {

                assertNotNull(this.userDao.findAll());

                assertNotNull(this.mapaDao.findAll());

        }

}

 

廣告

About fenjj

Perfect !!??...
本篇發表於 Uncategorized。將永久鏈結加入書籤。

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s