Mybatis傳入List實現批量更新的示例代碼
Dao層寫法
/** * 批量更新新庫存 * @param list * @return */ int updateNewStock(@Param(value = 'list') List<GreenBeanMsg> list);
xml具體實現代碼
<update parameterType='java.util.List'> <foreach collection='list' item='bean' index='index' open='' close='' separator=';'> UPDATE green_beans <set>stock=#{bean.stock} </set> <where>beanUid = #{bean.beanUid} </where> </foreach> </update>
注意的地方:我傳入的是一個集合,于是在dao層寫的方法里加上@Param(value“list”),mybatis將傳入的參數看做是一個集合list了。于是,在foreach中的collectio中就要寫作“list”;parameterType也定義為'java.util.List'。
注意:這種方法必須在配置連接數據庫url后面帶一個參數 &allowMultiQueries=true,表示允許批量操作,例 jdbc:mysql://localhost:3306/mysqlTest?characterEncoding=utf-8&allowMultiQueries=true
到此這篇關于Mybatis傳入List實現批量更新的示例代碼的文章就介紹到這了,更多相關Mybatis傳入List批量更新內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: