目的:两个关联表,A表为主表,B表为次表,删除A表中一行内容时,连同B表中的关联内容一同删除。
思路:在B表中要写一个新的删除语句,在Mapper.xml中用SQL语句写出根据两表相同字段删除内容。然后在A表的controller层删除语句中添加调用之前的方法。(例如:相同字段为contractId)
我使用的框架是:springboot+thymeleaf
B表dao层:
public int deleteItemByContractIds(String[] contractIds);
B表service层:
public int deleteItemByContractIds(String ids);
B表service.impl层:
@Override
public int deleteItemByContractIds(String ids) {
return contractItemMapper.deleteItemByContractIds(Convert.toStrArray(ids));
}
B表Mapper.xml:
<delete id="deleteItemByContractIds" parameterType="String">
delete from fd_contract_item where contract_id in
<foreach item="contractId" collection="array" open="(" separator="," close=")">
#{contractId}
</foreach>
</delete>
A表controller层:
public AjaxResult remove(String ids)
{
contractItemService.deleteItemByContractIds(ids);//就是这句话,调用B表中的内容
return toAjax(contractService.deleteContractByIds(ids));
}