前面章节,在GuestbookLocalServiceImpl写了一个addGuestbook服务方法,但从来没有使用。为了留言簿具备完整功能,还须加上修改和删除留言簿的方法,以及返回留言簿数量。
添加留言服务方法
请记住,使用Service Builder时,您需要在*Impl类中定义您的服务。向*Impl类添加或删除方法,或更改*Impl类中方法的参数后,必须运行Service Builder。Service Builder自动更新受影响的接口及其他代码。
按照以下步骤添加所需的留言簿服务方法:
- Go to the
guestbook-serviceproject and openGuestbookLocalServiceImpl.javain thecom.liferay.docs.guestbook.service.implpackage. Add the following method for updating a guestbook:public Guestbook updateGuestbook(long userId, long guestbookId, String name, ServiceContext serviceContext) throws PortalException, SystemException { Date now = new Date(); validate(name); Guestbook guestbook = getGuestbook(guestbookId); User user = userLocalService.getUser(userId); guestbook.setUserId(userId); guestbook.setUserName(user.getFullName()); guestbook.setModifiedDate(serviceContext.getModifiedDate(now)); guestbook.setName(name); guestbook.setExpandoBridgeAttributes(serviceContext); guestbookPersistence.update(guestbook); return guestbook; }updateGuestbook方法通过ID 检索Guestbook,并替换为用户输入的数据,然后调用持久层以保存数据库。 - Next, add the following method for deleting a guestbook:
public Guestbook deleteGuestbook(long guestbookId, ServiceContext serviceContext) throws PortalException, SystemException { Guestbook guestbook = getGuestbook(guestbookId); List<Entry> entries = entryLocalService.getEntries( serviceContext.getScopeGroupId(), guestbookId); for (Entry entry : entries) { entryLocalService.deleteEntry(entry.getEntryId()); } guestbook = deleteGuestbook(guestbook); return guestbook; }重点考虑如果删除还有条目的留言簿会发生什么情况。如果您刚刚删除了留言簿,留言簿的条目仍然存在于数据库中,会成为孤儿数据。
deleteGuestbook在删除该留言簿之前,会调用服务以删除留言簿的条目。 - Use [CTRL]+[SHIFT]+O to update your imports, then save
GuestbookLocalServiceImpl.java. - In the Gradle Tasks pane on the right side in Liferay IDE, run Service Builder by opening the
guestbook-servicemodule and double-clickingbuildService.
现在已完成服务层修改,可以开始处理Guestbook Admin portlet自身了。
本文介绍如何为留言簿应用增加修改和删除留言簿的功能,并提供获取留言簿数量的方法。通过具体的代码示例,展示了如何更新及删除留言簿条目,确保数据库的一致性和完整性。
545

被折叠的 条评论
为什么被折叠?



