liferay 7利用serviceBuilder来访问外部数据库

  1. 建一个serviceBuilder的module
  2. 修改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>
  1. 在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>
  1. 在portal-ext.properties中,写好数据库配置信息,如下:
include-and-override=portal-developer.properties
jdbc.ext.url=
jdbc.ext.driverClassName=oracle.jdbc.OracleDriver
jdbc.ext.username=
jdbc.ext.password=
  1. 在java->xxservice.persistence.impl下创建一个自定义的实现类,类名要求XXFinderImpl。
    实现类的类名必须是之前在service.xml的entity的name值,否则会无法生成XXFinder接口类
  2. 建好这个空的实现类之后,buildService,会自动生成XXFinder接口类。
  3. 在实现类中写一个空方法,暂不用写具体内容。buildService
  4. 在java->service.impl内的XXLocalServiceImpl类中,声明外部访问的方法。
    在这里插入图片描述
  5. 其中的内容如下,写完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);
    }
}
  1. 最后补全实现类中的实现方法。举例如下:
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;
    }
}

  1. 如上,则可以访问外部的数据库啦!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值