I love the java series --- [mybatis in bulk insert, if present, the update; does not exist, add]

Sample code:

1.dao level code:

int insertList(@Param("records") List<GwServerConfDetail> records);

2.xml level code:

<insert id="insertList"  parameterType="java.util.List">
    insert into gw_server_conf_detail
    ( SERVER_CONFIG_ID, CHANNEL_ID, NODE_NUM, SEND_ABILITY, CREATE_BY, UPDATE_BY)
    values
    <foreach collection="records" item="record" index="index" separator=",">
      (
      #{record.serverConfigId},
      #{record.channelId},
      #{record.nodeNum},
      #{record.sendAbility},
      #{record.createBy},
      #{record.updateBy}
      )
    </foreach>
    on duplicate key update
    NODE_NUM = values ​​(NODE_NUM),
    SEND_ABILITY=values(SEND_ABILITY),
    UPDATE_BY =values(UPDATE_BY)
  </insert>

note:

on duplicate key update NODE_NUM = values ​​(NODE_NUM), the variables in brackets, should be consistent with the field names of the table, and not consistent with the property name class.

Guess you like

Origin www.cnblogs.com/hujunwei/p/12093468.html