使用mybatis批量插入时如何获取每个实体的ID

使用mybatis批量插入时如何获取每个实体的ID

本文介绍了使用mybatis批量插入时如何获取每个实体的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,当我们使用mybatis保存实体时,我们可以使用keyPropertyuseGeneratedKeys属性来获取实体的ID.但是当我仅使用批处理插入实体时如何获取每个实体的ID.如下所示:

it is know that when we save an entity using mybatis,we can use the keyProperty and useGeneratedKeys attribute to get the entity's id.but how I can get each entity's id when I batch insert entities using just like below:

<insert id="inserts" keyProperty="id" useGeneratedKeys="true">
        insert into t_ext_wk_agent (agent_code,agent_name,agent_type,icon,agent_url_state,description,target,
        state,create_time,modify_time)
        values
        <foreach collection="list" separator="," item="item">
            (#{item.agentCode},#{item.agentName},#{item.agentType},#{item.icon},#{item.agentUrlState},#{item.description},
            #{item.target},#{item.state},#{item.createTime},#{item.modifyTime})
        </foreach>
    </insert>

推荐答案

我刚刚发布了相关的.

只需在Java Foreach循环中执行一个简单的Insert语句即可,而不是遍历Mybatis XML中的集合.最重要的是会话执行器类型(REUSE或BATCH).然后,您回到简单的插入/获取生成的密钥案例

Just execute a simple Insert statement in a Java Foreach loop instead of iterating over collection in Mybatis XML. The most important thing is the session Executor type (REUSE or BATCH). You then come back to the simple insert / get generated key case

这篇关于使用mybatis批量插入时如何获取每个实体的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:25