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>
复制代码
总结:
- 想要获取自增主键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;
}