Liferay開發學習Part6:Service Builder

一.什麼是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即爲表名;
local-service默認爲false,設爲true將自動生成本地接口;
remote-service默認爲false,設爲true將自動生成遠程接口。
<column name="title" type="String"/>
column配置表中的字段

2.生成Service

點擊按鈕即自動生成相應的目錄及文件

注意:生成了一大堆類,但,咱們只須要關注這4個類
1)BookLocalServiceImpl:這個類中寫咱們本身的業務邏輯,添加新的方法
2)BookLocalServiceUtile:經過該類調用方法
3)BookServiceImpl:安全檢測代碼
4)BookImpl:添加方法

3.實現本身的方法
BookLocalServiceImpl類中添加新方法
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
5.調用本身的方法
ThemeDisplay themeDisplay =(ThemeDisplay)actionRequest.getAttribute("THEME_DISPLAY"); 
User u = themeDisplay.getUser();//取得當前用戶
Book b = BookLocalServiceUtil.addBook(u.getUserId(), title);
相關文章
相關標籤/搜索