一个完整的CRUD例子

[color=red][size=large]1.思想[/size][/color]
明确的MVC模式,把所有的方法声明代码实现都写在componentcontroller里,在view里声明方法去调就可以了。

[size=large][color=red]2.节点及类和方法的实现flightinfo(sflight类型):[/color][/size]
carrid
connid
flighttab(sflight类型):
carrid
connid
fldate
planetype
seatmax
seatsocc
isreadonly:
isread(boolean)
issave(boolean)

两个内表:(通过se11定义)
zit_bookings zit_flighttab

***************** 类zcl_net310_flightmodel: *************************************************************************

Attributes:
ty_flights static type sflight 航班
zit_outtab static type ty_sflight table of flights
zit_outbook static type zit_bookings 航班订票

methods:
方法名称 参数 参数类型 说明
read_flights i_carrid importing type sflight-carrid 航运承运人id 读取航班信息表
i_connid importing type sflight-connid 航班连接id
e_flights exporting type zit_flighttab 航班

read_bookings i_carrid importing type sflight-carrid 航运承运人id 读取某一航班的具体信息
i_connid importing type sflight-connid 航班连接id
i_fldate importing type sflight-fldate 航班日期
e_bookings exporting type zit_bookings 航班订票

insert_flights i_flights importing type zit_flighttab inter table 插入一条航班记录

delete sel_carrid importing type sflight-carrid 航运承运人id 删除一条航班记录
sel_connid importing type sflight-connid 航班连接id

modify_flights i_flights importing type zit_flighttab inter table 通过内表更新航班记录

所有方法均定义成static public
***************************************************************************************************************************

方法的具体实现:
method READ_FLIGHTS.
data i_lines type i.

if i_carrid = '' or i_connid = ''.
select * from sflight into table zit_outtab.
else.
select * from sflight into table zit_outtab where carrid = i_carrid and connid = i_connid.
endif.

e_flights = zit_outtab.

describe table e_flights lines i_lines.
if i_lines <= 0.
return.
endif.
endmethod.


method READ_BOOKINGS.
select * from sbook into table zit_outbook where carrid = i_carrid and connid = i_connid and fldate = i_fldate.
*select * from sbook into table zit_outbook.
e_bookings = zit_outbook.
endmethod.


method INSERT_FLIGHTS.
insert sflight from table i_flights ACCEPTING DUPLICATE KEYS.
endmethod.


method DELETE.
delete from sflight where carrid = sel_carrid and connid = sel_connid.
endmethod.

method MODIFY_FLIGHTS.
*update,不可以改变关键字段的值,但可以改变非主关键字段的值。
*modify覆盖具有主关键字的数据条目,找不到已存在相同主关键字的条目就将新行添加到数*据库表中。
update sflight from table i_flights.
endmethod.
[size=large][color=red]3.get_user_input[/color][/size]
method GET_USER_INPUT .
data: lr_node_flightinfo type ref to if_wd_context_node,
lr_elem_flightinfo type ref to if_wd_context_element,
ls_flightinfo type if_componentcontroller=>element_flightinfo.

* get user input
lr_node_flightinfo = wd_context->get_child_node(
name = if_componentcontroller=>wdctx_flightinfo
).
lr_elem_flightinfo = lr_node_flightinfo->get_element( ).
lr_elem_flightinfo->get_static_attributes(
importing
static_attributes = es_flightinfo
).
endmethod.
[color=red][size=large]4.get_flights[/size][/color]
method GET_FLIGHTS .
data: ls_flightinfo type sflight,
lt_flighttab type zit_flighttab,
lr_node_flighttab type ref to if_wd_context_node.

* get flightinfo key from context
wd_this->get_user_input(
importing
es_flightinfo = ls_flightinfo
).
* get related flighttab
CALL METHOD zcl_net310_flightmodel=>read_flights
exporting
i_carrid = ls_flightinfo-carrid
i_connid = ls_flightinfo-connid
importing
e_flights = lt_flighttab.
*store flighttab in context
lr_node_flighttab = wd_context->get_child_node(
name = if_componentcontroller=>wdctx_flighttab
).
lr_node_flighttab->bind_table( lt_flighttab ).
endmethod.
[size=large][color=red]5.handle_from_inputview[/color][/size]
在inputview的outbound里定义了一个参数actiontype,这里也要定义一个相同类型的参数actiontype接收,以区别是显示还是修改,实现页面的共用。
method HANDLEFROM_INPUT_VIEW .
data lo_nd_isreadonly type ref to if_wd_context_node.
data lo_el_isreadonly type ref to if_wd_context_element.
data ls_isreadonly type wd_this->element_isreadonly.
DATA lo_COMPONENTCONTROLLER TYPE REF TO IG_COMPONENTCONTROLLER .
lo_COMPONENTCONTROLLER = wd_this->get_componentcontroller_ctr( ).

