向数据库插入一条数据同时获取主键id

首先创建一个对象

将这个对象使用mybatis插入数据库

java 复制代码
Student stu = new Student(null, "stu", 18, null);
mapper.insertEntity(stu);
System.out.println(stu);
复制代码
在mapper.xml文件中开启配置之后输出传入的stu对象就会有主键id回传
XML 复制代码
<!-- 插入语句示例  int insertEntity(Student student);
    useGeneratedKeys="true" 表示当前标签中要执行的sql使用了自动递增主键
    keyProperty="studentId" 表示把自动递增的主键的值 放入 方法中传输过来参数中的某一个属性中 (放入了student studentId属性中)
-->
<insert id="insertEntity" useGeneratedKeys="true" keyProperty="studentId">
    insert into student(`student_id`,`student_name`,`student_age`,`student_image`)
    values (#{studentId},#{studentName},#{studentAge},#{studentImage});
</insert>
相关推荐
HerayChen16 小时前
HbuilderX 内存溢出报错
java·大数据·linux
程序员小白条16 小时前
0经验如何找实习?
java·开发语言·数据结构·数据库·链表
liulilittle16 小时前
C++ 浮点数封装。
linux·服务器·开发语言·前端·网络·数据库·c++
小马爱打代码17 小时前
Spring AI:搭建自定义 MCP Server:获取 QQ 信息
java·人工智能·spring
郭涤生17 小时前
QT 架构笔记
java·数据库·系统架构
韩立学长17 小时前
基于Springboot流浪动物领养网站0kh2iyb4(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
daidaidaiyu17 小时前
基于LangGraph开发复杂智能体学习一则
java·ai
DBA小马哥17 小时前
Oracle迁移到金仓数据库:完整迁移步骤与兼容性优化实战
数据库·oracle·国产化平替
@nengdoudou17 小时前
KStudio 客户端无法访问 KES 数据库服务器的指定 IP / 端口
数据库
小小8程序员17 小时前
STL 库(C++ Standard Template Library)全面介绍
java·开发语言·c++