In the following post we will connect liferay with some third party database (legacy database):
Method 1:
1. Create Service.xml
2. Create ext-spring.xml file as /WEB-INF/src/META-INF/ext-spring.xml
You wont believe but yes we are done!!
Method 2:
1. Make an entry in portal-ext.properties file for another database:
2. create service.xml:
3. create ext-spring.xml file:
done!
NOTE: In both the above methods, we have to create the table manually in the database.
Method 1:
1. Create Service.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<entity name="Teacher" local-service="true" remote-service="true" data-source="myDatasource"> | |
<!-- PK fields --> | |
<column name="teacherId" type="long" primary="true" /> | |
<!-- Other fields --> | |
<column name="teacherName" type="String" /> | |
</entity> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:tx="http://www.springframework.org/schema/tx" 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-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/harish?useUnicode=true"/> | |
<property name="username" value="" /> | |
<property name="password" value="" /> | |
</bean> | |
<bean id="liferayHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration"> | |
<property name="dataSource" ref="myDatasource" /> | |
</bean> | |
</beans> |
Method 2:
1. Make an entry in portal-ext.properties file for another database:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jdbc.default.url=jdbc:mysql://localhost/lportal?useUnicode=true&characterEncoding=UTF-&useFastDateParsing=false | |
jdbc.default.driverClassName=com.mysql.jdbc.Driver | |
jdbc.default.username= | |
jdbc.default.password= | |
jdbc.test.driverClassName=com.mysql.jdbc.Driver | |
jdbc.test.url=jdbc:mysql://localhost/harish?useUnicode=true&characterEncoding=UTF-&useFastDateParsing=false | |
jdbc.test.username= | |
jdbc.test.password= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd"> | |
<service-builder package-path="com.harish"> | |
<author>harish.kumar</author> | |
<namespace>sample</namespace> | |
<entity name="Employee" local-service="true" remote-service="true" data-source="testDataSource" session-factory="testSessionFactory" tx-manager="testTransactionManager"> | |
<!-- PK fields --> | |
<column name="empId" type="long" primary="true" /> | |
<!-- other fields --> | |
<column name="name" type="String" /> | |
</entity> | |
</service-builder> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:tx="http://www.springframework.org/schema/tx" 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-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> | |
<aop:config> | |
<aop:pointcut id="transactionOperation" expression="bean(*Service.impl)" /> | |
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionOperation" /> | |
</aop:config> | |
<bean id="basePersistence" abstract="true"> | |
<property name="dataSource" ref="testDataSource" /> | |
<property name="sessionFactory" ref="testSessionFactory" /> | |
</bean> | |
<bean id="transactionAdvice" class="org.springframework.transaction.interceptor.TransactionInterceptor"> | |
<property name="transactionManager" ref="testTransactionManager" /> | |
<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="testHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration" lazy-init="true"> | |
<property name="dataSource" ref="testDataSource" /> | |
</bean> | |
<bean id="testSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl" lazy-init="true"> | |
<property name="sessionFactoryImplementor" ref="testHibernateSessionFactory" /> | |
</bean> | |
<bean id="testTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true"> | |
<property name="dataSource" ref="testDataSource" /> | |
<property name="globalRollbackOnParticipationFailure" value="false" /> | |
<property name="sessionFactory" ref="testHibernateSessionFactory" /> | |
</bean> | |
<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> | |
</beans> |
done!
NOTE: In both the above methods, we have to create the table manually in the database.
No comments:
Post a Comment