標籤:set href family ack == object com conf har
參考來自:https://stackoverflow.com/questions/1109061/insert-on-duplicate-update-in-postgresql/1109198#1109198
功能需求:介面定義中包含欄位update,當它為true時,批量插入中有記錄造成唯一鍵重複時,不報異常,而是執行更新。
1.介面定義
1 { 2 "resources": 3 [ 4 { 5 "name":"*", 6 "display_name": "*", 7 "description": "*", 8 "service": "*" 9 },10 ....11 ],12 "update":true13 14 }
2.代碼
原本是通過報的異常 DuplicateKeyException,在catch語句裡執行update。但是這裡有一個問題,後面單獨說。
1 try { 2 resourceDao.insert(resource); 3 // 查詢admingroup的id,添加關聯 4 AddAdminPermission(resource); 5 } catch (DuplicateKeyException e) { 6 // if (StringUtils.equals(update,Constants.KEY_FALSE)) { 7 // throw new AuthServiceException(ErrorCode.RESOURCE_DUPLICATE_KEY, new Object[]{resource.getFdResName()}, e); 8 // }else if (StringUtils.equals(update,Constants.KEY_TRUE)){ 9 // resourceDao.update(resource);10 // }11 throw new AuthServiceException(ErrorCode.RESOURCE_DUPLICATE_KEY, new Object[]{resource.getFdResName()}, e);12 } catch (Exception e) {13 throw new AuthServiceException(ErrorCode.RESOURCE_CREATE_FAILED, new Object[]{resource.getFdResName()}, e);14 }
3.SQL語句
樣本語句:
1 INSERT INTO the_table (id, column_1, column_2) 2 VALUES (1, ‘A‘, ‘X‘)3 ON CONFLICT (唯一鍵) DO UPDATE 4 SET column_1 =‘A‘, 5 column_2 = ‘X‘;
完整語句:
1 <insert id="insert" parameterType="Resource" useGeneratedKeys="true" keyProperty="fdResid"> 2 INSERT INTO t_resource 3 <trim prefix="(" suffix=")" suffixOverrides=","> 4 <if test="fdResName != null"> 5 fd_res_name, 6 </if> 7 <if test="fdDisplayName != null"> 8 fd_display_name, 9 </if>10 <if test="fdResDesc != null">11 fd_res_desc,12 </if>13 <if test="fdTenantId != null">14 fd_tenantid,15 </if>16 <if test="fdService != null">17 fd_service,18 </if>19 </trim>20 <trim prefix="values (" suffix=")" suffixOverrides=",">21 <if test="fdResName != null">22 #{fdResName,jdbcType=VARCHAR},23 </if>24 <if test="fdDisplayName != null">25 #{fdDisplayName,jdbcType=VARCHAR},26 </if>27 <if test="fdResDesc != null">28 #{fdResDesc,jdbcType=VARCHAR},29 </if>30 <if test="fdTenantId != null">31 #{fdTenantId,jdbcType=INTEGER},32 </if>33 <if test="fdService != null">34 #{fdService,jdbcType=VARCHAR},35 </if>36 </trim>37 38 <if test="fdUpdate == ‘true‘">39 ON CONFLICT(fd_res_name, fd_tenantid, fd_service) DO UPDATE40 <set>41 <if test="fdDisplayName != null">fd_display_name = #{fdDisplayName},</if>42 <if test="fdResDesc != null">fd_res_desc = #{fdResDesc},</if>43 <if test="fdService != null">fd_service = #{fdService},</if>44 <if test="fdModifyDate != null">45 fd_modify_date = #{fdModifyDate,jdbcType=VARCHAR},46 </if>47 <if test="fdModifyPerson != null">48 fd_modify_person = #{fdModifyPerson,jdbcType=VARCHAR},49 </if>50 </set>51 </if>52 </insert>
4.遇到的問題
之前提到過,之前的處理是通過所報異常資訊來做的。如果重複了,在catch語句裡執行update。
try {
resourceDao.insert(resource);
// 查詢admingroup的id,添加關聯
AddAdminPermission(resource);
} catch (DuplicateKeyException e) {
// if (StringUtils.equals(update,Constants.KEY_FALSE)) {
// throw new AuthServiceException(ErrorCode.RESOURCE_DUPLICATE_KEY, new Object[]{resource.getFdResName()}, e);
// }else if (StringUtils.equals(update,Constants.KEY_TRUE)){
// resourceDao.update(resource);
// }
throw new AuthServiceException(ErrorCode.RESOURCE_DUPLICATE_KEY, new Object[]{resource.getFdResName()}, e);
} catch (Exception e) {
throw new AuthServiceException(ErrorCode.RESOURCE_CREATE_FAILED, new Object[]{resource.getFdResName()}, e);
}
2017.7.7 postgreSQL在插入造成重複時執行更新