一.什麼是Service Builder?
Service Builder是liferay IDE提供的一種代碼生成方案,開發人員只需編輯一個數據庫實體的描述文件(XML),便可根據XML文件生成Spring層代碼、Hibernate層代碼、SQL、SQL索引建立文件、Spring和Hibernate的配置文件等,能夠提升開發效率
二.使用Service Builder
1.service.xml
java
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd"> <service-builder package-path="com.sample.portlet.liferay"> <author>laiwanshan</author> <namespace>Liferay</namespace> <entity name="Book" local-service="true" remote-service="true"> <!-- PK fields --> <column name="bookId" type="long" primary="true"/> <!-- Group instance --> <column name="groupId" type="long"/> <!-- Audit fields --> <column name="companyId" type="long"/> <column name="userId" type="long"/> <column name="userName" type="String"/> <column name="createDate" type="Date"/> <column name="modifiedDate" type="Date"/> <!-- Other fields --> <column name="title" type="String"/> </entity> </service-builder>配置說明:
<service-builder package-path="com.sample.portlet.liferay">依據這個配置生成目錄 WEB-INF/src/com/sample/portlet/library/
<namespace>Liferay</namespace>Table的前綴,表名爲namespace_tablename
<entity name="Book" local-service="true" remote-service="true">name即爲表名;
<column name="title" type="String"/>column配置表中的字段
public class BookLocalServiceImpl extends BookLocalServiceBaseImpl { public Book addBook(long userId,String title) throws PortalException,SystemException { User user = UserUtil.findByPrimaryKey(userId); Date now = new Date(); long bookId = CounterLocalServiceUtil.increment(Book.class.getName()); Book book = bookPersistence.create(bookId); book.setTitle(title); book.setCompanyId(user.getCompanyId()); book.setUserId(user.getUserId()); book.setCreateDate(now); book.setModifiedDate(now); return bookPersistence.update(book, false); } }4.從新生成Servcie,步驟同2
ThemeDisplay themeDisplay =(ThemeDisplay)actionRequest.getAttribute("THEME_DISPLAY"); User u = themeDisplay.getUser();//取得當前用戶 Book b = BookLocalServiceUtil.addBook(u.getUserId(), title);