文章目录

  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. jquery----js/css 导入

    <script type"text/javascript" src="JS文件"></script> <link rel = &q ...

  2. gcd,lcm

    定理:gcd(a,b)*lcm(a,b)=a*b; 更相损减术:gcd(a,b)=gcd(b,a-b)=gcd(a,a-b) 欧几里得算法:gcd(a,b)=gcd(b,a mod b) 复杂度O(l ...

  3. 常见的MySQL Replication Error

    现在不少公司都在用MySQL(master)-->MySQL(slave)的框架,当然也有一主多从的架构,这也是MySQL主从的一个延伸架构;当然也有的公司MySQL主主的架构,MySQL主主架 ...

  4. 步步为营-62-Excel的导入和导出

    说明:NPOI组件的使用 1 添加引用 2 代码 using System; using System.Collections.Generic; using System.ComponentModel ...

  5. 步步为营-20-XML

    说明:可扩展标记语言 eXtensible Markup Language--区分大小写 涉及到的知识点:DOM 文档对象模型 文本文件存储数据缺点:1,不易读取.2,易乱码 1 通过代码创建一个xm ...

  6. DDD领域模型AutoMapper实现DTO(七)

    DTO的应用场景: 定义产品类: public class Product { public string ProductName { get; set; } public decimal Produ ...

  7. SqlServer基础语句练习(一)

    学了不少东西,感觉自己的sql语句还是很不好,从基础学起吧. 来一段sql脚本: create database tongji go use tongji go create table studen ...

  8. 【C++ Primer 第15章】定义派生类拷贝构造函数、赋值运算符

    学习资料 • 派生类的赋值运算符/赋值构造函数也必须处理它的基类成员的赋值 • C++ 基类构造函数带参数的继承方式及派生类的初始化 定义拷贝构造函数 [注意]对派生类进行拷贝构造时,如果想让基类的成 ...

  9. linux命令简写解释

    命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户rpm:redhat package manager 红帽子打包管 ...

  10. EL 表达式截取字符串/替换字符/……

    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 下面是 ...