文章目录

  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. LeetCode(55): 跳跃游戏

    Medium! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...

  2. hdu1828 扫描线计算周长

    和扫描线计算面积差不多,新加了lbd,rbd线段树来标记区间的左右两侧是否被填充(左右边界是否存在),numbd线段树统计区间有多少边 /*数据弱不用离散化,但是要处理一下坐标*/ #include& ...

  3. cf276E 两棵线段树分别维护dfs序和bfs序,好题回头再做

    搞了一晚上,错了,以后回头再来看 /* 对于每次更新,先处理其儿子方向,再处理其父亲方向 处理父亲方向时无法达到根,那么直接更新 如果能达到根,那么到兄弟链中去更新,使用bfs序 最后,查询结点v的结 ...

  4. 通配符(WildCard)的使用

    一.关于WildCard:一个web应用,有成千上万个action声明,可以利用struts2提供的映射机制把多个彼此相似的映射关系简化成一个映射关系,即通配符. 1.新建类 ActionWildCa ...

  5. 步步为营-36-ADO.Net简介

    与数据库进行连接交互 方法一 #region 01连接对象 //01 连接字符串 string connstr = "server=.;uid=sa;pwd=sa;database=Demo ...

  6. System.getenv()和System.getProperty() 的区别

    1.System.getenv() 方法是获取指定的环境变量的值.它有两种方法,一种是接收参数为任意字符串,当存在指定环境变量时即返回环境变量的值,否则返回null.另外一种是不接受参数,那么返回的是 ...

  7. [转] Java基础知识——Java语言基础

    http://blog.csdn.net/loneswordman/article/details/9905931 http://blog.csdn.net/wanghuan203/article/d ...

  8. ahoi2009维护序列

    链接:https://www.luogu.org/problemnew/show/P2023 裸的线段树维护+* 代码: #include <bits/stdc++.h> using na ...

  9. \x 开头编码的数据解码成中文

    在python里,直接decode('utf-8')即可 >>> "\xE5\x85\x84\xE5\xBC\x9F\xE9\x9A\xBE\xE5\xBD\x93 \xE ...

  10. jquery 中remove()与detach()的区别

    remove()与detach()方法都是从dom中删除所有的元素 两者的共同之处在于都不会把匹配的元素从jQuery对象中删除. 不同之处在于用remove()删除的元素,除了元素被保留,其他的在这 ...