mybatis返回插入的主键ID

来源:互联网 发布:java的api下载 编辑:程序博客网 时间:2024/06/05 18:13

需求:使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值。

 

方法:在mapper中指定keyProperty属性,示例如下:

Xml代码  收藏代码
  1. <insert id="insertAndGetId" useGeneratedKeys="true" keyProperty="userId" parameterType="com.chenzhou.mybatis.User">  
  2.     insert into user(userName,password,comment)  
  3.     values(#{userName},#{password},#{comment})  
  4. </insert>  

 如上所示,我们在insert中指定了keyProperty="userId",其中userId代表插入的User对象的主键属性。

转载地址:http://chenzhou123520.iteye.com/blog/1849881

0 0