2016-03-30 9 views
2

mybatisを使用してエンティティを保存すると、keyPropertyおよびuseGeneratedKeys属性を使用してエンティティのidを取得できることがわかります。以下のように一括挿入するエンティティ:mybatisを使用してバッチ挿入するときに各エンティティのIDを取得する方法

<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> 

答えて

0

私はちょうど関連記事answer hereを投稿しました。

Mybatis XMLのコレクションを反復処理するのではなく、単純なInsert文をJava Foreachループで実行するだけです。最も重要なことは、セッションのExecutorタイプ(REUSEまたはBATCH)です。 挿入/取得キーをの場合に戻します。

関連する問題