mybatis中的批量操作,多条件查询

批量操作

批量操作包括:批量插入,批量更新,批量查询

可以用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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mybatis,可以通过批量更新操作来一次性更新多条记录,减少网络通信的次数。这种批量更新操作可以提高效率。 以Oracle为例,下面是一个mybatis批量更新XML配置示例: ```xml <!--mybatis批量更新,注意mybatis还是按Java语法或按对象操作--> <update id="batchUpdate" parameterType="java.util.List"> begin <foreach collection="userList" index="index" item="user" separater=";"> update xTable <set> <if test="user.userName != ''"> userName = #{user.userName}, </if> <if test="user.age != null"> age = #{user.age}, <!--注意:此处最后一个逗号是需要的--> </if> </set> where user.department='DEV' <!--XXX条件--> </foreach> ; end; </update> ``` 这个配置文件,通过循环遍历一个用户列表数据,对满足条件(在示例是dev开发部门人员)的用户的用户名和年龄进行更新操作。通过一次性发送批量更新的SQL语句到数据库进行执行,减少了app和db之间的通信次数,提高了效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [mybatis批量更新操作](https://blog.csdn.net/shenzhenNBA/article/details/97518547)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值