使用mybatis寫sql,需要動態更新對象數據,每次需要更新的字段不同,為了防止null空異常,就需要用動態sql了,
1
2
3
4
5
6
7
8
9
10
11
|
update s_user <trim prefix= "set" suffixOverrides= "," > < if test= "name!=null" >name=#{name},</ if > < if test= "age!=null" >age=#{age},</ if > < if test= "address!=null" >address=#{address},</ if > < if test= "createDate!=null" >createDate=#{createDate},</ if > </trim> where id=#{id} </update> |
注:trim標簽用來去掉sql中最后一個字段的逗號“,”。
以上所述是小編給大家介紹的使用MyBatis 動態update數據,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/u011490355/article/details/53282890