http://www.liferaysavvy.com/2013/08/liferay-plugin-portlet-connecting-to.html
我的非預設 database : Training
裡面的table : training
文件路徑D:\server\liferay-portal-6.1.2-ce-ga3\tomcat-7.0.40\webapps\ROOT\WEB-INF\classes
portal-ext.properties
=============================================
#mysql
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/lportal61spec?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=root
jdbc.one.driverClassName=com.mysql.jdbc.Driver
jdbc.one.url=jdbc:mysql://localhost/training?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.one.username=root
jdbc.one.password=root
shard.selector=com.liferay.portal.dao.shard.ManualShardSelector
shard.available.names=default,one
shard.default.name=default
jdbc.one = 待會在文件內會用到的識別子
training = 是想要連到的databaseName
portal-ext.properties 內容有更動 tomcat 需要重新啟動:
再來 在自己的project裡
先根據外部database 裡面的 table 欄位 做設定;
=====================================================
<service-builder package-path="com.bc.od.store">
<author>yourName</author>
<namespace>yourNS</namespace>
<entity name="Training" local-service="true" remote-service="false"
data-source="otherDatasource" session-factory="anotherSessionFactory">
<column name="userId" type="long" primary="true"></column>
<column name="dogName" type="String"></column>
<column name="wifeName" type="String"></column>
</entity>
</service-builder>
黃色字串 是要與下面那份文件 做 識別子;
build它===============
再來建立 ext-spring.xml 檔案
放在專案的 docroot/WEB-INF/src/META-INF 底下;
=====================================================
<?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="otherDatasource"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="otherDatasourceWrapper" />
</bean>
<bean id="otherDatasourceImpl"
class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
<property name="propertyPrefix" value="jdbc.one." />
</bean>
<bean id="otherDatasourceWrapper" class="com.liferay.portal.dao.jdbc.util.DataSourceWrapper">
<constructor-arg ref="otherDatasourceImpl" />
</bean>
<bean class="com.liferay.portal.dao.jdbc.util.DataSourceSwapper">
<property name="liferayDataSourceWrapper" ref="otherDatasourceWrapper" />
</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="otherDatasource" />
</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="otherDatasource" />
<entry key="sessionFactoryClassLoader" value-ref="portletClassLoader" />
<entry key="sessionFactoryImplementor" value-ref="anotherHibernateSessionFactory" />
</map>
</constructor-arg>
</bean>
</beans>
每個bean 的id 都有互相參考到... 字串不要打錯:
再來就可以在view.jsp 測試看看 可不可以對 外部 Training 的資料庫的 table training做CRUD
<%
List<Training> trainings= null;
Training training= null;
try {
trainings= TrainingLocalServiceUtil.getTrainings(-1, -1);
training= TrainingLocalServiceUtil
.createTraining(CounterLocalServiceUtil.increment());
training.setDogName("aaa");
training.setWifeName("bbb");
TrainingLocalServiceUtil.updateTraining(training);
} catch (Exception e) {
e.printStackTrace();
}
if (trainings != null ) {
for (Training m : trainings) {
out.print(m.getUserId() + "<br>");
}
}
%>
沒有留言:
張貼留言