简体   繁体   中英

how to get each entity's id when batch insert using mybatis

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>

I have just posted a related answer here .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM