向数据库插入一条数据同时获取主键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>
相关推荐
w_t_y_y1 小时前
Nginx Plus
运维·数据库·nginx
川贝枇杷膏cbppg2 小时前
dm_unknown_202512.log:达梦数据库 “未分类日志“
数据库·oracle
It's now3 小时前
Spring AI 基础开发流程
java·人工智能·后端·spring
cxh_陈3 小时前
线程的状态,以及和锁有什么关系
java·线程·线程的状态·线程和锁
计算机毕设VX:Fegn08953 小时前
计算机毕业设计|基于springboot + vue图书商城系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
R.lin3 小时前
Java 8日期时间API完全指南
java·开发语言·python
毕设源码-赖学姐3 小时前
【开题答辩全过程】以 高校教学质量监控平台为例,包含答辩的问题和答案
java·eclipse
高山上有一只小老虎3 小时前
翻之矩阵中的行
java·算法
求学中--3 小时前
MySQL 数据库完整操作命令与使用指南
数据库·sql·mysql·oracle
火钳游侠3 小时前
java单行注释,多行注释,文档注释
java·开发语言