文章目录

  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. 基于Golang设计一套微服务架构[转]

      article- @嘟嘟噜- May/26/2018 18:35:30 如何基于Golang设计一套微服务架构 微服务(Microservices),这个近几年我们经常听到.那么现在市面上的的微服 ...

  2. nginx部署网站

    部署单个网站非常简单,只要将网站HTML文件和资源文件(.jpg .css .js等)全部复制到nginx-1.13.12\html目录下. 然后启动 启动进入cmd,切换到nginx-1.13.12 ...

  3. 【C++ Primer 第13章】6.对象移动

    右值引用 左值和右值 (1)两者区别: ①左值:能对表达式取地址.或具名对象/变量.一般指表达式结束后依然存在的持久对象. ②右值:不能对表达式取地址,或匿名对象.一般指表达式结束就不再存在的临时对象 ...

  4. nodejs模块——网络编程模块

    net模块提供了一个异步网络包装器,用于TCP网络编程,它包含了创建服务器和客户端的方法.dgram模块用于UDP网络编程. 参考链接:https://nodejs.org/api/net.html, ...

  5. FFT 【JSOI2012】bzoj4332 分零食 (未解决)

    很不错的一道倍增优化dp?? 第一次做这类题挺难想的 题目大意: 有n个小朋友,m块糖. 给小朋友分糖,如果一个小朋友分不到糖,那他后面的小朋友也分不到糖. 每个小朋友有一个喜悦值,有三个参数,O,S ...

  6. jquery中方法扩展 ($.fn & $.extend) 学习笔记

    A.$.fn 1.$.fn.method() 函数为jQuery对象扩展一个属性和方法(主要用于扩展方法) :method 为自定义方法名 ($.fn 等效 $.prototype) $.fn.bor ...

  7. 网络流24题 第五题 - PowerOJ1740 CodeVS1905 圆桌问题 二分图多重匹配 网络最大流

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - PowerOJ1740 - 有SPJ - 推荐 题目传送门 - CodeVS1905 - 无SPJ - 0% ...

  8. JsBom

    BOM 所谓BOM指的是浏览器对象模型 Browser Object Model,它的核心就是浏览器 Bom浏览器对象模型 1.bom输出 <!DOCTYPE html> <html ...

  9. 优化 --cache

    注意:配置成cache的地址段就不能再放入data and code,因为链接的时候,是不包含cache地址段的,如果想使用L1DSRAM或L1P SRAM,则应该相应减小cache段大小. 1:Ke ...

  10. IntelliJ IDEA(九) :插件(转)

    最近项目比较忙,很久没有更新IDEA系列了,今天介绍一下IDEA的一些炫酷的插件,IDEA强大的插件库,不仅能给我们带来一些开发的便捷,还能体现我们的与众不同. 0|11.插件的安装 打开settin ...