Liferay 有多重資料庫的連線功能,可以同時存取不同資料庫的資料表,以下說明;

 

1.建立portal-ext.properties  (\tomcat-7.0.27\webapps\ROOT\WEB-INF\classes)

portal-ext.properties內加入以下內容;

 

jdbc.default.url=jdbc\:mysql\://localhost/liferay612?useUnicode\=true&characterEncoding\=UTF-8&useFastDateParsing\=false

 

jdbc.default.driverClassName=com.mysql.jdbc.Driver

 

jdbc.default.username=root

 

jdbc.default.password=root

 

 

 

jdbc.test .driverClassName=com.mysql.jdbc.Driver

 

jdbc.test .url=jdbc:mysql://localhost/testE?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false

 

jdbc.test .username=root

 

jdbc.test .password=root

 

default就是預設的資料庫,test則是額外的資料庫,要存進testE這個資料庫的資料表要手動額外建立。

 

 

2.建立service.xml and build

 

<entity name="TableFromAnotherDataSource" table="TableFromAnotherDataSource"  local-service="true" remote-service="true" data-source="anotherDataSource" session-factory="anotherSessionFactory">

 

                <column name="testId" type="long" primary="true" />

 

                <column name="description" type="String" />

 

/entity>

 

 

3.建立ext-spring.xml  (文件路徑/ WEB-INF/src/META-INF/ext-spring.xml) and build

 

 

<?xml version="1.0"?>

 

<beans default-destroy-method="destroy" default-init-method="afterPropertiesSet"

 

        xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 

        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 

 

 

        <bean id="anotherDataSource"

 

                class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">

 

                <property name="targetDataSource" ref="anotherDataSourceWrapper" />

 

        </bean>

 

        <bean id="anotherDataSourceImpl"

 

                class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">

 

                <property name="propertyPrefix" value="jdbc.test." />

 

        </bean>

 

        <bean id="anotherDataSourceWrapper" class="com.liferay.portal.dao.jdbc.util.DataSourceWrapper">

 

                <constructor-arg ref="anotherDataSourceImpl" />

 

        </bean>

 

        <bean class="com.liferay.portal.dao.jdbc.util.DataSourceSwapper">

 

                <property name="liferayDataSourceWrapper" ref="anotherDataSourceWrapper" />

 

        </bean>

 

        <bean id="anotherHibernateSessionFactory" class="com.liferay.portal.kernel.spring.util.SpringFactoryUtil"

 

                factory-method="newBean">

 

                <constructor-arg

 

                        value="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration" />

 

                <constructor-arg>

 

                        <map>

 

                                <entry key="dataSource" value-ref="anotherDataSource" />

 

                        </map>

 

                </constructor-arg>

 

        </bean>

 

        <bean id="anotherSessionFactory" class="com.liferay.portal.kernel.spring.util.SpringFactoryUtil"

 

                factory-method="newBean">

 

                <constructor-arg

 

                        value="com.liferay.portal.dao.orm.hibernate.PortletSessionFactoryImpl" />

 

                <constructor-arg>

 

                        <map>

 

                                <entry key="dataSource" value-ref="anotherDataSource" />

 

                                <entry key="sessionFactoryClassLoader" value-ref="portletClassLoader" />

 

                                <entry key="sessionFactoryImplementor" value-ref="anotherHibernateSessionFactory" />

 

                        </map>

 

                </constructor-arg>

 

        </bean>

 

</beans>

 

都建立完成之後,在Java檔案之內只要用一般的方式就可以new出Impl並且進行存取的動作了;

 

 

TableFromAnotherDataSourceImpl  tableFromDefaultDataSource = new TableFromAnotherDataSourceImpl();

 

long testId = CounterLocalServiceUtil.increment(TableFromAnotherDataSourceImpl.class.getName());

 

tableFromDefaultDataSource.setTestId(testId);

 

tableFromDefaultDataSource.setDescription("hi");

TableFromAnotherDataSourceLocalServiceUtil.addTableFromAnotherDataSource(....

 

arrow
arrow
    全站熱搜

    squall75726 發表在 痞客邦 留言(0) 人氣()