This wiki does not contain official documentation and is currently deprecated and read only. Please try reading the documentation on the Liferay Developer Network, the new site dedicated to Liferay documentation. DISCOVER Build your web site, collaborate with your colleagues, manage your content, and more. DEVELOP Build applications that run inside Liferay, extend the features provided out of the box with Liferay's APIs. DISTRIBUTE Let the world know about your app by publishing it in Liferay's marketplace. PARTICIPATE Become a part of Liferay's community, meet other Liferay users, and get involved in the open source project. « Voltar para Development
Connect to a Database with Plugins SDK
Table of Contents [-]
Introduction #
This is possible because Liferay architecture allows it in its Plugins SDK.
Environment #
- Lifery-tomcat-6.0-5.2.5 Plugins SDK
- MySQL
Configuration to be Updated #
Create a portlet from the sample #
Include the following in conf/context.xml #
<Resource name="jdbc/TrainingPool" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/training?useUnicode=true&characterEncoding=UTF-8" username="lportal" password="lportal" maxActive="20" />
Write service.xml as follows and build service #
<?xml version="1.0" encoding="UTF-8"?> <!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="com.bc.od.store"> <author>Your Name</author> <namespace>OD</namespace> <entity name="Training" local-service="true" data-source="trainingDataSource" session-factory="trainingSessionFactory" tx-manager="trainingTransactionManager"> <column name="userId" type="long" primary="true"></column> <column name="dogName" type="String"></column> <column name="wifeName" type="String"></column> </entity> </service-builder>
Create database and run queries #
create database training character set utf8; grant all on training.* to 'lportal'@'localhost' identified by 'lportal' with grant option; grant all on training.* to 'lportal'@'localhost.localdomain' identified by 'lportal' with grant option; CREATE TABLE `training` ( `userId` bigint(20) NOT NULL DEFAULT '0', `dogName` varchar(75) DEFAULT NULL, `wifeName` varchar(75) DEFAULT NULL, PRIMARY KEY (`userId`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; insert into training.training values (1,"ada","betsy"); insert into training.training values (2,"cynthia","dorothy");
Update configuration files with listed content #
base-spring.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" > <bean id="beanReferenceAnnotationBeanPostProcessor" class="com.liferay.portal.spring.annotation.BeanReferenceAnnotationBeanPostProcessor" /> <aop:config> <aop:pointcut id="transactionOperation" expression="bean(*Service.impl)" /> <aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionOperation" /> </aop:config> <bean id="transactionAdvice" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="trainingTransactionManager" /> <property name="transactionAttributeSource"> <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"> <constructor-arg> <bean class="com.liferay.portal.spring.annotation.PortalTransactionAnnotationParser" /> </constructor-arg> </bean> </property> </bean> <bean id="portletBeanFactoryPostProcessor" class="com.liferay.portal.spring.context.PortletBeanFactoryPostProcessor" /> <bean id="portletClassLoader" class="com.liferay.portal.kernel.portlet.PortletClassLoaderUtil" factory-method="getClassLoader" /> <bean id="logAdvice" class="com.liferay.portal.spring.aop.LogAdvice" /> <bean id="velocityServiceInterceptor" class="com.liferay.portal.spring.aop.BeanInterceptor"> <property name="classLoader" ref="portletClassLoader" /> <property name="exceptionSafe" value="true" /> </bean> <bean id="baseVelocityService" abstract="true"> <property name="interceptorNames"> <list> <value>velocityServiceInterceptor</value> </list> </property> </bean> <bean id="baseModelExtensionAdvice" class="com.liferay.portal.spring.aop.ModelExtensionAdvice" abstract="true" /> <bean id="basePersistence" abstract="true"> <property name="dataSource" ref="trainingDataSource" /> <property name="sessionFactory" ref="trainingSessionFactory" /> </bean> </beans>
hibernate-spring.xml
<?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="trainingHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortalHibernateConfiguration" lazy-init="true">
<property name="dataSource">
<ref bean="trainingDataSource" />
</property>
<property name="mappingResources">
<list>
<value>META-INF/portlet-hbm.xml</value>
</list>
</property>
</bean>
<bean id="trainingSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl" lazy-init="true">
<property name="sessionFactoryImplementor">
<ref bean="trainingHibernateSessionFactory" />
</property>
</bean>
<bean id="trainingTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
<property name="dataSource">
<ref bean="trainingDataSource" />
</property>
<property name="sessionFactory">
<ref bean="trainingHibernateSessionFactory" />
</property>
</bean>
</beans>infrastructure-spring.xml
<?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="trainingDataSourceTarget" class="com.liferay.portal.spring.jndi.JndiObjectFactoryBean" lazy-init="true"> <property name="jndiName"> <value>jdbc/TrainingPool</value> </property> </bean> <bean id="trainingDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" lazy-init="true"> <property name="targetDataSource"> <ref bean="trainingDataSourceTarget" /> </property> </bean> </beans>
Deploy the portlet and test #
Add code in view.jsp as follows:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
This is the <b>Sample JSP Portlet</b>. Use this as a quick way to include JSPs.
<%
java.util.List<com.bc.od.store.model.Training> trainings = null;
com.bc.od.store.model.Training aTrain = null;
try{
trainings = com.bc.od.store.service.TrainingLocalServiceUtil.getTrainings(0, 2);
aTrain = com.bc.od.store.service.TrainingLocalServiceUtil.getTraining(1);
}
catch(Exception e){
e.printStackTrace();
}
if(trainings != null){
System.out.println("trainings are not null");
for(com.bc.od.store.model.Training t: trainings){
System.out.println("training: " + t.getWifeName());
}
}
if(aTrain != null){
System.out.println("train is not null: " + aTrain.getWifeName());
}
%>Future Improvements #
(1) Try using CounterUtil.java to get primary keys for tables in the separate database.
(2) The fields of the separate database table are not indexed. So Lucene search may not work.
(3) Tables are not automatically created in the separate database.
59410 Visualizações