參考來自: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":"true" 13 14 }
2.代碼
原本是通過報的異常 DuplicateKeyException,在catch語句里執行update。但是這樣操作有一個問題,后面單獨說。
1 try { 2 resourceDao.insert(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 UPDATE 40 <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。
1 catch (DuplicateKeyException e) { 2 if (StringUtils.equals(update,Constants.KEY_FALSE)) { 3 throw new AuthServiceException(ErrorCode.RESOURCE_DUPLICATE_KEY, new Object[]{resource.getFdResName()}, e); 4 }else if (StringUtils.equals(update,Constants.KEY_TRUE)){ 5 resourceDao.update(resource); 6 } 7 throw new AuthServiceException(ErrorCode.RESOURCE_DUPLICATE_KEY, new Object[]{resource.getFdResName()}, e); 8 }
但是在插入重復數據時,報了如下錯誤:
1 Caused by: org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
原因是這個類ServiceResource上標記了@Transactional(在沒有事務管理的場景下,是可以像上面那樣做的):