向数据库插入一条数据同时获取主键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>
相关推荐
Lucifer三思而后行13 小时前
Vertica 玩转示例数据库:VMart
数据库·oracle
Lucifer三思而后行13 小时前
zCloud 纳管 Oracle 数据库
数据库·oracle
m0_6178814213 小时前
如何将扁平数组转换为嵌套树形结构(JavaScript 实现)
jvm·数据库·python
ATCH IERV13 小时前
MySQL数据库误删恢复_mysql 数据 误删
数据库·mysql·adb
2401_8371638914 小时前
如何用 window.name 跨域存储不敏感的临时业务数据
jvm·数据库·python
.柒宇.14 小时前
Java八股之反射
java·开发语言
敖正炀14 小时前
LinkedTransferQueue 详解
java
m0_7349497914 小时前
如何在 Gin 框架中自定义 JSON 响应的 Content-Type 头部
jvm·数据库·python
环流_14 小时前
多线程1(面试题--常见的线程创建方式)
java·开发语言·面试
cyber_两只龙宝14 小时前
【Oracle】Oracle之SQL的转换函数和条件表达式
linux·运维·数据库·sql·云原生·oracle