MyBatis 插入时候获取自增主键
方法有二
方法1-1:
<insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id">
insert into person(name,pswd) values(#{name},#{pswd})
</insert>
方法1-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>
方法2:
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;
insert插入后可以通过getId()获取id
@generatedvalue
本文介绍两种使用MyBatis获取插入记录后自动生成的主键ID的方法:一是通过设置useGeneratedKeys属性和keyProperty;二是利用注解@Id和@GeneratedValue。这两种方式适用于需要跟踪新记录ID的应用场景。
391

被折叠的 条评论
为什么被折叠?



