核心思想:
在原始SQL中嵌入if语句,类似于XML格式,如下
@Update("<script>update user set " +
"<if test = 'username != null'> username = #{username} ,</if>" +
"<if test = 'password != null'> password = #{password} ,</if>" +
"<if test = 'realName != null'> real_name = #{realName},</if>" +
"<if test = 'identitySign != null'> identity_sign= #{identitySign},</if>" +
"<if test = 'collegeName != null'>college_name= #{collegeName},</if>" +
"<if test = 'majorName != null'> major_name= #{majorName},</if>" +
"<if test = 'className != null'> class_name= #{className},</if>" +
"<if test = 'email != null'> email= #{email},</if>" +
"<if test = 'phone != null'> phone= #{phone},</if>" +
"<if test = 'operateIp != null'> operate_ip= #{operateIp} </if>" +
"where id = #{id} and del_flag = 0 </script>" )
void updateUser(User user);
传入的User对象,可能只包含更新某些字段,那么其它没有传入的字段意味着不更新。
如果不在SQL中判空,也不再代码中增加判空,那么没传进来的数据会被更新成NULL。
参考 https://blog.csdn.net/weixin_41637378/article/details/79397254