Liferay7开发文档_3.3.2生成模型,服务和持久层

持久层保存并检索模型数据。服务层是应用程序层和持久层之间的隔离区:可以替换不同的持久层实现,而不用修改除服务层调用之外的任何其他层。

为了建模留言簿和条目,将创建留言簿和条目模型类。但是不会直接用Java来做这件事。相反,应在Service Builder中定义,它将生成对象模型,并映射到Liferay Portal支持的所有SQL数据库。

应用程序的设计可创建多个留言簿,每个留言簿都包含不同的条目集。所有有权访问应用程序的用户都可以添加条目,但只有管理员可以添加留言簿。

首先创建实Guestbook体:

  1. In your guestbook-service project, open service.xml.
  2. When Liferay IDE generated your project, it filled this file with dummy entities, which you’ll replace. First replace the file’s opening contents (below the DOCTYPE) with the following code:
    <service-builder auto-namespace-tables="true" package-path="com.liferay.docs.guestbook">
        <author>liferay</author>
        <namespace>GB</namespace>
        <entity name="Guestbook" local-service="true" uuid="true">
    

    This defines the author, namespace, and the entity name. The namespace keeps the database field names from conflicting. The last tag is the opening tag for the Guestbook entity definition. In this tag, you enable local services for the entity, define its name, and specify that it should have a universally unique identifier (UUID).

  3. Next, replace the PK fields section:
    <column name="guestbookId" primary="true" type="long" />
    

    This defines guestbookId as the entity’s primary key, of the type long.

  4. The group instance can be left alone.
    <column name="groupId" type="long" />
    

    This defines the ID of the site in Liferay Portal that the entity instance belongs to (more on this in a moment).

  5. Leave the Audit Fields section alone. Add status fields:
    <!-- Status fields -->
    
    <column name="status" type="int" />
    <column name="statusByUserId" type="long" />
    <column name="statusByUserName" type="String" />
    <column name="statusDate" type="Date" />
    

    The Audit section defines Liferay Portal metadata. The companyId is the primary key of a portal instance. The userId is the primary key of a user. The createDate and modifiedDate store the respective dates on which the entity instance is created and modified. The Status section is used later to implement workflow.

  6. In the Other fields section, remove all the generated fields and put this one in their place:
    <column name="name" type="String" />
    
  7. Next, remove everything else from the Guestbook entity. Before the closing </entity> tag, add this finder definition:
        <finder name="GroupId" return-type="Collection">
            <finder-column name="groupId" />
        </finder>
    

    This defines a finder that generates a get method you’ll use to retrieve Guestbook entities. The fields used by the finder define the scope of the data retrieved. This finder gets all Guestbooks by their groupId, which corresponds to the site the application is on. This lets administrators put Guestbooks on multiple sites, and each Guestbook has its own data scoped to its site.

Guestbook实体现在完成。接下来,创建Entry实体:

  1. Add the opening entity tag:
    <entity name="Entry" local-service="true" uuid="true">
    

    As with the Guestbook entity, you enable local services, define the entity’s name, and specify that it should have a UUID.

  2. Add the tag to define the primary key and the groupId:
    <column name="entryId" primary="true" type="long" />
    
    <column name="groupId" type="long" />
    
  3. Add the audit fields as you did with the Guestbook entity:
    <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" />
    
  4. Add status fields like you did for the guestbook:
    <!-- Status fields -->
    
    <column name="status" type="int" />
    <column name="statusByUserId" type="long" />
    <column name="statusByUserName" type="String" />
    <column name="statusDate" type="Date" />
    
  5. Add the fields that define an Entry:
    <column name="name" type="String" />
    <column name="email" type="String" />
    <column name="message" type="String" />
    <column name="guestbookId" type="long" />
    

    The nameemail, and message fields comprise an Entry. These fields define the name of the person creating the entry, their email address, and the Guestbook message, respectively. The guestbookId is assigned automatically by code you’ll write, and is a Guestbook foreign key. This ties the Entry to a specific Guestbook.

  6. Add your finder and closing entity tag:
        <finder name="G_G" return-type="Collection">
            <finder-column name="groupId" />
            <finder-column name="guestbookId" />
        </finder>
    </entity>
    

    Here, you define a finder that gets guestbook entries by groupId and guestbookId. As before, the groupIdcorresponds to the site the application is on. The guestbookId defines the guestbook the entries come from. This finder returns a Collection of entries.

  7. Define your exception types outside the <entity> tags, just before the closing </service-builder> tag:
    <exceptions>
        <exception>EntryEmail</exception>
        <exception>EntryMessage</exception>
        <exception>EntryName</exception>
        <exception>GuestbookName</exception>
    </exceptions>
    

    These generate exception classes you’ll use later in try/catch statements.

  8. Save your service.xml file.

现在您已准备好运行Service Builder来生成模型,服务和持久层!

  1. In the Gradle Tasks pane on the right side of IDE, open guestbook-service → build.
  2. Run buildService by right-clicking it and selecting Run Gradle Tasks. Make sure you’re connected to the Internet, as Gradle downloads dependencies the first time you run it.
  3. In the Project Explorer, right-click the guestbook-service module and select Refresh. Repeat this step for the guestbook-api module. This ensures that the new classes and interfaces generated by Service Builder show up in IDE.
  4. In the Project Explorer, right-click the guestbook-service module and select Gradle → Refresh Gradle Project. Repeat this step for the guestbook-api module. This ensures that your modules’ Gradle dependencies are up to date.

Service Builder基于松散耦合的设计理念。它会生成三层应用程序:模型,服务和持久层。松耦合意味着您可以在模型层和服务层几乎不做任何更改的情况下更换持久层。该模型位于-api模块中,服务层和持久层位于-service模块中。

模型服务persistence.png

图1:模型,服务和持久层。

每一层都使用Java接口和这些接口的实现类来实现。Service Builder 生成一个代表您的模型的Entry类,而是生成系列类,包括一个Guestbook接口、一个Service Builder 管理的GuestbookBaseImpl抽象类,以及一个可以自定义的GuestbookImpl类。

  接下来,您将创建服务实现。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Liferay 使用手册是指Liferay平台的一份详细说明文档,用于指导用户如何正确安装、配置和使用Liferay平台的各项功能和特性。 Liferay是一种开源的企业门户解决方案,它提供了许多功能和工具,方便用户快速搭建和管理自己的网站、门户或应用程序。然而,对于不熟悉Liferay的用户来说,往往需要一个指导手册来帮助他们更好地理解和使用平台。 Liferay 使用手册通常会包含以下内容: 1. 安装和部署指南:介绍如何正确安装和部署Liferay平台,并提供相应的操作步骤和注意事项。 2. 配置和管理:详细说明如何进行Liferay平台的配置和管理,包括用户管理、权限设置、主题和布局等。 3. 内容管理:介绍如何创建和管理网站的内容,包括新闻、博客、论坛等功能,并提供相应的操作示例。 4. 社交协作:解释如何使用Liferay平台进行协作和合作,包括团队和项目管理、文档共享、日历和通知等。 5. 应用程序开发:指导开发人员如何使用Liferay平台进行应用程序的开发和集成,包括使用API和插件的方式。 6. 故障排除和支持:提供常见问题的解答和故障排除的方法,以及如何获取更多的支持和帮助。 总之,Liferay 使用手册是一份重要的指导文档,它可以帮助用户快速入门和掌握Liferay平台的使用方法,提高工作效率和用户体验。用户可以根据手册中提供的步骤和示例,了解和掌握各个功能的使用方式,从而更好地利用Liferay平台来满足自己的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值