Mybatis框架-19:多表操作之一对多——删除操作

多表之间,要删除一个对象,首先要接触他们之间的关系

第一步:mapper

//根据客户id删除客户
void deleteCustomerById(Integer cust_id);

第二步:mapper.xml

客户的mapper.xml


        delete from `customer` where `cust_id`=#{cust_id};
    

订单的mapper.xml


    
        update `order` set  `orderCustId`= null where `orderCustId`=#{cust_id};
    

第三步:测试类

 @Test
    public void test9(){
        SqlSession session = MyUtils.openSession();
        CustomerMapper mapper = session.getMapper(CustomerMapper.class);
        OrderMapper orderMapper = session.getMapper(OrderMapper.class);
        //打破外键的联系
        orderMapper.updateRemoveRelationWithCustomer(33);
        //删除客户
        mapper.deleteCustomerById(33);
        session.commit();
        session.close();
    }

 

你可能感兴趣的:(Mybatis)