在Spring Boot和MyBatis中,實現動態更新不固定字段的步驟如下:
方法一:使用MyBatis動態SQL(適合字段允許為null的場景)
-
定義實體類
包含所有可能被更新的字段。 -
Mapper接口
定義更新方法,參數為實體對象:java
void updateUserSelective(User user);
-
XML映射文件
使用<set>
和<if>
動態生成SQL:xml
<update id="updateUserSelective" parameterType="User">UPDATE user<set><if test="name != null">name = #{name},</if><if test="age != null">age = #{age},</if><if test="address != null">address = #{address},</if><if test="phone != null">phone = #{phone},</if></set>WHERE id = #{id} </update>
注意:此方法無法將字段更新為null
,因為參數為null
時條件不成立。
方法二:使用Map和字段過濾(支持字段更新為null)
-
Service層過濾字段
在Service中定義允許更新的字段,并過濾請求參數:java
public void updateUser(Long id, Map<String, Object> updates) {Set<String> allowedFields = Set.of("name", "age", "address", "phone");updates.keySet().retainAll(allowedFields); // 過濾非法字段userMapper.updateUserSelective(id, updates); }
-
Mapper接口
使用Map接收動態字段:java
void updateUserSelective(@Param("id") Long id, @Param("updates") Map<String, Object> updates);
-
XML映射文件
動態生成更新語句:xml
<update id="updateUserSelective">UPDATE user<set><foreach collection="updates" index="key" item="value" separator=",">${key} = #{value}</foreach></set>WHERE id = #{id} </update>
注意:使用${key}
存在SQL注入風險,需在Service層嚴格過濾字段名。
方法三:使用@UpdateProvider(靈活且安全)
-
定義SQL提供類
動態構建安全SQL:java
public class UserSqlProvider {public String updateSelective(Map<String, Object> params) {Long id = (Long) params.get("id");Map<String, Object> updates = (Map<String, Object>) params.get("updates");Set<String> allowedFields = Set.of("name", "age", "address", "phone");StringBuilder sql = new StringBuilder("UPDATE user SET ");allowedFields.forEach(field -> {if (updates.containsKey(field)) {sql.append(field).append(" = #{updates.").append(field).append("}, ");}});sql.setLength(sql.length() - 2); // 移除末尾逗號sql.append(" WHERE id = #{id}");return sql.toString();} }
-
Mapper接口
使用@UpdateProvider注解:java
@UpdateProvider(type = UserSqlProvider.class, method = "updateSelective") void updateUserSelective(@Param("id") Long id, @Param("updates") Map<String, Object> updates);
優點:避免SQL注入,動態生成安全語句。
總結
-
方法一適合簡單場景,字段無需設置為
null
。 -
方法二靈活,需嚴格過濾字段。
-
方法三推薦用于生產環境,安全且維護性強。
根據需求選擇合適方案,確保字段更新的靈活性和安全性。