Mybatis Dynamic Query 更新


文章目錄

  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM