By
wz2cool
2017-07-31
更新日期:2025-11-30
项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynamic-query-zh-cn/content/
简介 更新和插入的问题其实是一样的,基本上我们可以解决方案也是类似的,唯一的不同就是,一般更新的时候我们都是带筛选条件的。常用我们都是通过ID筛选去找记录,但是现在有了前面的知识,这个筛选条件真的是so easy!!! 关于粒度的控制,同样可以使用@Column 中的 updateIfNull 标记来达到 updateSelective效果。 废话不多说上代码
准备工作 这里我们沿用简单筛选 里面的准备工作即可。
开始更新 update 和insert 一样,默认是null的时候我们不更新。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @Test public void testUpdate () throws Exception { FilterDescriptor idFilter = new FilterDescriptor (FilterCondition.AND, Product.class, Product::getProductID, FilterOperator.EQUAL, 1 ); Product newProduct = new Product (); String productName = "modifiedName" ; newProduct.setProductName(productName); ParamExpression paramExpression = mybatisQueryProvider.getUpdateExpression(newProduct, idFilter); Map<String, Object> updateParam = new HashMap <>(); updateParam.put("updateExpression" , paramExpression.getExpression()); updateParam.putAll(paramExpression.getParamMap()); int result = northwindDao.update(updateParam); assertEquals(1 , result); }
在XML写好与之对应的update
1 2 3 <update id ="update" parameterType ="java.util.Map" > ${updateExpression} </update >
输出结果我们可以看到,我们只更新了产品名称,并且是通过id 找到对应的记录。
1 2 3 ==> Preparing: UPDATE product SET `product_name`=? WHERE (product_id = ?) ==> Parameters: modifiedName(String), 1(String) <== Updates: 1
update Null 更新的时候默认是null的时候不更新,那么我们可以强制更新null的列。我们创建一个Product3实体类,唯一和Product不同就是在price列上加上了强制更新。
1 2 3 4 5 6 7 8 9 10 11 12 @Table(name = "product") public class Product3 { @Column(name = "product_id") private Integer productID; private String productName; @Column(updateIfNull = true) private BigDecimal price; private Integer categoryID; }
更新操作不变
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @Test public void testUpdateNull () throws Exception { FilterDescriptor idFilter = new FilterDescriptor (FilterCondition.AND, Product.class, Product::getProductID, FilterOperator.EQUAL, 1 ); Product3 newProduct = new Product3 (); String productName = "modifiedName" ; newProduct.setProductName(productName); ParamExpression paramExpression = mybatisQueryProvider.getUpdateExpression(newProduct, idFilter); Map<String, Object> updateParam = new HashMap <>(); updateParam.put("updateExpression" , paramExpression.getExpression()); updateParam.putAll(paramExpression.getParamMap()); int result = northwindDao.update(updateParam); assertEquals(1 , result); }
输出结果发现price被更新成为了null
1 2 3 ==> Preparing: UPDATE product SET `price`=?, `product_name`=? WHERE (product_id = ?) ==> Parameters: null, modifiedName(String), 1(String) <== Updates: 1
结束 更新和插入基本操作是一样的,唯一就是多了后面支持动态查询。
关注@我 ## 最后大家可以关注我和 Mybatis-Dynamic-query项目 ^_^Follow @wz2cool Star Fork