gpt4 book ai didi

java - 使用 getGenerateKeys 获取更新记录主键失败 - MyBatis

转载 作者:行者123 更新时间:2023-12-01 17:52:28 24 4
gpt4 key购买 nike

我尝试在 MyBatis(v3.5.1) 中使用这种方式获取更新记录主键:

    <update id="updateForFreeSeat"
parameterType="com.sportswin.soa.room.model.entity.RoomSeat"
useGeneratedKeys="true"
keyProperty="id"
keyColumn="id">
update r_room_seat
set status = 1
where room_play_id = #{roomPlayId,jdbcType=BIGINT}
limit 1
</update>

Mapper.java 定义如下:

int updateForFreeSeat(RoomSeat roomSeat);

现在我得到更新记录 id 失败,该 id 是我的主键。这是我的调试输出:

enter image description here

我应该怎么做才能获取更新记录的id?

最佳答案

我是这样实现的:

    <select id="updateForFreeSeat"
parameterType="com.sportswin.soa.room.model.entity.RoomSeat" resultType="com.sportswin.soa.room.model.entity.RoomSeat">
set @update_id := 0;
UPDATE r_room_seat SET status = #{record.status,jdbcType=INTEGER},
id = (SELECT @update_id := id),
user_id = #{record.userId,jdbcType=BIGINT},
robot_flag = #{record.robotFlag,jdbcType=TINYINT}
WHERE room_play_id = #{record.roomPlayId,jdbcType=BIGINT} and status = 0
LIMIT 1;
select <include refid="Base_Column_List" /> from r_room_seat where id = @update_id;
</select>

这是我的映射器:

RoomSeat updateForFreeSeat(@Param(value = "record")RoomSeat roomSeat);

这个问题给了我启发How to get ID of the last updated row in MySQL?

关于java - 使用 getGenerateKeys 获取更新记录主键失败 - MyBatis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60771806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com