zoukankan      html  css  js  c++  java
  • 数据库唯一性约束异常插入处理

    数据库

    CREATE TABLE `auth_role` (
        `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
        `rolename` VARCHAR(50) NULL DEFAULT NULL COMMENT '角色名称',
        `adduser` INT(11) NULL DEFAULT NULL COMMENT '添加人',
        `addtime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
        PRIMARY KEY (`id`),
        UNIQUE INDEX `rolename` (`rolename`)
    )COMMENT='统一平台角色'
    COLLATE='utf8_general_ci'
    ENGINE=InnoDB
    ROW_FORMAT=COMPACT
    AUTO_INCREMENT=15;

    Controller

        @RequestMapping(value="/insertSelective",method={RequestMethod.GET,RequestMethod.POST})
        @ResponseBody
        public Object insertSelective(Auth_role record,HttpSession session){
            Userinfo sessionUserinfo = (Userinfo)session.getAttribute("CURR_USER");
            String sessionuserid = sessionUserinfo.getUserid().toString();
            record.setAdduser(Integer.parseInt(sessionuserid));
    Map map
    = new HashMap(); int result = 0; /*异常处理*/ try { result = service.insertSelective(record); }catch (org.springframework.dao.DuplicateKeyException e){ /*异常处理截获流程,如果是存在DuplicateKeyException则是重名,直接返回到前端,id主键为自增长,不会重复*/ map.put("code", "fail"); map.put("msg", "该角色名已存在!"); return map; } /*正常返回流程*/ if (result == 1) { map.put("code", "success"); map.put("msg", CN_MessageEnum.AddSuccess.getName()); }else { map.put("code", "fail"); map.put("msg", CN_MessageEnum.AddFailed.getName()); } return map; }
  • 相关阅读:
    一篇带你了解私有仓库 Harbor 的搭建
    docker优化之Cgroup资源配置
    WARNING: IPv4 forwarding is disabled. Networking will not work.
    Docker镜像、私有仓库
    了解Docker Consul 工具 ,一篇就够了
    运维必备之日志分析工具ELK
    mysql5.6与5.7版本的区别
    Docker Compose-容器编排工具
    SQL中对于增加和删除“约束”的语句
    python3-基础5
  • 原文地址:https://www.cnblogs.com/aeolian/p/12073764.html
Copyright © 2011-2022 走看看