让Liferay的Service Builder连接其他数据库

2016年08月11日 Liferay 评论 2 条 阅读 17,324 views 次

在Liferay里面使用ServiceBuilder默认情况下连接的数据库是Liferay本身的数据库,在某些场景和需求下,我们可能需要连接另外的数据库或者数据源。ServiceBuilder默认情况下已经提供了相关的支持,需要做下简单的配置。

第一步,在service.xml里面为对应的实体指定对应的数据源等。在entity里面定义data-source、session-factory、tx-manager

在对应的entity里面添加如下配置,

data-source="sampleDataSource"

session-factory=“sampleSessionFactory”

tx-manager="sampleTransactionManager"

上面的名称可以自己定义,放在entity的属性里面。

第二步:在自己项目的META-INF里面创建ext-spring.xml文件

里面的内容如下,将下面的内容全部复制到ex-spring.xml里面,注意里面的id名称和第一步里面的保持一致。

<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
 <bean id="sampleDataSourceTarget" class="com.liferay.portal.dao.jdbc.util.DataSourceFactoryBean">
 <property name="propertyPrefix" value="jdbc.custom.default." />
 </bean>
 <bean id="sampleDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
 <property name="targetDataSource">
 <ref bean="sampleDataSourceTarget" />
 </property>
 </bean>
 <bean id="sampleHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
 <property name="dataSource">
 <ref bean="sampleDataSource" />
 </property>
 </bean>
 <bean id="sampleSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl">
 <property name="sessionFactoryImplementor">
 <ref bean="sampleHibernateSessionFactory" />
 </property>
 </bean>
 <bean id="sampleTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 <property name="dataSource">
 <ref bean="sampleDataSource" />
 </property>
 <property name="sessionFactory">
 <ref bean="sampleHibernateSessionFactory" />
 </property>
 </bean>
</beans>

第三步:配置相关数据源

可在第二步里面看到,我们指定的其他第三方的数据源是采用的如下代码:

 <property name="propertyPrefix" value="jdbc.custom.default." />

我们可以在portal-setup-wizard.properties或者是portal-ext.properties,里面配置其他数据库的连接信息。配置方法如下:

jdbc.custom.default.driverClassName=xxx

jdbc.custom.default.url=xxxx

jdbc.custom.default.username=xxx

jdbc.custom.default.password=xxx

也就是前缀保持和我们有在ext-spring.xml里面配置的一致即可。

说明:

ext-spring.xml里面的数据源还可以采用如下的配置方法:

<bean id="sampleDataSourceTarget" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${jdbc.sample.driverClassName}" />
  <property name="url" value="${jdbc.sample.url}" />
  <property name="username" value="${jdbc.sample.username}" />
  <property name="password" value="${jdbc.sample.password}" />
</bean>

也就是在这里指定各项的配置参数,我更推荐前面的方法。

第四步:和普通的serviceBuilder一样使用。

其他地方的使用就和在liferay同一个数据库里面的表是一样的用法了。但是这里需要注意的是,当我们连接其他第三方数据库时,serviceBuilder不会在第三方数据库里面执行自动建表的操作,需要我们手动的建立相关表

其他的查询等用法就没有区别了。

用户头像

2 条留言  访客:2 条  博主:0 条

  1. 启动报错class path resource [META-INF/portlet-hbm.xml] cannot be opened because it does not exist

  2. liferay 怎么和Activiti数据源结合呢 ?

给我留言

您必须 登录 才能发表留言!

Copyright © IT人生录 保留所有权利.   主题设计 知更鸟 滇ICP备16001547号

用户登录

分享到: