文章目录java
- 1. 简介
- 2. 准备工做
- 3. 开始更新
- 3.1. update
- 3.2. update Null
- 4. 结束
- 5. 关注@我
项目地址:https://github.com/wz2cool/mybatis-dynamic-query
文档地址:https://wz2cool.gitbooks.io/mybatis-dynamic-query-zh-cn/content/git
简介
更新和插入的问题实际上是同样的,基本上咱们能够解决方案也是相似的,惟一的不一样就是,通常更新的时候咱们都是带筛选条件的。经常使用咱们都是经过ID筛选去找记录,可是如今有了前面的知识,这个筛选条件真的是so easy!!!
关于粒度的控制,一样可使用@Column 中的 updateIfNull 标记来达到 updateSelective效果。
废话很少说上代码github
准备工做
这里咱们沿用简单筛选里面的准备工做便可。mybatis
开始更新
update
和insert 同样,默认是null的时候咱们不更新。ide
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 { // 查找ID筛选 // 这里咱们使用表达式去设置propertyPath. FilterDescriptor idFilter = new FilterDescriptor(FilterCondition.AND, Product.class, Product::getProductID, FilterOperator.EQUAL, 1);
Product newProduct = new Product(); // only update product name // 这里咱们只更新产品名字,因此只设置产品名。 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写好与之对应的updatespa
1 2 3
|
<update id="update" parameterType="java.util.Map"> ${updateExpression} </update>
|
输出结果咱们能够看到,咱们只更新了产品名称,而且是经过id 找到对应的记录。code
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列上加上了强制更新。ip
1 2 3 4 5 6 7 8 9 10 11 12
|
@Table(name = "product") public class Product3 { // id 为null 的时候不插入 @Column(name = "product_id") private Integer productID; private String productName; // 强制更新price列,不管是否为null @Column(updateIfNull = true) private BigDecimal price; private Integer categoryID; // get/set... }
|
更新操做不变ci
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 { // 查找ID筛选 // 这里咱们使用表达式去设置propertyPath. FilterDescriptor idFilter = new FilterDescriptor(FilterCondition.AND, Product.class, Product::getProductID, FilterOperator.EQUAL, 1);
Product3 newProduct = new Product3(); // only update product name // 这里咱们只更新产品名字,因此只设置产品名。 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