建一个serviceBuilder的module 修改service.xml,修改结束后buildservice 。
<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_7_1_0.dtd">
<service-builder
package-path="ns.esys.portal.applicationext.service">
<namespace>VFCT</namespace>
*<entity local-service="true" name="VfcTServiceTimeDef"
remote-service="true" uuid="true" data-source="extDataSource"
session-factory="extSessionFactory" tx-manager="extTransactionManager">*
<column name="contextRoot" db-name="CONTEXT_ROOT" type="String"
primary="true" />
<column name="vcticeFlg" db-name="ACTIVE_FLG" type="long" />
<column name="startTm" db-name="START_TM" type="String" />
<column name="endTm" db-name="END_TM" type="String" />
*</entity>*
</service-builder>
在src->META-INF->spring下创建一个ext-db-spring.xml
<!--?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.xsd">
<!-- NOTE: Current restriction in LR7's handling of external data sources
requires us to redefine the liferayDataSource bean in our spring configuration.
The following beans define a new liferayDataSource based on the jdbc.ext.
prefix in portal-ext.properties. -->
<bean id="extDataSource"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource">
<ref bean="liferayDataSourceImpl" />
</property>
</bean>
<bean id="extHibernateSessionFactory"
class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
<property name="dataSource">
<ref bean="extDataSource" />
</property>
</bean>
<bean id="extSessionFactory"
class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl">
<property name="sessionFactoryImplementor">
<ref bean="extHibernateSessionFactory" />
</property>
</bean>
<bean id="extTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource">
<ref bean="extDataSource" />
</property>
<property name="sessionFactory">
<ref bean="extHibernateSessionFactory" />
</property>
</bean>
<bean
class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean"
id="liferayDataSourceImpl">
<property name="propertyPrefix" value="jdbc.ext.">
</property>
</bean>
</beans>
在portal-ext.properties中,写好数据库配置信息,如下:
include-and-override=portal-developer.properties
jdbc.ext.url=
jdbc.ext.driverClassName=oracle.jdbc.OracleDriver
jdbc.ext.username=
jdbc.ext.password=
在java->xxservice.persistence.impl下创建一个自定义的实现类,类名要求XXFinderImpl。 建好这个空的实现类之后,buildService ,会自动生成XXFinder接口类。 在实现类中写一个空方法,暂不用写具体内容。buildService 在java->service.impl内的XXLocalServiceImpl类中,声明外部访问的方法。 其中的内容如下,写完buildService 。
public class VgdTSysmstLocalServiceImpl extends VgdTSysmstLocalServiceBaseImpl {
/*
* NOTE FOR DEVELOPERS:
*
* Never reference this class directly. Always use {@link
* ns.esys.portal.applicationext.service.service.VgdTSysmstLocalServiceUtil} to
* access the vgd t sysmst local service.
*/
public Container getVgdTSysmstBySystemId(String SystemId) {
return vgdTSysmstFinder.getVgdTSysmstBySystemId(SystemId);
}
}
最后补全实现类中的实现方法。举例如下:
public class VgdTSysmstFinderImpl extends VgdTSysmstFinderBaseImpl implements VgdTSysmstFinder {
/**
*
* @param SystemId
* @return Container
*/
public Container getVgdTSysmstBySystemId(String SystemId) {
Session session = super.openSession();
SQLQuery queryObject = session.createSQLQuery(
"select vgd.STOP_FLG,vgd.SYSTEM_STOP_TY,vgd.SYSTEM_STOP_DTNM,vgd.SERVICE_FLG from VGD_T_SYSMST vgd where vgd.SYSTEM_ID=?");
queryObject.setCacheable(false);
//对应列名
queryObject.addScalar("STOP_FLG", Type.STRING);
queryObject.addScalar("SYSTEM_STOP_TY", Type.STRING);
queryObject.addScalar("SYSTEM_STOP_DTNM", Type.STRING);
queryObject.addScalar("SERVICE_FLG", Type.STRING);
QueryPos qPos = QueryPos.getInstance(queryObject);
qPos.add(SystemId);
List<?> resultList = QueryUtil.list(queryObject, getDialect(), -1, -1);
Iterator<?> ir = resultList.iterator();
while (ir.hasNext()) {
Object[] data = (Object[]) ir.next();
Container container = new Container();
container.setStopFlg(null == data[0] ? "" : data[0].toString());
container.setSystemStopTy(null == data[1] ? "" : data[1].toString());
container.setSystemStopDtnm(null == data[2] ? "" : data[2].toString());
container.setServiceFlg(null == data[3] ? "" : data[3].toString());
return container;
}
closeSession(session);
return null;
}
}
如上,则可以访问外部的数据库啦!!!