
Mybatis【6】-- Mybatis插入数据后自增id怎么获取?
发布日期:2021-05-08 23:24:24
浏览次数:13
分类:博客文章
本文共 3223 字,大约阅读时间需要 10 分钟。
代码直接放在Github仓库【 】
需要声明的是:此Mybatis
学习笔记,是从原始的Mybatis
开始的,而不是整合了其他框架(比如Spring
)之后,个人认为,这样能对它的功能,它能帮我们做什么,有更好的理解,后面再慢慢叠加其他的功能。
我们知道很多时候我们有一个需求,我们需要把插入数据后的id返回来,以便我们下一次操作。
其实一开始的思路是我插入之后,再执行一次select,根据一个唯一的字段来执行select
操作,但是Student
这个类如果插入后再根据名字或者年龄查出来,这根本就是不可行的!!!重名与同年龄的人一定不少。
/** * 测试插入后获取id */@Testpublic void testinsertStudentCacheId(){ Student student=new Student("helloworld",17,85); System.out.println("插入前:student="+student); dao.insertStudentCacheId(student); System.out.println("插入后:student="+student);}
useGeneratedKeys 设置主键自增
insert into student(name,age,score) values(#{name},#{age},#{score})
需要注意的点:
- 1.
useGeneratedKeys="true"
表示设置属性自增 - 2.
keyProperty="id"
设置主键的字段 - 3.
parameterType="Student"
设置传入的类型 - 4.注意:虽然有返回类型,但是我们不需要手动设置返回的类型,这个是由框架帮我们实现的,所以对应的接口方法也是没有返回值的,会修改我们插入的对象,设置id值。
- 5.实体类中id属性字段一定需要set以及get方法
使用selectKey 查询主键
insert into student(name,age,score) values(#{name},#{age},#{score}) select @@identity
或者写成:
insert into student(name,age,score) values(#{name},#{age},#{score}) select LAST_INSERT_ID()
两种方式的结果:
注意要点:
- 1.最外层的
<insert></insert>
没有返回属性(resultType)
,但是里面的<selectKey></selectKey>
是有返回值类型的。 - 2.
order="AFTER"
表示先执行插入,之后才执行selectkey
语句的。 - 3.
select @@identity
和select LAST_INSERT_ID()
都表示选出刚刚插入的最后一条数据的id。 - 4.实体类中id属性字段一定需要set以及get方法
- 5.此时,接口中仍不需要有返回值,框架会自动将值注入到我们
insert
的那个对象中,我们可以直接使用就可以了。
其实,我们的接口中可以有返回值,但是这个返回值不是id,而是表示插入后影响的行数,此时sql中仍和上面一样,不需要写返回值。
insert into student(name,age,score) values(#{name},#{age},#{score}) select LAST_INSERT_ID()
接口中:
// 增加新学生并返回id返回resultpublic int insertStudentCacheId(Student student);
接口的实现类:
public int insertStudentCacheId(Student student) { int result; try { sqlSession = MyBatisUtils.getSqlSession(); result =sqlSession.insert("insertStudentCacheId", student); sqlSession.commit(); } finally { if (sqlSession != null) { sqlSession.close(); } } return result; }
Test中:
public void testinsertStudentCacheId(){ Student student=new Student("helloworld",17,101); System.out.println("插入前:student="+student); int result = dao.insertStudentCacheId(student); System.out.println(result); System.out.println("插入后:student="+student); }
结果证明:result的值为1,表示插入了一行,查看数据库,确实插入了数据。
PS:如果无法创建连接,需要把Mysql
的jar包升级:
mysql mysql-connector-java 8.0.21
如果报以下的错误,那么需要将&改成转义后的符号&
:
org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession.### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 107; 对实体 "serverTimezone" 的引用必须以 ';' 分隔符结尾。
在xml里面配置需要转义,不在xml文件里面配置则不需要
【作者简介】:
秦怀,公众号【秦怀杂货店】作者,技术之路不在一时,山高水长,纵使缓慢,驰而不息。这个世界希望一切都很快,更快,但是我希望自己能走好每一步,写好每一篇文章,期待和你们一起交流。此文章仅代表自己(本菜鸟)学习积累记录,或者学习笔记,如有侵权,请联系作者核实删除。人无完人,文章也一样,文笔稚嫩,在下不才,勿喷,如果有错误之处,还望指出,感激不尽~
发表评论
最新留言
感谢大佬
[***.8.128.20]2025年04月05日 11时33分36秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【Linux】2.3 Linux目录结构
2021-05-09
java.util.Optional学习笔记
2021-05-09
远程触发Jenkins的Pipeline任务的并发问题处理
2021-05-09
jackson学习之七:常用Field注解
2021-05-09
jackson学习之八:常用方法注解
2021-05-09
Web应用程序并发问题处理的一点小经验
2021-05-09
entity framework core在独立类库下执行迁移操作
2021-05-09
Asp.Net Core 2.1+的视图缓存(响应缓存)
2021-05-09
服务器开发- Asp.Net Core中的websocket,并封装一个简单的中间件
2021-05-09
没花一分钱的我竟然收到的JetBrains IDEA官方免费赠送一年的Licence
2021-05-09
Redis 集合统计(HyperLogLog)
2021-05-09
RE套路 - 关于pyinstaller打包文件的复原
2021-05-09
【wp】HWS计划2021硬件安全冬令营线上选拔赛
2021-05-09
Ef+T4模板实现代码快速生成器
2021-05-09
dll详解
2021-05-09
c++ static笔记
2021-05-09
C++中头文件相互包含与前置声明
2021-05-09
JQuery选择器
2021-05-09
MVC中在一个视图中,怎么加载另外一个视图?
2021-05-09
SQL--存储过程
2021-05-09