【MyBatis 】MyBatis 插入时候获取自增主键(1:写sql。2:注解@generatedvalue)
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