Liferay service builder connects with other database

来源:互联网 发布:乔安官网软件下载 编辑:程序博客网 时间:2024/05/01 17:53

1. We need to do some change in our service.xml. Here, we need to specify our own data-source, session-factory, tx-manager. If we don't specify them, it won't work. Previously, I didn't do that, it always connects with Liferay database.


<?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.rujuan.book">
<author>Tina Xing</author>
<namespace>BookStore</namespace>
<entity name="Book" local-service="true" remote-service="false"
uuid="true"  data-source="myAppDataSource" session-factory="myAppSessionFactory" tx-manager="myAppTransactionManager">
<column name="bookid" type="long" primary="true" />
<column name="title" type="String" />
<column name="authorname" type="String" />
<column name="isbn" type="String" />
<column name="summary" type="String" />
<column name="status" type="int"></column>
<column name="statusByUserId" type="long"></column>
<column name="statusByUserName" type="String"></column>
<column name="statusDate" type="Date"></column>

<column name="companyId" type="long"></column>
<column name="groupId" type="long"></column>
<column name="userId" type="long"></column>


<!-- <reference package-path="com.liferay.portal" entity="User" />
<reference package-path="com.liferay.asset" entity="AssetEntry" />
<reference package-path="com.liferay.ratings" entity="RatingsStats" />
<reference package-path="com.liferay.portal" entity="Resource" />
<reference package-path="com.liferay.portal" entity="WorkflowInstanceLink" /> -->
</entity>
</service-builder>


2. write our own ext-spring.xml. In the file, we configure the second datasource, session factory etc. We put the file in /WEB-INF/src/META-INF which is the folder contains a lot of other spring,hibernate xml.


<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/jdbc.properties</value>
</property>
</bean>
<bean id="myAppDataSourceTarget"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="myAppDataSource"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource">
<ref bean="myAppDataSourceTarget" />
</property>
</bean>
<bean id="myAppHibernateSessionFactory"
class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
<property name="dataSource">
<ref bean="myAppDataSource" />
</property>
</bean>
<bean id="myAppSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl">
<property name="sessionFactoryImplementor">
<ref bean="myAppHibernateSessionFactory" />
</property>
</bean>
<bean id="myAppTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource">
<ref bean="myAppDataSource" />
</property>
<property name="sessionFactory">
<ref bean="myAppHibernateSessionFactory" />
</property>
</bean>
</beans>


3. Write jdbc.properties, put in WEB-INF folder


jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/liferay
jdbc.username=root
jdbc.password=root


4. make sure ext-spring.xml is configured in your service.properties, like this

    spring.configs=\
        WEB-INF/classes/META-INF/base-spring.xml,\
        \
        WEB-INF/classes/META-INF/hibernate-spring.xml,\
        WEB-INF/classes/META-INF/infrastructure-spring.xml,\
        \
        WEB-INF/classes/META-INF/cluster-spring.xml,\
        \
        WEB-INF/classes/META-INF/portlet-spring.xml,\
        \
        WEB-INF/classes/META-INF/dynamic-data-source-spring.xml,\
        WEB-INF/classes/META-INF/shard-data-source-spring.xml,\
        \
        WEB-INF/classes/META-INF/ext-spring.xml


All configurations are done. It's very easy right. But it took me a lot of time to configure. The reason is because of the first step. Good luck!


原创粉丝点击