mybatis如何获取刚刚新插入数据的主键值id
作者:洛阳纸贵
这篇文章主要介绍了mybatis如何获取刚刚新插入数据的主键值id问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
mybatis获取刚刚新插入数据的主键值id
插入操作后,立马返回主键Id。查阅了很多资料,用keyProperty和useGeneratedKeys属性。
useGeneratedKeys 参数只针对 insert 语句生效,默认为 false。
当设置为 true 时,表示如果插入的表以自增列为主键,则允许 JDBC 支持自动生成主键,并可将自动生成的主键返回。
主要是以这样的方式:
<insert id="insertFileRecord" useGeneratedKeys="true" keyProperty="id">
insert xxx
</insert>但是没有生效,于是我找用了useGeneratedKeys="true"的写法,发现我少写了
<insert id="insertFileRecord" parameterType="FileRecord" useGeneratedKeys="true" keyProperty="id">
insert xxx
</insert>parameterType只能告诉它,在哪个实体类中,在Mybatis Mapper文件中添加属性 “useGeneratedKeys”和“keyProperty”,其中 keyProperty 是 Java 对象的属性名,而不是表的字段名。
但是还是没生效,我开始怀疑是我自增id是long类型,但实际上,并不是的,是我在mapper接口类中,加了这样一个注释
int insertFileRecord(@Param("fileRecord") FileRecord fileRecord);并且在insert语句中fileRecord.fileName,去掉@Param,终于看到id了,

最后附上完整的写法:
mapper接口:
/**
* @Description: 新增数据
* @Author: wj
* @param: cc.jz.work.entity.FileRecord
* @Return: 影响行数
* @Date: 2022-03-31 10:37:18
*/
int insertFileRecord(FileRecord fileRecord);xml:
<insert id="insertFileRecord" parameterType="cc.jz.work.entity.FileRecord" useGeneratedKeys="true" keyProperty="id">
insert into
file_record
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="fileAddress != null and fileAddress != ''">
file_address,
</if>
<if test="fileName != null and fileName != ''">
file_name,
</if>
<if test="uploadUserId != null">
upload_user_id,
</if>
<if test="uploadTime != null">
upload_time,
</if>
<if test="status != null and status != ''">
status,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="fileAddress != null and fileAddress != ''">
#{fileAddress,jdbcType=VARCHAR},
</if>
<if test="fileName != null and fileName != ''">
#{fileName,jdbcType=VARCHAR},
</if>
<if test="uploadUserId != null">
#{uploadUserId,jdbcType=INTEGER},
</if>
<if test="uploadTime != null">
#{uploadTime,jdbcType=TIMESTAMP},
</if>
<if test="status != null and status != ''">
#{status,jdbcType=VARCHAR},
</if>
</trim>
</insert>mybatis中插入记录获取主键值
在数据库中主键的值是通过自动递增的方式创建的,然而在通过mybatis插入数据之后想获取插入数据的主键值可以通过下面两种方法:
方法一
在xml的配置文件中的insert标签中加入 <selectKey> 标签
<insert id="insert">
<selectKey keyProperty="id" resultType="int" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
insert into t_student(uname,pass,stu_name,gender,birthdate,score)
values(#{uname},#{pass},#{stu_name},#{gender},#{birthdate},#{score})
</insert>public static void doInsert() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
Student s = new Student();
s.setBirthdate("2020-01-01");
s.setGender('1');
s.setPass("123");
s.setScore(69.3);
s.setStu_name("张四");
s.setUname("jack");
int i = sqlSession.insert("student.insert",s);
System.out.println("i="+i);
System.out.println("新插入的学生的id为:"+s.getId());
sqlSession.commit();
sqlSession.close();
}注意:
- selectKey标签中的 select LAST_INSERT_ID() 语句就能获取生成的主键
- selectKey标签中的keyProperty属性就是主键名,MyBatis会自动将获取的主键封装给此属性。
order的值有两种:BEFORE、AFTER
- BEFORE:先获取主键,然后执行insert; 比如 Oracle数据库。
- AFTER:先执行insert,然后获取主键; 比如 MySql数据库。
方法二
在select标签中加入useGeneratedKeys=“true” keyProperty=“id”
<insert id="insert2" useGeneratedKeys="true" keyProperty="id">
insert into t_student(uname,pass,stu_name,gender,birthdate,score)
values(#{uname},#{pass},#{stu_name},#{gender},#{birthdate},#{score})
</insert>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
