Saturday 12 November 2011

Connect to different database through service.xml. Liferay6

Suppose you want to access datas from another database using liferay service.ml.
Then you can do this by the following steps.
  1. Create Service.xml
  2. Create ext-spring.xml file path /WEB-INF/src/META-INF/ext-spring.xml
Service.xml entry looks like this
<service-builder package-path=”com.test”>
    <author>satya</author>
<namespace>test</namespace>
<entity name=”competency” local-service=”true” table=”competency” remote-service=”true” data-source=”myDatasource”>
<column name=”userId” type=”long” primary=”true”></column>
<column name=”firstName” type=”String”></column>
<column name=”lastName” type=”String”></column>
</entity></service-builder>
Run Build-service
Here we are telling the service.xml that this entity will use the userdefined datasource “myDatasource” instead of pointing it into liferay defaultdatasource.
this entry has to be created in the ext-spring.xml file
<?xml version=”1.0″?>
<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=”myDatasource” 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=”myDatasource” />
</bean>
</beans>
Deploy the portlet
Now your portlet will fetch datas from different database table through the liferay service API.

No comments:

Post a Comment