10.2框架知识_mybatis#mybatis中插入数据自动返回自增长id的配置

 

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返回并且封装到插入时插入的参数对象中
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值