Mybatis mapper文件中处理集合List类型数据

本文介绍使用MyBatis进行批量数据更新和插入的方法,包括通过foreach元素处理多个ID的更新操作,以及如何一次性插入多条记录到数据库中。探讨了参数映射和集合的使用,适用于需要高效数据库操作的场景。

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

1 .

    <update id="updateStatus" parameterMap="SystemUserNotice">
        UPDATE
        <include refid="table" />
        SET
        `status` = #{status},`edit_time`=#{editTime}
        WHERE
        `id`
        IN
        (
        <foreach collection="idList" item="item" index="index" separator=",">
            #{item}
        </foreach>
        )
    </update>

 

2.

public class UserMessage{
   
    private Long id;

    private MessageModeEnum mode;

    private UserMessageTypeEnum sendType;

    private String title;

    private String content;

    private List<Long> userIds;

    private String iconUrl;

    private String args;

    private LocalDateTime createTime;

    private LocalDateTime readTime;
  <insert id="userMessage" parameterMap="UserMessage">
    INSERT INTO
      <include refid="table" />
      (`mode`,`send_type`,`title`,`content`,`user_id`,`icon_url`,`args`,`create_time`)
    VALUES
   <foreach collection="userIds" item="userId" index="index" separator =",">
      (#{mode},#{sendType},#{title}, #{content}, #{userId}, #{iconUrl}, #{args},#{createTime})
      </foreach >
    </insert>

3.

    <insert id="createList" parameterType="java.util.List">
        INSERT INTO
        <include refid="table" />
        (`xxxx_id`, `xxx_id`)
        VALUES
        <foreach collection="list" item="o" index="index" separator =",">
            (#{o.xxxx_id}, #{o.xxx_id})
        </foreach >
    </insert>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值