This is a step-by-step guide detailing how to connect to an external database in addition to the default "lportal" Liferay schema/database, and then generate services against that datasource using Liferay Service Builder. This is for Liferay 5.2.
#
# MySQL
#
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/lportal524?emulateLocators=true&useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=asdf
jdbc.test.driverClassName=com.mysql.jdbc.Driver
jdbc.test.url=jdbc:mysql://localhost/test?emulateLocators=true&useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.test.username=root
jdbc.test.password=asdf
Above I added another Mysql DB. You can change it to whatever other DB vendor. The point is, there are two.
<?xml version="1.0" encoding="UTF-8"?>
<beans
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-2.5.xsd"]
>
<bean id="testDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource">
<bean class="com.liferay.portal.dao.jdbc.util.DataSourceFactoryBean">
<property name="propertyPrefix" value="jdbc.test." />
</bean>
</property>
</bean>
<bean id="testHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortalHibernateConfiguration" lazy-init="true">
<property name="dataSource">
<ref bean="testDataSource" />
</property>
</bean>
<bean id="testSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl" lazy-init="true">
<property name="sessionFactoryImplementor">
<ref bean="testHibernateSessionFactory" />
</property>
</bean>
<bean id="testTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
<property name="dataSource">
<ref bean="testDataSource" />
</property>
<property name="sessionFactory">
<ref bean="testHibernateSessionFactory" />
</property>
</bean>
</beans>
I called mine, "test". Also notice the "jdbc.test."
##
## Spring
##
#
# Input a list of comma delimited Spring configurations. These will be
# loaded after the bean definitions specified in the contextConfigLocation
# parameter in web.xml.
#
spring.configs=\
META-INF/base-spring.xml,\
\
META-INF/hibernate-spring.xml,\
META-INF/infrastructure-spring.xml,\
META-INF/management-spring.xml,\
\
META-INF/util-spring.xml,\
\
META-INF/editor-spring.xml,\
META-INF/jcr-spring.xml,\
META-INF/messaging-spring.xml,\
META-INF/scheduler-spring.xml,\
META-INF/search-spring.xml,\
\
META-INF/counter-spring.xml,\
META-INF/document-library-spring.xml,\
META-INF/lock-spring.xml,\
META-INF/mail-spring.xml,\
META-INF/portal-spring.xml,\
META-INF/portlet-container-spring.xml,\
META-INF/wsrp-spring.xml,\
\
#META-INF/dynamic-data-source-spring.xml,\
#META-INF/shard-data-source-spring.xml,\
\
META-INF/ext-spring.xml,\
META-INF/infrastructure-spring-ext.xml
Added the last line to take infrastructure-spring-ext.xml into effect.
<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 5.2.0//EN" "[http://www.liferay.com/dtd/liferay-service-builder_5_2_0.dtd">]
<service-builder package-path="org.test">
<author>Tester</author>
<namespace>Test</namespace>
<entity name="test" table="foo" local-service="true" remote-service="false" data-source="testDataSource" session-factory="testSessionFactory" tx-manager="testTransactionManager">
<!-- PK fields -->
<column name="fooId" type="long" primary="true" />
<!-- Other fields -->
<column name="name" type="String" />
</entity>
<exceptions>
</exceptions>
</service-builder>
A very simple service.xml to an already existing DB table called "foo"
<target name="build-service-test">
<antcall target="build-service">
<param name="service.file" value="src/org/test/service.xml" />
</antcall>
</target>
Add the above as an ant task. It points to the location of the service.xml
package org.test.service.impl;
import java.util.List;
import org.test.model.test;
import org.test.service.base.testLocalServiceBaseImpl;
import org.test.service.persistence.testUtil;
import com.liferay.portal.SystemException;
public class testLocalServiceImpl extends testLocalServiceBaseImpl {
@SuppressWarnings("unchecked")
public List findAll()
throws SystemException {
List<test> list =
(List<test>) testUtil.findAll();
return list;
}
}
Add a simple method as shown above for something like findAll().
For example:
package org.test;
import java.util.List;
import org.test.model.test;
import com.liferay.portal.SystemException;
import org.test.service.testLocalServiceUtil;
public class testPortlet {
public static List findAll()
throws SystemException {
List<test> list =
(List<test>) testLocalServiceUtil.findAll();
return list;
}
}
The testLocalServiceUtil is the wrapper class that is called. We are calling the findAll() method we implemented earlier. We didn't have to make this dummy class, but it's just to demonstrate how to call the Util from Java.
Go to the doView method:
public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
List<test> a = null;
try {
a = testPortlet.findAll();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("--> Items in test table: "+a.size());
include(viewJSP, renderRequest, renderResponse);
}
It's very simple code that will output in the log. Obviously, you can do something more fancy, or from the JSP.
It should have pulled the data and given you the count.
0 Attachments | Average (1 Vote) ![]() ![]() ![]() ![]() |