向数据库插入一条数据同时获取主键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>
相关推荐
vigor51218 小时前
MySQL通过Mango实现分库分表
android·数据库·mysql
格子软件19 小时前
2026年GEO优化系统源码解构:核心状态机与高并发流控深度剖析
java·vue.js·spring boot·vue·geo
weixin1997010801619 小时前
[特殊字符]《京东订单API(jd.order.detail.get)对接ERP:企业认证+OAuth授权避坑指南》(附Python源码)
java·数据库·python
赵渝强老师19 小时前
【赵渝强老师】高斯数据库(openGauss)的模式
数据库·opengauss·国产数据库·高斯数据库
长不胖的路人甲19 小时前
Redis 数据删除策略
数据库·redis·spring
pW3g3lLuu19 小时前
在 VS Code 里直接改 JAR,我复刻了JarEditor
java·pycharm·jar
ClouGence19 小时前
SQL Server CDC 如何降低主库压力?Always On 备库读取实践
数据库·后端·sql·sqlserver
Tim_1019 小时前
【C++】009、extern关键字
java·开发语言
ShiXZ21319 小时前
PDF-OCR文件识别篇(七):数据入库
java·pdf·json·ocr·springboot
尽兴-19 小时前
Redis 为什么快?
数据库·redis·内存