Home  >  Article  >  Database  >  MyBatis+MySQL 返回插入的主键ID

MyBatis+MySQL 返回插入的主键ID

WBOY
WBOYOriginal
2016-06-07 15:39:561304browse

需求:使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键。 方法:在mapper中指定keyProperty属性,示例如下: insert id=insertAndGetId useGeneratedKeys=true keyProperty=userId parameterType=com.chenzhou.mybatis.Userinsert in

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

 

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


	insert into user(userName,password,comment)
	values(#{userName},#{password},#{comment})

如上所示,我们在insert中指定了keyProperty="userId",其中userId代表插入的User对象的主键属性。
 
User.java
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn