By
wz2cool
更新日期:
项目地址:https://github.com/wz2cool/mybatis-dynamic-query
文档地址:https://wz2cool.gitbooks.io/mybatis-dynamic-query-zh-cn/content/
简介
删除和更新真的是非常的相似,主要是通过动态查询去定位我们要删除的数据,唯一不同是参数,update 传入的是一个实体,delete 传入的是一个实体的类。一个简单例子就可以说明用法。
准备工作
这里我们沿用简单筛选里面的准备工作即可。
开始删除
dao 中声明delete
1 2 3 4
| @Mapper public interface NorthwindDao { int delete(Map<String, Object> params); }
|
xml 中映射
1 2 3
| <delete id="delete" parameterType="java.util.Map"> ${deleteExpression} </delete>
|
通过动态查询定位我们要删除的数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Test public void testDelete() throws Exception { FilterDescriptor idFilter = new FilterDescriptor(FilterCondition.AND, Product.class, Product::getProductID, FilterOperator.EQUAL, 1);
ParamExpression paramExpression = mybatisQueryProvider.getDeleteExpression(Product.class, idFilter); Map<String, Object> updateParam = new HashMap<>(); updateParam.put("deleteExpression", paramExpression.getExpression()); updateParam.putAll(paramExpression.getParamMap());
int result = northwindDao.delete(updateParam); assertEquals(1, result); }
|
输出结果
1 2 3
| ==> Preparing: DELETE FROM product WHERE (product_id = ?) ==> Parameters: 1(String) <== Updates: 1
|
结束
删除基本上和更新一模一样,有了动态查询知识,我们就可以方便定位我们要删除的数据即可。
关注@我 ##
最后大家可以关注我和 Mybatis-Dynamic-query项目 ^_^
Follow @wz2cool Star Fork