向数据库插入一条数据同时获取主键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>
相关推荐
学c菜鸟鸟1 分钟前
漏洞知识——sql注入(二)
数据库·sql·oracle
Y***89084 分钟前
【JAVA进阶篇教学】第十二篇:Java中ReentrantReadWriteLock锁讲解
java·数据库·python
P***84398 分钟前
SpringBoot详解
java·spring boot·后端
guslegend15 分钟前
第2章:Linux服务器-Docker安装
java
5***262216 分钟前
【SpringBoot】SpringBoot中分页插件(PageHelper)的使用
java·spring boot·后端
周杰伦fans26 分钟前
在C#中,`StringContent` 是 `HttpContent` 的一个派生类
开发语言·数据库·c#
DanB2428 分钟前
Java(多线程)
java·开发语言·python
P***843930 分钟前
SQL多行数据合并到一行中的一个字段
数据库·sql
O***p60432 分钟前
Java在分布式中的Archaius
java·开发语言·分布式
j***294835 分钟前
【MySQL】数据库和表的操作
数据库·mysql·oracle