mybatis级联删除

如有两个表:

--收货地址表:es_ShipAddress

create table es_shipAddress(

saId number(7) primary key,

receiveName varchar2(255) not null,

address varchar2(255) not null,

phone varchar2(15) not null,

customer_id number(7) references es_customer(customerId)

);

--订单表:es_orderForm

create table es_orderForm(

orderFormId number(12) primary key,

cost number(9,2) not null,

orderDate date not null,

shipAddress_id number(7) references es_ShipAddress(saId),

customer_id number(7) references es_customer(customerId)

);

收货地址表的主键在订单表里是外键。

一、在不改动表语句的前提下,要根据收货地址ID删除收货地址,可分成两条语句,先删除字表数据es_orderForm,再删除父表数据es_shipAddress:



    delete from es_orderForm where shipAddress_id=#{shipAddress_id}





    delete from es_shipAddress where saId=#{saId}

即持久层是两条语句共同完成一次操作,那么service层就调用两次mapper接口即可完成。

二、修改建表语句重新建子表

给子表在建立外键关联那可以加 on delete cascade 或者 on delete set null

--订单表:es_orderForm

create table es_orderForm(

orderFormId number(12) primary key,

cost number(9,2) not null,

orderDate date not null,

shipAddress_id number(7) references es_ShipAddress(saId),

customer_id number(7) references es_customer(customerId)

on delete cascade

);

删除时即可一同删除。

你可能感兴趣的:(db&orm,spring生态)