Mybatis中批量处理

批量查询

<select id="listStudent" resultType="Student">
	SELECT <include refid="Base_Column_List"/>
	FROM student
	WHERE id IN
		<foreach collection="list" open"(" close=")" separator="," index="item">
			#{item}
		</foreach>
 </select>		

批量插入

<insert id="insertStudent" parameterType="List" keyProperty="Id" useGeneratedKeys="true"
keyColumn="id">
	INSERT INTO student
	(id,name,sex,address)
	VALUES
		<foreach collection="list" index="index" item="item" open="" close="" separator=",">
			( #{item.id},#{item.name},#{item.sex},#{item.address})
		</foreach>
  </insert>  

keyProperty=“Id”(主键) useGeneratedKeys=“true” keyColumn=“id” (主键)
写这个可以返回主键id

批量更新

<update id="updateStudent">
	UPDATE
		student
	SET
  	del_flag = 1
  WHERE id IN
  	<foreach collection="list" open="(" close=")" separator="," item="item">
  		#{item}
  	</foreach>	
</update>

批量删除

<delete id="deleteStudent" parameterType="List">
	DELETE FROM student
	WHERE id IN
		<foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
				#{item}
		</foreacn>
	</delete>	
发布了239 篇原创文章 · 获赞 66 · 访问量 14万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览