方法一
<insert id="insertSelective" parameterType="com.neil.blog.entity.SysMenuEntity" useGeneratedKeys="true" keyProperty="seq">
在insert标签里加两个属性,分别是useGeneratedKeys="true"和keyProperty="seq"
其中seq是我数据库表里的自增主键
方法二
在insert标签内部加上
<selectKey keyProperty="seq" keyColumn="seq" resultType="long" order="AFTER">
select last_insert_id()
</selectKey>
完整如下
<insert id="insertSelective" parameterType="com.neil.blog.entity.SysMenuEntity" >
insert into sys_menu
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="seq != null">
seq,
</if>
<if test="id != null">
id,
</if>
<if test="pid != null">
pid,
</if>
<if test="text != null">
text,
</if>
<if test="iconCls != null">
icon_cls,
</if>
<if test="url != null">
url,
</if>
<if test="state != null">
state,
</if>
<if test="createDate != null">
create_date,
</if>
<if test="updateDate != null">
update_date,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="seq != null">
#{seq,jdbcType=INTEGER},
</if>
<if test="id != null">
#{id,jdbcType=VARCHAR},
</if>
<if test="pid != null">
#{pid,jdbcType=VARCHAR},
</if>
<if test="text != null">
#{text,jdbcType=VARCHAR},
</if>
<if test="iconCls != null">
#{iconCls,jdbcType=VARCHAR},
</if>
<if test="url != null">
#{url,jdbcType=VARCHAR},
</if>
<if test="state != null">
#{state,jdbcType=INTEGER},
</if>
<if test="createDate != null">
#{createDate,jdbcType=TIMESTAMP},
</if>
<if test="updateDate != null">
#{updateDate,jdbcType=TIMESTAMP},
</if>
</trim>
<selectKey keyProperty="seq" keyColumn="seq" resultType="long" order="AFTER">
select last_insert_id()
</selectKey>
</insert>
调用实例
@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