mybatis中在相应的mapper.xml的insert方法中加入配置参数,就能够在插入语句的时候自动返回自增长的id,如下代码段为后端执行的代码
int count = jzAskingAnswerMapper.insertreturnid(answer);
if(count>0){
return new Result(ResultCode.C200.getCode(), MessageConstants.SAVE_SUCCESS, answer.getId());
}
对应的xml中的insertreturnid方法为:
<insert id="insertreturnid" parameterType="com.chinaredstar.erp.domain.po.JzAskingAnswer" useGeneratedKeys="true"
keyProperty="id">
insert into jz_asking_answer (id, asking_id, answer_title,
user_open_id, user_realname, opt_user_id,
opt_username, status, is_top,
set_top_date, is_online, ip,
create_date, is_del, answer_content,sort_num
)
values (#{id,jdbcType=INTEGER}, #{askingId,jdbcType=INTEGER}, #{answerTitle,jdbcType=VARCHAR},
#{userOpenId,jdbcType=VARCHAR}, #{userRealname,jdbcType=VARCHAR}, #{optUserId,jdbcType=VARCHAR},
#{optUsername,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, #{isTop,jdbcType=INTEGER},
#{setTopDate,jdbcType=TIMESTAMP}, #{isOnline,jdbcType=INTEGER}, #{ip,jdbcType=VARCHAR},
#{createDate,jdbcType=TIMESTAMP}, #{isDel,jdbcType=INTEGER}, #{answerContent,jdbcType=LONGVARCHAR}, #{sortNum,jdbcType=INTEGER}
)
</insert>
在<insert ..></insert>中加入
useGeneratedKeys="true"
keyProperty="id"
配置,就会在插入语句的时候自动返回自增长id返回并且封装到插入时插入的参数对象中