修复insertOrUpdate更新中条件字段没有移除问题

This commit is contained in:
Looly 2023-04-18 22:41:44 +08:00
parent ce1c0bab45
commit b79e8ced7e
3 changed files with 18 additions and 7 deletions

View File

@ -2,12 +2,13 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.19.M1 (2023-04-17)
# 5.8.19.M1 (2023-04-18)
### 🐣新特性
### 🐞Bug修复
* 【core 】 修复URLUtil.decode无法解码UTF-16问题issue#3063@Github
* 【db 】 修复insertOrUpdate更新中条件字段没有移除问题issue#I6W91Z@Gitee
-------------------------------------------------------------------------------------------------------------
# 5.8.18 (2023-04-16)

View File

@ -129,7 +129,9 @@ public class DialectRunner implements Serializable {
public int insertOrUpdate(Connection conn, Entity record, String... keys) throws SQLException {
final Entity where = record.filter(keys);
if (MapUtil.isNotEmpty(where) && count(conn, where) > 0) {
return update(conn, record, where);
// issue#I6W91Z
// 更新时给定的字段无需更新
return update(conn, record.removeNew(keys), where);
} else {
return insert(conn, record)[0];
}

View File

@ -3,6 +3,7 @@ package cn.hutool.db;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.func.Func0;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ReflectUtil;
@ -10,11 +11,7 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.db.sql.SqlUtil;
import java.nio.charset.Charset;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.RowId;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.*;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
@ -257,6 +254,17 @@ public class Entity extends Dict {
return result;
}
/**
* 过滤Map去除指定键值对如果键不存在跳过
*
* @param keys 键列表
* @return Dict 结果
* @since 5.8.19
*/
public Entity removeNew(final String... keys) {
return (Entity) MapUtil.removeAny(this.clone(), keys);
}
// -------------------------------------------------------------------- Put and Set start
@Override
public Entity set(String field, Object value) {