添加用户、返回主键

--场景:在执行新增用户sql后,service层返回新增用户的主键值(与mybatis一起使用)

          insert into user(username, sex, birthday) values(#{username}, #{sex}, #{birthday})

 

1.  数据库MySQL  主键自增

     执行insert提交之后自动生成一个自增主键。

     通过mysql的函数获取到刚刚插入记录的自增主键。LAST_INSERT_ID();

1 <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
2              Select LAST_INSERT_ID();
3 </selectKey>
4 insert into user(name, age, sex, birthday) 
5                values(#{name}, #{age}, #{sex}, #{birthday})
View Code

相关文章:

  • 2021-07-25
  • 2022-12-23
  • 2021-06-09
  • 2021-05-28
  • 2022-02-08
  • 2022-12-23
猜你喜欢
  • 2021-07-18
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2023-01-10
  • 2021-07-20
相关资源
相似解决方案