mapper层
/**
* 批量更新新库存
* @param list
* @return
*/
int updateNewStock(@Param(value = "list") List<GreenBeanMsg> list);
xml层sql写法
<update id="updateNewStock" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
UPDATE user
<set>
name=#{item.name},
age=#{item.age}
</set>
<where>
id = #{item.id}
</where>
</foreach>
</update>
这里需要注意两点:
- 传入的是一个集合,于是在dao层写的方法里加上@Param(value“list”),mybatis将传入的参数看做是一个集合list了。于是,在foreach中的collectio中就要写作“list”;parameterType也定义为"java.util.List"。
- 由于Mybatis映射文件中的sql语句默认是不支持以" ; " 结尾的,也就是不支持多条sql语句的执行,所以必须在配置连接数据库url后面带一个参数 &allowMultiQueries=true,表示允许批量操作,例 jdbc:mysql://localhost:3306/mysqlTest?characterEncoding=utf-8&allowMultiQueries=true。