我正在从对象对表进行jdbc更新。该对象有几个属性,我只想用具有值的对象更新我的表。问题是下面的更新语句将擦除所有省略的值,如果它们不在对象中。
例如:
String statementStr = "update users "
+ "set firstName = ?, lastName = ?, email = ?, birthday = ?, "
+ "gender = ?"
+ "where id = ?;";
如何修改此语句以仅更新对象的填充值?
答案 0 :(得分:1)
如果我理解正确,那么您可以使用ISNULL()
仅使用不可为空的属性更新字段,否则请保留该字段而不更新它:
UPDATE users
SET firstName = ISNULL(@nameParam, firstName),
lastName = ISNULL(@lastNameParam, lastName),
email = ISNULL(@emailParam, email),
...
WHERE id = ?;