2017.7.7 postgreSQL在插入造成重复时执行更新
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了2017.7.7 postgreSQL在插入造成重复时执行更新,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4616字,纯文字阅读大概需要7分钟。
内容图文
![2017.7.7 postgreSQL在插入造成重复时执行更新](/upload/InfoBanner/zyjiaocheng/486/b7d40e8d928c46cb99c24604908f2173.jpg)
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);
}
2017.7.7 postgreSQL在插入造成重复时执行更新
标签:set href family ack == object com conf har
本文系统来源:http://www.cnblogs.com/lyh421/p/7131169.html
内容总结
以上是互联网集市为您收集整理的2017.7.7 postgreSQL在插入造成重复时执行更新全部内容,希望文章能够帮你解决2017.7.7 postgreSQL在插入造成重复时执行更新所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。