mybatis在更新一行数据或者插入一条新数据后返回对应的主键id

本文介绍了如何在MyBatis中使用`useGeneratedKeys`和`keyProperty`属性在数据插入和更新时自动获取新插入数据的ID,特别强调了数据库表ID需为自增的条件。
摘要由CSDN通过智能技术生成

        今天在做项目的时候需要用到上次数据更新或者插入的id,学习了一下找到此方法,记录一下。

        在MyBatis中,当插入一条新数据后,可以使用useGeneratedKeys属性和keyProperty属性来获取新插入数据的ID。useGeneratedKeys属性告诉MyBatis使用JDBC的getGeneratedKeys方法来获取数据库生成的主键,keyProperty属性则指定哪个Java对象的属性应该被设置为这个主键。

在xml中在做更新操作时,加入useGeneratedKeys属性和keyProperty属性,或者上次更新数据的id。

    <update id="updateStationManagerInfo" useGeneratedKeys="true" keyProperty="id">
        update storage_tool_station_info_confirm
        set team_manager_id           = #{teamManagerId},
            team_manager_face         = #{teamManagerFace},
            team_manager_confirm_time = SYSDATE(),
            station_account_id        = #{stationAccountId},
            station_face              = #{stationFace},
            station_confirm_time      = SYSDATE(),
            type                      = #{type}
        where id = #{id}
    </update>

在xml中在做插入操作时,加入useGeneratedKeys属性和keyProperty属性 ,获取上次插入数据的id

    <insert id="insertStationManagerInfo" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO storage_tool_station_info_confirm(model_id, team_id, team_manager_id, team_manager_face,
                                                      team_manager_confirm_time, station_account_id, station_face,
                                                      station_confirm_time,
                                                      tool_count, operate_time, receiptor, type)
        VALUES (#{modelId}, #{teamId}, #{teamManagerId}, #{teamManagerFace}, #{teamManagerConfirmTime},
                #{stationAccountId}, #{stationFace}, #{stationConfirmTime}, #{recipentCount}, #{operateTime},
                #{receiptor}, #{type});

    </insert>

 java代码里取出就可以了

例如:

//更新操作
warehouseRequisitionMapper.updateStationManagerInfo(toolInfoConfirm);
//取id
log.info("id-------------:"+toolInfoConfirm.getId());



插入数据
//取id
log.info("Insert StorageToolInfoConfirm successfully.");
log.info("id-------------:"+toolInfoConfirm.getId());

注意:在使用此方式取id时,需要保证数据库表的id是自增的。

MyBatis 中,插入一条数据成功后可以返回主键的值。通常有以下几种方式可以实现: 1. 使用 `useGeneratedKeys` 和 `keyProperty` 属性:在插入语句的 XML 配置中,可以通过设置 `useGeneratedKeys` 属性为 `true` 来启用自动生成主键的功能,然后使用 `keyProperty` 属性指定主键对应的实体类属性。这样,在插入数据成功后,MyBatis 会自动将生成的主键值赋给 `keyProperty` 指定的属性。 例如,在 XML 配置中: ```xml <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> INSERT INTO user (username, password) VALUES (#{username}, #{password}) </insert> ``` 在 Java 代码中,插入数据成功后可以获取主键值: ```java User user = new User(); user.setUsername("test"); user.setPassword("123456"); sqlSession.insert("insertUser", user); Integer id = user.getId(); ``` 2. 使用 `selectKey` 元素:另一种方式是使用 `selectKey` 元素将插入语句与查询主键的语句结合起来。`selectKey` 元素可以定义一个查询语句,用于获取主键值,并将主键值赋给指定的属性。 例如,在 XML 配置中: ```xml <insert id="insertUser"> <selectKey keyProperty="id" resultType="java.lang.Integer" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> INSERT INTO user (username, password) VALUES (#{username}, #{password}) </insert> ``` 在 Java 代码中,插入数据成功后可以通过 `id` 属性获取主键值: ```java User user = new User(); user.setUsername("test"); user.setPassword("123456"); sqlSession.insert("insertUser", user); Integer id = user.getId(); ``` 无论使用哪种方式,插入数据成功后,都可以通过获取实体类的属性或通过参数的方式,得到刚插入数据主键值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值