简体   繁体   中英

get update record primary key failed when using getGeneratedKeys - MyBatis

I am trying to get update record primary key using this way in 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>

and the Mapper.java define like this:

int updateForFreeSeat(RoomSeat roomSeat);

And now I get update record id failed, the id is my primay key.This is my debug output:

在此处输入图片说明

what should I do to get update record's id?

I implement it like this:

    <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>

and this is my mapper:

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

I inspire by this question How to get ID of the last updated row in MySQL?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM