mybatis 传入list集合查询和新增

本文详细介绍使用MyBatis进行批量数据操作的方法,包括如何通过Mapper接口和XML配置实现List集合的查询和插入,适用于需要高效处理大量数据的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入代码片
service:调用mapper里的方法
  List<ClubGoodsCommonClass> mapList = classMapper.selectCommonClassByGcIds(list);
  mapper:
 List<ClubGoodsCommonClass> selectCommonClassByGcIds(@Param("goodsIds") List<String> goodsIds);

mapper.xml里的方法:
  <select id="selectCommonClassByGcIds" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select gc_id, gc_name, gc_parent_id, full_name, full_id, gc_sort, gc_img, is_delete,gc_type, `level`,
    create_time
    from club_goods_common_class
    where 1>1
    <if test="goodsIds!=null ">
      <foreach item="goodsId" index="index" collection="goodsIds">
         or gc_id = #{goodsId}
      </foreach>
    </if>
  </select>

在这里插入图片描述

mybatis 传入集合新增:
service调mapper里的接口:

clubGoodsClassMapper.createGoodsClassList(arrayList);

mapper里的方法:
int createGoodsClassList(@Param(“list”) List bean);

mapper.xml里的方法:

在这里插入代码片 
<!--批量创建-->
  <insert id="createGoodsClassList" parameterType="com.shop.core.beans.ClubGoodsClass">
    INSERT INTO club_goods_class
    (class_id,gc_id, gc_name, gc_parent_id,
    full_name, full_id, gc_sort,
    gc_img, gc_type, shop_id, `level`, create_time
    )
    VALUES
    <foreach collection="list" item="item" index="index" separator="," >
      (#{item.classId},
      #{item.gcId},
      #{item.gcName},
      #{item.gcParentId},
      #{item.fullName},
      #{item.fullId},
      #{item.gcSort},
      #{item.gcImg},
      #{item.gcType},
      #{item.shopId},
      #{item.level},
      #{item.createTime})
    </foreach>
  </insert>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值