更新語句不報錯但數據不變,日誌顯示 SQL 中出現 title = null。
<if test="title != null"> -- 錯誤:MyBatis OGNL 表達式不需 #{}
title = #{title}
</if>
<if test="title != null"> -- 直接用屬性名
title = #{title}
</if>
test 屬性裡是 OGNL 表達式,直接引用參數對象屬性;#{} 只用於 SQL 參數佔位符,不能用在 test 中。
開啟 MyBatis 日誌 log4j.logger.org.apache.ibatis=DEBUG,觀察生成的完整 SQL 及參數值。