首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MyBatis中执行update时有没有办法返回主键或实体

问在MyBatis中执行update时有没有办法返回主键或实体
EN

Stack Overflow用户
提问于 2019-06-23 20:04:21
回答 1查看 507关注 0票数 0

我正在使用update语句修改使用MyBatis(DB为MySQL)的记录,我想返回update entity,有没有办法获取update entity或主键来查询update entity?这是我的update语句:

代码语言:javascript
复制
  <update id="updateForFreeSeat" parameterType="com.sportswin.soa.red.envelop.model.entity.RoomSeat">
    update r_room_seat
    set status = 1
    where room_id = #{roomId,jdbcType=BIGINT}
    order by room_play_id desc
    limit 1
  </update>

我使用以下方法读取了insert return select pk:

代码语言:javascript
复制
  <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
    SELECT LAST_INSERT_ID()
  </selectKey>

更新怎么样?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-24 04:20:36

根据mybatis documentation的说法,您可以使用useGeneratedKeys进行更新以及插入。

假设您想要返回的字段和属性名为id,您可以这样做:

代码语言:javascript
复制
<update id="updateForFreeSeat"
      parameterType="com.sportswin.soa.red.envelop.model.entity.RoomSeat"
      useGeneratedKeys="true"
      keyProperty="id">
    update r_room_seat
    set status = 1
    where room_id = #{roomId,jdbcType=BIGINT}
    order by room_play_id desc
    limit 1
</update>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56723677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档