参考来自: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); 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 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。
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);}
时间: 2024-10-14 18:36:42