批量操作
批量操作包括:批量插入,批量更新,批量查询
可以用foreach 其中collection主要有list,array,map,目前本人只用到list
批量插入
<insert id="insertBatch">
INSERT INTO tb_student (name, age, phone, address, class_id) VALUES
<foreach collection="list" separator="," item="item">
(#{item.name},#{item.age},#{item.phone},#{item.address},#{item.classId})
</foreach>
</insert>
如果批量插入有问题的话,可以简单粗暴用java实现遍历,逐条加入
这里需要注意括号的位置,也可写成下面的形式
批量更新
<update id="deleteNoticeById">
update t_w_send_notice
set
is_deleted = 1
where id in
<foreach collection="idlist" item="item" index="index"
open="(" separator="," close=")">
#{item}
</foreach>
</update>
注意如果collection 不写 "list",则需在mapper接口中写@Param("idList")
void deleteNoticeById(@Param("idList")List<Long> noticeIdList);
批量查询
批量查询和上面两种方法类似
<select id="selectByIdList" resultType="com.ruoyi.project.wxb.entity.po.Notice">
select t.id,t.user_id,t.start_time,t.content from t_w_send_notice t
where is_deleted=0 and id in
<foreach collection="list" item="item" index="index"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
多条件查询
<select id="totalCount" resultType="java.lang.Integer">
select * from xxxx
<where>
<if test="bedNo!=null and bedNo!='' ">
and bed_no like '%${bedNo}%'
</if>
<if test="bedRoomId!=null and bedRoomId!=''">
and bed_roomId like '%${bedRoomId}%'
</if>
</where>
</select>