http://liferayatglance.blogspot.jp/2011/11/connecting-to-different-database-using.html
先建立service.xml
=====================================================
<service-builder package-path="com.bc.od.store">
<author>yourName</author>
<namespace>yourNS</namespace>
<entity name="Training" local-service="true" table="training" remote-service="false" data-source="otherDatasource">
<column name="userId" type="long" primary="true"></column>
<column name="firstName" type="String"></column>
<column name="lastName" type="String"></column>
</entity>
</service-builder>
entity name="Training" (物件名稱)
table="training" (資料庫table名稱)
data-source="otherDatasource" (底下那份文件的識別子)
build完後
再來建立 "ext-spring.xml" 放在 WEB-INF/src/META-INF dir底下
ext-spring.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"
default-destroy-method="destroy" default-init-method="afterPropertiesSet"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="otherDatasource" lazy-init="true"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost/training?useUnicode=true" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="liferayHibernateSessionFactory"
class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
<property name="dataSource" ref="otherDatasource" />
</bean>
</beans>
當中的training是 databaseName ;
再來就測試看看 能不能執行溜~~~~~
<%
java.util.List<com.bc.od.store.model.Training> trainings = null;
com.bc.od.store.model.Training aTrain = null;
try{
trainings = com.bc.od.store.service.TrainingLocalServiceUtil.getTrainings(0, 2);
aTrain = com.bc.od.store.service.TrainingLocalServiceUtil.getTraining(1);
}
catch(Exception e){
e.printStackTrace();
}
if(trainings != null){
System.out.println("trainings are not null");
for(com.bc.od.store.model.Training t: trainings){
System.out.println("training: " + t.getFirstName());
}
}
if(aTrain != null){
System.out.println("train is not null: " + aTrain.getLastName());
}
%>
console視窗應該會出現;
trainings are not null
training: aaa
training: bbb
train is not null: aaa
沒有留言:
張貼留言