需求:传入一行数据的ID及要修改的字段值,如果参数中没有某些字段值,则不修改此字段,仅修改要修改的字段
@Update({
"update table set City=#{City},County=#{County} where ID = #{ID}"
})
void updateData(DustData dustData);
当使用注解方式写SQL时,使用上面的方式,整个语句是写死的,必须有对应的参数,如果没有参数,在修改的时候会把这个字段置空,这并不是我们想要的,因此要使用动态SQL,SQL语句需要用标签包围,然后像XML语法一样书写即可,具体代码如下
@Update({
"<script>" +
"update table" +
"<set>" +
"<if test='City != null'>" +
"City=#{City}," +
"</if>" +
"<if test='County != null'>" +
"County=#{County}," +
"</if>" +
"</set>" +
"<where>" +
"ID = #{ID}" +
"</where>" +
"</script>"
})
void updateData(Data Data);