向数据库插入一条数据同时获取主键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>
相关推荐
满怀冰雪13 小时前
第04篇-双指针算法-从有序数组到回文判断的高频解法
java·算法
2601_9618451513 小时前
考研网课资源网盘|2027|资料
数据库·vim·sublime text·figma·photoshop·墨刀·高考
matlabgoodboy13 小时前
计算机java程序代写python代码编写c/c++代做qt设计php开发matlab
java·c语言·python
视觉小萌新13 小时前
C++利用libmicrohttpd制作交互网页端——C1
java·c++·交互
Amnesia0_013 小时前
MYSQL复合查询和内外连接
数据库·mysql
Gauss松鼠会13 小时前
【GaussDB】GaussDB SMP特性调优详解
java·服务器·前端·数据库·sql·算法·gaussdb
AI数据皮皮侠13 小时前
全国高考报名、录取数据(1977-2026)
大数据·数据库·人工智能·python·机器学习·高考
格发许可优化管理系统13 小时前
Mentor许可证使用规定全解析
java·大数据·c语言·开发语言·c++
JAVA面经实录91713 小时前
Redis 知识体系(完整版)
java·redis·nosql数据库·nosql
武子康13 小时前
Java-21 深入浅出 MyBatis 手写ORM框架2 手写Resources、MappedStatment、XMLBuilder等
java·后端