文章目录

  1. 1. 简介
  2. 2. 准备工作
  3. 3. 开始更新
    1. 3.1. update
    2. 3.2. update Null
  4. 4. 结束
  5. 5. 关注@我 

项目地址: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 {
// 查找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写好与之对应的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 {
// 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...
}

更新操作不变

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

Mybatis Dynamic Query 更新的更多相关文章

  1. Mybatis Dynamic Query 框架整合

    项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...

  2. Mybatis Dynamic Query 2.0 入门

    简介 2.0 昨天打包好了,主要是整合了tk.mybatis.mapper 到项目中去,所以和1.x比起来主要多了一个通用mapper.因为作者主要是使用springboot 这里讲一下Springb ...

  3. Mybatis Dynamic Query 2.0.2

    项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...

  4. Mybatis Dynamic Query 1.0.2版本

    项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...

  5. Mybatis Dynamic Query 简单筛选

    在框架中,筛选描述类有两种(FilterDescriptor, FilterGroupDescriptor),这里我们主要举例来说明FilterDescriptor用法. FilterDescript ...

  6. [Liferay6.2]Liferay Dynamic Query API示例

    介绍 Liferay提供了几种方法定义复杂的查询用来检索数据库中的数据. 通常情况下,在每个service Entity中,通过定义一些'finder'方法,可以便捷地满足基本的数据查询操作. 但是, ...

  7. mybatis执行批量更新update

    Mybatis的批量插入这里有http://ljhzzyx.blog.163.com/blog/static/38380312201353536375/.目前想批量更新,如果update的值是相同的话 ...

  8. mybatis 的批量更新操作sql

    转: mybatis 的批量更新操作sql 2018年07月23日 10:38:19 海力布 阅读数:1689   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...

  9. 170829、mybatis使用oracle和mybatis中批量更新

    一.mybatis执行批量更新batch update 的方法(mysql数据库) 1.数据库连接必须配置:&allowMultiQueries=true(切记一定要加上这个属性,否则会有问题 ...

随机推荐

  1. 转:vue+element实现树形组件

    项目中需要用到树形组件,在网上发现一个用vue+element实现的树形组件,现在记录下: demo地址:https://github.com/wilsonIs/vue-treeSelect

  2. CSS伪元素before、after妙用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. LeetCode(4):两个排序数组的中位数

    Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...

  4. each()遍历

    在<jQuery教程/理解选取更新范围>一节中,我们知道:当选择器返回了多个元素时,可以使用一个方法来更新所有的元素,不再需要使用循环. 然后有的时候需要遍历元素,怎么办? 使用each( ...

  5. HTML5——Data Url生成

    HTML5——Data Url生成 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  6. DDD领域模型查询方法实现(八)

    在DDD.Domain工程文件夹Repository下创建RequestPage类: public class RequestPage { public RequestPage(int pagesiz ...

  7. JQuery动画详解(四)

    一:基本动画show()显示隐藏的匹配元素.这个就是 'show( speed, [callback] )' 无动画的版本.如果选择的元素是可见的,这个方法将不会改变任何东西.无论这个元素是通过hid ...

  8. sql分组获取第一条或者最小一条值

    很多人都在问这个问题,现在记录一下 select id from test as a where id = (select min(b.id ) from test as b where a.pid ...

  9. Flink(二)CentOS7.5搭建Flink1.6.1分布式集群

    一. Flink的下载 安装包下载地址:http://flink.apache.org/downloads.html  ,选择对应Hadoop的Flink版本下载 [admin@node21 soft ...

  10. bzoj3687

    3687: 简单题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 700  Solved: 319[Submit][Status][Discuss] ...