Home > Database > Mysql Tutorial > body text

MyBatis+MySQL 返回插入的主键ID

WBOY
Release: 2016-06-07 15:39:56
Original
1410 people have browsed it

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

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

 

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

<insert id="insertAndGetId" usegeneratedkeys="true" keyproperty="userId" parametertype="com.chenzhou.mybatis.User">
	insert into user(userName,password,comment)
	values(#{userName},#{password},#{comment})
</insert>
Copy after login

如上所示,我们在insert中指定了keyProperty="userId",其中userId代表插入的User对象的主键属性。
 
User.java
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!