方法一
在insert标签里加两个属性,分别是useGeneratedKeys="true"和keyProperty="seq"
其中seq是我数据库表里的自增主键
方法二
在insert标签内部加上
select last_insert_id()
完整如下
insert into sys_menu
seq,
id,
pid,
text,
icon_cls,
url,
state,
create_date,
update_date,
#{seq,jdbcType=INTEGER},
#{id,jdbcType=VARCHAR},
#{pid,jdbcType=VARCHAR},
#{text,jdbcType=VARCHAR},
#{iconCls,jdbcType=VARCHAR},
#{url,jdbcType=VARCHAR},
#{state,jdbcType=INTEGER},
#{createDate,jdbcType=TIMESTAMP},
#{updateDate,jdbcType=TIMESTAMP},
select last_insert_id()
调用实例
@RequestMapping("add")
public String addMenu(String jsonData){
Result result=new Result();
SysMenuEntity sysMenuEntity=JSONObject.parseObject(jsonData,SysMenuEntity.class);
sysMenuEntity.setCreateDate(new Date());
sysMenuEntity.setUpdateDate(new Date());
int count = sysMenuService.insertSelective(sysMenuEntity);
if (count == 1){
result.setResultCode(Constant.ResultCode.OK);
result.setMessage("保存成功");
}
System.out.println(sysMenuEntity.getSeq());
super.writeJson(result);
return null;
}
输出结果
2018-10-31 15:35:02.802 DEBUG 5252 --- [nio-8098-exec-7] c.n.b.m.r.SysMenuMapper.insertSelective : ==> Preparing: insert into sys_menu ( id, pid, text, icon_cls, url, state, create_date, update_date ) values ( ?, ?, ?, ?, ?, ?, ?, ? )
2018-10-31 15:35:02.804 DEBUG 5252 --- [nio-8098-exec-7] c.n.b.m.r.SysMenuMapper.insertSelective : ==> Parameters: 不不不(String), (String), 呃呃呃(String), 啊啊啊(String), (String), 1(Integer), 2018-10-31 15:35:02.799(Timestamp), 2018-10-31 15:35:02.799(Timestamp)
2018-10-31 15:35:02.895 DEBUG 5252 --- [nio-8098-exec-7] c.n.b.m.r.SysMenuMapper.insertSelective : <== Updates: 1
2018-10-31 15:35:02.896 DEBUG 5252 --- [nio-8098-exec-7] c.n.b.m.r.S.insertSelective!selectKey : ==> Preparing: select last_insert_id()
2018-10-31 15:35:02.896 DEBUG 5252 --- [nio-8098-exec-7] c.n.b.m.r.S.insertSelective!selectKey : ==> Parameters:
2018-10-31 15:35:02.943 DEBUG 5252 --- [nio-8098-exec-7] c.n.b.m.r.S.insertSelective!selectKey : <== Total: 1
18