首页 > 编程 > Java > 正文

MyBatis插入时获取自增主键方法

2019-11-26 13:37:21
字体:
来源:转载
供稿:网友

MyBatis 3.2.6插入时候获取自增主键方法有两种。下面以以MySQL5.5为例通过两种方法给大家介绍mybatis获取自增主键的方法,一起看看吧。

以MySQL5.5为例:

方法1:

<insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id">insert into person(name,pswd) values(#{name},#{pswd})</insert>

方法2:

<insert id="insert" parameterType="Person"><selectKey keyProperty="id" resultType="long">select LAST_INSERT_ID()</selectKey>insert into person(name,pswd) values(#{name},#{pswd})</insert>

插入前实体id属性为0;

插入后实体id属性为保存后自增的id;

以上所述是小编给大家介绍的MyBatis插入时获取自增主键方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表