lo_componentcontroller->get_flights(
).

* set the isreadonly
lo_nd_isreadonly = wd_context->get_child_node(
name = wd_this->wdctx_isreadonly
).
lo_el_isreadonly = lo_nd_isreadonly->get_element( ).
*lo_el_isreadonly->get_static_attributes(
* importing
* static_attributes = ls_isreadonly
*).

case actiontype.
when 'V'.
ls_isreadonly-isread = 'X'.
ls_isreadonly-issave = ''.
lo_el_isreadonly->set_static_attributes(
exporting
static_attributes = ls_isreadonly
).
when 'U'.
ls_isreadonly-isread = ''.
ls_isreadonly-issave = 'X'.
lo_el_isreadonly->set_static_attributes(
exporting
static_attributes = ls_isreadonly
).
endmethod.
[size=large][color=red]6.store_data[/color][/size]
method STORE_DATA .
data: lo_nd_flighttab type ref to if_wd_context_node,
lo_el_flighttab type ref to if_wd_context_element,
lt_flighttab type wd_this->elements_flighttab.
* get context node
lo_nd_flighttab = wd_context->get_child_node( name = wd_this->wdctx_flighttab ).
*
lo_nd_flighttab->get_static_attributes_table(
importing
table = lt_flighttab
).

* call the method <insert_flights>
call method zcl_net310_flightmodel=>insert_flights
exporting
i_flights = lt_flighttab.
endmethod.
[size=large][color=red]7.create_view_wddoinit[/color][/size]
method WDDOINIT .
DATA lo_nd_flighttab TYPE REF TO if_wd_context_node.
DATA lo_el_flighttab TYPE REF TO if_wd_context_element.
DATA ls_flighttab TYPE wd_this->element_flighttab.
data lt_flighttab type zit_flighttab.


* navigate from <CONTEXT> to <FLIGHTTAB> via lead selection
lo_nd_flighttab = wd_context->get_child_node( name = wd_this->wdctx_flighttab ).
lo_nd_flighttab->invalidate( ).
free lt_flighttab.
* @TODO handle not set lead selection
IF lo_nd_flighttab IS INITIAL.
ENDIF.

* get element via lead selection
lo_el_flighttab = lo_nd_flighttab->get_element( ).

* set attributes
do 5 times.
ls_flighttab-carrid = ''.
ls_flighttab-connid = ''.
ls_flighttab-fldate = ''.
ls_flighttab-planetype = ''.
ls_flighttab-seatsmax = ''.
ls_flighttab-seatsocc = ''.
append ls_flighttab to lt_flighttab.
enddo.
* @TODO handle not set lead selection
IF lo_el_flighttab IS not INITIAL.
lo_nd_flighttab->invalidate( ).

ENDIF.
* alternative access via index
* lo_el_flighttab = lo_nd_flighttab->get_element( index = 1 ).
* @TODO handle non existant child
* IF lo_el_flighttab IS INITIAL.
* ENDIF.

lo_nd_flighttab->bind_table( lt_flighttab ).

endmethod.
[size=large][color=red]8.delete_data[/color][/size]
method DELETE_DATA.
data: lo_nd_flighttab type ref to if_wd_context_node,
* lt_flighttab type wd_this->elements_flighttab,
lo_el_flighttab type ref to if_wd_context_element,
ls_flighttab type wd_this->element_flighttab.

lo_nd_flighttab = wd_context->get_child_node(
name = if_componentcontroller=>wdctx_flighttab
).
*lo_nd_flighttab->get_static_attributes_table(
* importing
* table = lt_flighttab
*).
lo_el_flighttab = lo_nd_flighttab->get_element( ) .
lo_el_flighttab->get_static_attributes(
importing
static_attributes = ls_flighttab
).
* calling the method delete in the class zcl_net310_flightmodel
call method zcl_net310_flightmodel=>delete
exporting
sel_carrid = ls_flighttab-carrid
sel_connid = ls_flighttab-connid.
endmethod.
[size=large][color=red]9.modify_flights[/color][/size]
method MODIFY_FLIGHTS .
data: lo_nd_flighttab type ref to if_wd_context_node,
lt_flighttab type wd_this->elements_flighttab.

* get the context node
lo_nd_flighttab = wd_context->get_child_node(
name = if_componentcontroller=>wdctx_flighttab
).

lo_nd_flighttab->get_static_attributes_table(
importing
table = lt_flighttab
).

call method zcl_net310_flightmodel=>modify_flights
exporting
i_flights = lt_flighttab.
endmethod.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值