02 持久层 - 客制化

文章目录

    • OverView
    • [0] Provision
    • [1] New Package
    • [2] Create Table
    • [3] Insert MockData
    • [4] Check Data


OverView

  • 创建 ABAP Package
  • 客制化底表
  • 向底表写入测试数据
  • 查看测试数据

[0] Provision

没有创建 BTP Trail User Account 的需先申请账号,并通过 Eclipse 连接到 BTP,具体内容可参见「00 - RAP 开发环境配置」

Booster - Abap Environment - Start

  • 如果提示 Error 缺失 Entitlement,点击进入 Subaccount 后点击左侧 Entitlements
  • 点击 Configure Entitlements 按钮,点击 Add Service Plans
  • 添加 ABAP environment
  • 然后再返回主控台尝试即可

下载 Service Key,用于 Eclipse 连接 BTP


[1] New Package

Project - New - ABAP Package

这里创建名为 ZTRAVEL_APP_10001 的 Package

P.S. Package 可以添加到 Favorite Packages 中,方便检索

02 持久层 - 客制化_第1张图片


[2] Create Table

Package - New - Other ABAP Repository Object

这里创建名为 ZTRAVEL_10001 的 Table

02 持久层 - 客制化_第2张图片

如下为 Table 的定义:

@EndUserText.label : 'Database table for travel data 10001'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table ztravel_10001 {

  key client      : abap.clnt not null;
  key mykey       : sysuuid_x16 not null;
  travel_id       : /dmo/travel_id;
  agency_id       : /dmo/agency_id;
  customer_id     : /dmo/customer_id;
  begin_date      : /dmo/begin_date;
  end_date        : /dmo/end_date;
  @Semantics.amount.currencyCode : 'ztravel_10001.currency_code'
  booking_fee     : /dmo/booking_fee;
  @Semantics.amount.currencyCode : 'ztravel_10001.currency_code'
  total_price     : /dmo/total_price;
  currency_code   : /dmo/currency_code;
  description     : /dmo/description;
  overall_status  : /dmo/overall_status;
  created_by      : syuname;
  created_at      : timestampl;
  last_changed_by : syuname;
  last_changed_at : timestampl;

}

保存并激活(快捷键与 GUI 端一致「Ctrl + S, Ctrl + F3」)


[3] Insert MockData

创建 Table 完成后,接下来就是写入几条测试数据,这里通过构造类来写入

Package - New - ABAP Class

这里创建名为 zcl_generate_travel_data_10001 的类,代码如下:

CLASS zcl_generate_travel_data_10001 DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS zcl_generate_travel_data_10001 IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.

    DATA itab TYPE TABLE OF ztravel_10001.

*   fill internal travel table (itab)
    itab = VALUE #(
      ( mykey = '02D5290E594C1EDA93815057FD946624' travel_id = '00000022' agency_id = '070001' customer_id = '000077' begin_date = '20190624' end_date = '20190628' booking_fee = '60.00' total_price =  '750.00' currency_code = 'USD'
        description = 'mv' overall_status = 'A' created_by = 'MUSTERMANN' created_at = '20190612133945.5960060' last_changed_by = 'MUSTERFRAU' last_changed_at = '20190702105400.3647680' )
      ( mykey = '02D5290E594C1EDA93815C50CD7AE62A' travel_id = '00000106' agency_id = '070005' customer_id = '000005' begin_date = '20190613' end_date = '20190716' booking_fee = '17.00' total_price = '650.00' currency_code = 'AFN'
        description = 'Enter your comments here' overall_status = 'A' created_by = 'MUSTERMANN' created_at = '20190613111129.2391370' last_changed_by = 'MUSTERMANN' last_changed_at = '20190711140753.1472620' )
      ( mykey = '02D5290E594C1EDA93858EED2DA2EB0B' travel_id = '00000103' agency_id = '070010' customer_id = '000011' begin_date = '20190610' end_date = '20190714' booking_fee = '17.00' total_price = '800.00' currency_code = 'AFN'
        description = 'Enter your comments here' overall_status = 'X' created_by = 'MUSTERFRAU' created_at = '20190613105654.4296640' last_changed_by = 'MUSTERFRAU' last_changed_at = '20190613111041.2251330' )
    ).

*   delete existing entries in the database table
    DELETE FROM ztravel_10001.

*   insert the new table entries
    INSERT ztravel_10001 FROM TABLE @itab.

*   output the result as a console message
    out->write( |{ sy-dbcnt } travel entries inserted successfully!| ).

  ENDMETHOD.
ENDCLASS.

Ctrl + F3 激活,F9 执行写入数据


[4] Check Data

切换到 Table 页签,F8 执行即可查看底表数据

02 持久层 - 客制化_第3张图片

至此,客制化底表已经完成。

你可能感兴趣的:(RAP,后端)