做工作流时候 Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1

Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1
错误说明:
  返回的1是影响的行数,并不是自增的主键id;
  想要获取自增主键id,需要通过xx.getId()方法获取,因为在mybatis中指定自增主键id封装到了对象的属性中,所以我们需要在对象中来获取

代码示例如下:

<insert id="insertBizLeave" parameterType="BizLeaveVo" useGeneratedKeys="true" keyProperty="id">
        insert into biz_leave
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="type != null  and type != ''">type,</if>
            <if test="title != null  and title != ''">title,</if>
            <if test="reason != null  and reason != ''">reason,</if>
            <if test="startTime != null ">start_time,</if>
            <if test="endTime != null ">end_time,</if>
            <if test="totalTime != null ">total_time,</if>
            <if test="instanceId != null  and instanceId != ''">instance_id,</if>
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="updateTime != null ">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="type != null  and type != ''">#{type},</if>
            <if test="title != null  and title != ''">#{title},</if>
            <if test="reason != null  and reason != ''">#{reason},</if>
            <if test="startTime != null ">#{startTime},</if>
            <if test="endTime != null ">#{endTime},</if>
            <if test="totalTime != null ">#{totalTime},</if>
            <if test="instanceId != null  and instanceId != ''">#{instanceId},</if>
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
        </trim>
    </insert>

复制代码

总结:

  1. 想要获取自增主键id,应该通过对象的getId()方法,而并不是insert的返回值,insert的返回值表示的是影响行数

2.在mapper.xml中:useGeneratedKeys=“true”、keyProperty=“id”,这两个属性的作用:

共同决定了sql执行后,会将主键封装到id属性上;

自增主键封装到了对象的id属性上了,那么想要获取,直接调用对象的getId()方法就可以了

 @Override
    public int addNewApply(BizLeave bizLeave) {
       /* if(StringUtils.isEmpty(bizLeave.getInstanceId())){
            throw new BusinessException("流程信息不存在");
        }*/
        HashMap <String, Object> variables = new HashMap <>();
        if (StringUtil.isNotEmpty(bizLeave.getApplyUser())) {
            variables.put("username", bizLeave.getApplyUser());
        }
        if (StringUtil.isNotEmpty(bizLeave.getType())) {
            variables.put("types", bizLeave.getType());
        }
        if (StringUtil.isNotEmpty(bizLeave.getContents())) {
            variables.put("contents", bizLeave.getContents());
        }
        Integer days = bizLeave.getDays();
        if (days != null) {
            if (days <= 3) {
                variables.put("user", "xiaohua");
            }
            if (days > 3 && days <= 7) {
                variables.put("user", "wangls");
            }
            if (days > 7 && days <= 15) {
                variables.put("user", "lizr");
            }
            variables.put("days", days);
        }
        //权限
        Subject subject = SecurityUtils.getSubject();
        SysUser principal = (SysUser) subject.getPrincipal();
        //设置发起人
        Authentication.setAuthenticatedUserId(principal.getUserName());

        //保存业务数据
        
        //插入总是返回1
        int bId = bizLeaveService.insertBizLeave(bizLeave);
        //直接用获取getID
        Long id = bizLeave.getId();
        System.out.println(id + "==============");
        BizLeave bizLeave1 = bizLeaveService.selectBizLeaveById(id);

        //启动流程实列 资源管理
        ProcessInstance instance = runtimeService
                .startProcessInstanceByKey("Process_12-2",bizLeave.getId().toString(), variables);


        bizLeave.setInstanceId(instance.getProcessInstanceId()); // 建立双向关系
        bizLeaveMapper.updateBizLeave(bizLeave);

        System.out.println("流程定义ID:" + instance.getProcessDefinitionId());
        System.out.println("流程实例ID:" + instance.getId());
        System.out.println(instance.getBusinessKey() + "" + instance.getName());
        return 1;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值