简体   繁体   中英

mybatis 3 selectkey inside foreach

MyBatis 3 - Spring

I want to insert list of items and for every item id has to generate from "TVA_UPSELLADMIN_CHANNEL_SEQ.nextVal" but i am getting .xml validation error that you can't sub-child "selectKey" inside "foreach".

<insert id="insertServiceMappings" parameterType="java.util.List">

INSERT  
<foreach collection="list" item="channel" index="index" >

<selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT TVA_UPSELLADMIN_CHANNEL_SEQ.nextVal from dual
        </selectKey>

into tva_upselladmin_channel (id,source_id, service_id, name) values (#{id}, #{channel.sourceId}, #{channel.serviceId}, #{channel.name})
</foreach>
</insert>

MyBatis in its current version (or anyone for that matter) doesn't allow the use of <selectKey> inside a <forEach> tag. Is it only for iterate through a list.

The MyBatis dtd validation section for the <foreach> tag is as follow:

<!ELEMENT foreach (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*>
<!ATTLIST foreach
collection CDATA #REQUIRED
item CDATA #IMPLIED
index CDATA #IMPLIED
open CDATA #IMPLIED
close CDATA #IMPLIED
separator CDATA #IMPLIED
>

So in order to solve your problem you have to iterate through your list setting all the ids with an extra select definition on your XML. It would be like:

<select id="nextvalKey" resultType="java.lang.Integer">
     SELECT TVA_UPSELLADMIN_CHANNEL_SEQ.nextVal from dual
</select>

Your insert section would be like:

<insert id="insertServiceMappings" parameterType="java.util.List">
    <foreach collection="list" item="channel" index="index" >
        INSERT INTO tva_upselladmin_channel (id,source_id, service_id, name)
              VALUES ( #{id}, #{channel.sourceId}, #{channel.serviceId}, #{channel.name});
    </foreach>
</insert>

Don't forget the ; after the insert command because ORACLE does not support multiple values insert eg insert ... values (1,'bla'), (2, 'ble'), (3, 'bli'); )

So the second part is to have in your implementation a method to set each id for the items on the list. It would be something like:

  public void someMethodInsertList(List<SomeObject> list){
       //normally I do an implementation that allows me to use
       //sqlSessionTemplate from mybatis through an extended class 
       for ( SomeObject obj : list ){
           obj.setId( getSqlSessionTemplate.selectOne( 'nextvalKey' ) );
       }
       getSqlSessionTemplate.insert( 'insertServiceMappings', list );
  }

Hope it helps

I find another solution as below, it worked for me!

 <insert id="insertServiceMappings" parameterType="java.util.List" useGeneratedKeys="true">
         <selectKey keyProperty="id" resultType="java.lang.Long" order="BEFORE">
                SELECT TVA_UPSELLADMIN_CHANNEL_SEQ.nextVal as id FROM DUAL
         </selectKey>    
         INSERT INTO
         tva_upselladmin_channel (id,source_id, service_id, name)
         SELECT TVA_UPSELLADMIN_CHANNEL_SEQ.nextVal, A.* from (
         <foreach collection="list" item="channel" index="index" separator="union all">
         SELECT 
           #{channel.sourceId} as source_id,
           #{channel.serviceId} as service_id,
           #{channel.name}) as name
         FROM DUAL
         </foreach> ) A
    </insert>

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