ホームページ  >  記事  >  データベース  >  MyBatis+MySQL 返回插入的主键ID

MyBatis+MySQL 返回插入的主键ID

WBOY
WBOYオリジナル
2016-06-07 15:39:561363ブラウズ

需求:使用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>

如上所示,我们在insert中指定了keyProperty="userId",其中userId代表插入的User对象的主键属性。
 
User.java
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。