1. 程式人生 > >mybatis 當需要批量插入並更新主鍵相同的資料時的處理方法

mybatis 當需要批量插入並更新主鍵相同的資料時的處理方法

該方法 如果表中沒有沒有主鍵對應的資料就新增一條 如果有的話就更新該條資料

<insert id="insertBatch">
    REPLACE into representatives_and_principal_personnel
    (id,examine_and_approve_id, idcard_number, name, sex, duty, rankv, birth,
    standard_culture, workspace, state, phone, have_bad_record, amount_of_contribution)
    values
    <foreach collection="list" item="representativesAndPrincipalPersonnel" separator=",">
        (#{representativesAndPrincipalPersonnel.id},
        #{representativesAndPrincipalPersonnel.examine_and_approve_id},
        #{representativesAndPrincipalPersonnel.idcard_number},
        #{representativesAndPrincipalPersonnel.name},
        #{representativesAndPrincipalPersonnel.sex},
        #{representativesAndPrincipalPersonnel.duty},
        #{representativesAndPrincipalPersonnel.rankv},
        #{representativesAndPrincipalPersonnel.birth},
        #{representativesAndPrincipalPersonnel.standard_culture},
        #{representativesAndPrincipalPersonnel.workspace},
        #{representativesAndPrincipalPersonnel.state},
        #{representativesAndPrincipalPersonnel.phone},
        #{representativesAndPrincipalPersonnel.have_bad_record},
        #{representativesAndPrincipalPersonnel.amount_of_contribution})
    </foreach>
</insert>