mybatis 批量操作大全

本文概述了批量插入、查询、更新和删除操作在数据库中的应用,涉及SQL语句,包括分组排序、使用foreach遍历和参数化查询。讨论了针对不同场景的Java和前端操作,如班级学生筛选和文章分类更新。

批量插入

<!--批量插入-->
 	<insert id="addBatch">
		INSERT INTO sys_file_extra_public (file_extra_id, dept_id)
		VALUES
		<foreach collection="publicDeptIdList" item="deptId" separator=",">
			(#{id}, #{deptId})
		</foreach>
	</insert>

List classIdList = classIds.split(“,”);
Student page(@Param(“classIdList”) List classIdList);

<!--批量插入-->
 	<insert id="addBatch">
		SELECT *
		FROM
			stu_table
		WHERE
			class_id
		IN
		<foreach collection="classIdList" item="classId" open="(" separator="," close=")">
			#{classId}
		</foreach>
		-- classIdList=[1,2,3]
		-- ( 1,2,3 )
	</insert>

Student page(@Param(“classIds” String classIds); // classIds = “1,2,3”;’

<!--批量插入-->
 	<insert id="addBatch">
		SELECT *
		FROM
			stu_table
		WHERE
			class_id
		FINd_IN_SET #{classIds}
		ORDER BY
			stu_table.name ASC, stu_table.create_time DESC
	</insert>

Set classIdList = classIds.split(“,”);

根据id列表批量更新单个字段

1 4
1 5
1 6
2 2
2 5
2 6
Student page(@Param(“categoryId”) Long categoryId, @Param(“articleIdList”) List articleIdList);

<!--批量修改文章专栏-->
	<update id="updateCategoryByIds">
		UPDATE cms_article SET category_id = #{categoryId} WHERE id IN
		<foreach collection="articleIdList" item="item" open="(" separator="," close=")">
		  #{item}
		</foreach>
</update>

批量更新对象列表

<!--批量更新-->
    <update id="updateBatch">
		<foreach collection="appsCategoryList" item="item" open="" separator=";" close="">
			UPDATE cms_category
			<set>
				parent_ids = #{item.parentIds},
			  update_by = #{item.updateBy}
			</set>
			WHERE id = #{item.id}
		</foreach>
	</update>

批量删除

<delete id="batchDelete" parameterType="_int[]">
	delete from products where productId IN
	<foreach collection="array" open="(" close=")" separator="," item="id">
		#{id}
	</foreach>
</delete>

先分组后排序

		GROUP BY
				t1.id
			ORDER BY
			    t1.sort_order DESC,
			    t1.create_time DESC

1  1
1  3
2  2

1  1
2  2
1  3

1  1
1  3
2  2
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Whitemeen太白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值