在MyBatis中,<select><insert><update><delete>标签可以使用last关键字来获取最后插入的自增主键值。

例如,在插入数据时,可以使用last关键字来获取最后插入的自增主键值,如下所示:

<insert id="insertUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
  INSERT INTO user (username, password, email)
  VALUES (#{username}, #{password}, #{email})
</insert>

在执行完上述插入操作后,可以通过#{id}来获取最后插入的自增主键值。

另外,last关键字还可以用于<selectKey>标签中,用于获取最后插入的自增主键值。

<insert id="insertUser" parameterType="User">
  <selectKey resultType="int" keyProperty="id" order="AFTER">
    SELECT last_insert_id()
  </selectKey>
  INSERT INTO user (username, password, email)
  VALUES (#{username}, #{password}, #{email})
</insert>

在执行完上述插入操作后,可以通过#{id}来获取最后插入的自增主键值

MYBITIS中 last

原文地址: http://www.cveoy.top/t/topic/iZjl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录