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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值