点击图示 生成
然后点击上面图示去实现create 方法
refine
METHOD personset_create_entity.
*SUPER->PERSONSET_CREATE_ENTITY(
* EXPORTING
* IV_ENTITY_NAME = IV_ENTITY_NAME
* IV_ENTITY_SET_NAME = IV_ENTITY_SET_NAME
* IV_SOURCE_NAME = IV_SOURCE_NAME
* IT_KEY_TAB = IT_KEY_TAB
** io_tech_request_context = io_tech_request_context
* IT_NAVIGATION_PATH = IT_NAVIGATION_PATH
** io_data_provider = io_data_provider
** IMPORTING
** er_entity = er_entity
* ).
DATA:ls_requst_input_data TYPE zcl_ztest_demo_mpc=>ts_person,
ls_data_insert TYPE ztest_odata.
io_data_provider->read_entry_data( IMPORTING es_data = ls_requst_input_data ).
ls_data_insert-uname = ls_requst_input_data-uname .
ls_data_insert-phone = ls_requst_input_data-phone .
ls_data_insert-address = ls_requst_input_data-address.
INSERT ztest_odata FROM ls_data_insert .
IF sy-subrc EQ 0 .
er_entity = ls_requst_input_data .
ENDIF.
ENDMETHOD.
编写逻辑
METHOD personset_update_entity.
*SUPER->PERSONSET_UPDATE_ENTITY(
* EXPORTING
* IV_ENTITY_NAME = IV_ENTITY_NAME
* IV_ENTITY_SET_NAME = IV_ENTITY_SET_NAME
* IV_SOURCE_NAME = IV_SOURCE_NAME
* IT_KEY_TAB = IT_KEY_TAB
** io_tech_request_context = io_tech_request_context
* IT_NAVIGATION_PATH = IT_NAVIGATION_PATH
** io_data_provider = io_data_provider
** IMPORTING
** er_entity = er_entity
* ).
DATA:ls_requst_input_data TYPE zcl_ztest_demo_mpc=>ts_person.
io_data_provider->read_entry_data( IMPORTING es_data = ls_requst_input_data ).
UPDATE ztest_odata SET phone = ls_requst_input_data-phone
address = ls_requst_input_data-address
WHERE uname = ls_requst_input_data-uname .
IF sy-subrc EQ 0 .
er_entity = ls_requst_input_data .
ENDIF.
ENDMETHOD.
METHOD personset_delete_entity.
*SUPER->PERSONSET_DELETE_ENTITY(
* IV_ENTITY_NAME = IV_ENTITY_NAME
* IV_ENTITY_SET_NAME = IV_ENTITY_SET_NAME
* IV_SOURCE_NAME = IV_SOURCE_NAME
* IT_KEY_TAB = IT_KEY_TAB
** io_tech_request_context = io_tech_request_context
* IT_NAVIGATION_PATH = IT_NAVIGATION_PATH
* ).
DATA: ls_key TYPE /iwbep/s_mgw_name_value_pair,
lv_id TYPE /iwbep/s_mgw_name_value_pair-value.
DATA lv_flag TYPE c .
CLEAR ls_key .
READ TABLE it_key_tab INTO ls_key WITH KEY name = 'Uname' .
lv_id = ls_key-value.
CHECK lv_id IS NOT INITIAL .
DELETE FROM ztest_odata WHERE uname = lv_id .
IF sy-subrc NE 0.
lv_flag = 'X'.
ENDIF.
IF lv_flag EQ 'X'.
ROLLBACK WORK .
MESSAGE 'DELETE DATA FAILE' TYPE 'E' .
ELSE.
COMMIT WORK .
ENDIF.
ENDMETHOD.
METHOD personset_get_entity.
*SUPER->PERSONSET_GET_ENTITY(
* EXPORTING
* IV_ENTITY_NAME = IV_ENTITY_NAME
* IV_ENTITY_SET_NAME = IV_ENTITY_SET_NAME
* IV_SOURCE_NAME = IV_SOURCE_NAME
* IT_KEY_TAB = IT_KEY_TAB
** io_request_object = io_request_object
** io_tech_request_context = io_tech_request_context
* IT_NAVIGATION_PATH = IT_NAVIGATION_PATH
** IMPORTING
** er_entity = er_entity
** es_response_context = es_response_context
* ).
DATA: ls_key TYPE /iwbep/s_mgw_name_value_pair,
lv_value TYPE /iwbep/s_mgw_name_value_pair-value.
CLEAR ls_key .
READ TABLE it_key_tab INTO ls_key WITH KEY name = 'Uname' .
lv_value = ls_key-value.
IF lv_value IS NOT INITIAL.
SELECT SINGLE * FROM ztest_odata
INTO CORRESPONDING FIELDS OF er_entity
WHERE uname = lv_value.
ENDIF.
ENDMETHOD.
3.4 GET_ENTITYSET 实现 -> 查询多条数据
METHOD personset_get_entityset.
*SUPER->PERSONSET_GET_ENTITYSET(
* EXPORTING
* IV_ENTITY_NAME = IV_ENTITY_NAME
* IV_ENTITY_SET_NAME = IV_ENTITY_SET_NAME
* IV_SOURCE_NAME = IV_SOURCE_NAME
* IT_FILTER_SELECT_OPTIONS = IT_FILTER_SELECT_OPTIONS
* IS_PAGING = IS_PAGING
* IT_KEY_TAB = IT_KEY_TAB
* IT_NAVIGATION_PATH = IT_NAVIGATION_PATH
* IT_ORDER = IT_ORDER
* IV_FILTER_STRING = IV_FILTER_STRING
* IV_SEARCH_STRING = IV_SEARCH_STRING
** io_tech_request_context = io_tech_request_context
** IMPORTING
** et_entityset = et_entityset
** es_response_context = es_response_context
* ).
SELECT *
INTO CORRESPONDING FIELDS OF TABLE et_entityset
FROM ztest_odata .
ENDMETHOD.
sap/opu/odata/SAP/ZTEST_DEMO_SRV/personSet
选择post 方法
测试json
{
"Uname":"TEST",
"Phone":"12345678901",
"Address":"ztest"
}
执行后
跟创建类似,调用put 方法
/sap/opu/odata/SAP/ZTEST_DEMO_SRV/personSet('TEST')
测试jSON
{
"Uname":"TEST",
"Phone":"12345678901",
"Address":"ztest01"
}
执行成功后
/sap/opu/odata/SAP/ZTEST_DEMO_SRV/personSet(Uname='TEST')
如果多个key 的话可以这样输入
/sap/opu/odata/SAP/ZTEST_DEMO_SRV/personSet(Uname='TEST',Key2='',Key3='')
用逗号隔开
选择get 方法 输入 存在数据 uname = 'TEST'
返回结果
personSet('TEST')
2023-07-06T06:21:17Z
-
-
TEST
12345678901
ztest01
/sap/opu/odata/SAP/ZTEST_DEMO_SRV/personSet
直接get 获取输出结果
personSet('TEST')
2023-07-06T06:24:29Z
-
-
TEST
12345678901
ztest01
personSet('test')
2023-07-06T06:24:29Z
-
-
test
12345678901
ztest
可以查询出多条结果
也可以类似过滤查询单条数据
odata/SAP/ZTEST_DEMO_SRV/personSet?$filter=(Uname eq 'ZTEST')&$format=json
如果是多个key ,用逗号隔开
eg ?$filter=(Uname eq 'ZTEST',Key2 eq 'ZTEST',Key3 eq 'ZTEST')
查询单条数据需要在 PERSONSET_GET_ENTITYSET 写过滤逻辑,上面没写
需要加以下逻辑
METHOD personset_get_entityset.
*SUPER->PERSONSET_GET_ENTITYSET(
* EXPORTING
* IV_ENTITY_NAME = IV_ENTITY_NAME
* IV_ENTITY_SET_NAME = IV_ENTITY_SET_NAME
* IV_SOURCE_NAME = IV_SOURCE_NAME
* IT_FILTER_SELECT_OPTIONS = IT_FILTER_SELECT_OPTIONS
* IS_PAGING = IS_PAGING
* IT_KEY_TAB = IT_KEY_TAB
* IT_NAVIGATION_PATH = IT_NAVIGATION_PATH
* IT_ORDER = IT_ORDER
* IV_FILTER_STRING = IV_FILTER_STRING
* IV_SEARCH_STRING = IV_SEARCH_STRING
** io_tech_request_context = io_tech_request_context
** IMPORTING
** et_entityset = et_entityset
** es_response_context = es_response_context
* ).
DATA: lt_range_name TYPE RANGE OF ztest_odata-uname.
READ TABLE it_filter_select_options ASSIGNING FIELD-SYMBOL() WITH KEY property = |Uname|.
IF sy-subrc EQ 0.
LOOP AT -select_options ASSIGNING FIELD-SYMBOL().
IF -low IS NOT INITIAL.
APPEND INITIAL LINE TO lt_range_name ASSIGNING FIELD-SYMBOL().
-sign = -sign.
-option = -option.
-low = -low.
ENDIF.
ENDLOOP.
ENDIF.
SELECT *
INTO CORRESPONDING FIELDS OF TABLE et_entityset
FROM ztest_odata
WHERE uname IN lt_range_name.
ENDMETHOD.
5.3 测试 DELETE_ENTITY
选中DELETE 方法
/sap/opu/odata/SAP/ZTEST_DEMO_SRV/personSet(Uname='TEST')
执行