向数据库插入一条数据同时获取主键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>
相关推荐
神王宝宝 王者小学2 小时前
HBase: 看上去很美
大数据·数据库·hbase
io无心2 小时前
Shardingsphere5分库分表
数据库·mysql
mifengxing2 小时前
LeetCode 41.缺失的第一个正数|Hard题O(n)+O(1)最优解法深度解析
java·算法·leetcode·排序算法
wWYy.3 小时前
Mysql:主键索引 唯一索引 普通索引 前缀索引
数据库·mysql
宝杰X73 小时前
Android Room3 多平台数据库
android·数据库
markinmarkin5 小时前
Spring 中Bean 的作用域有哪些?
java·后端·spring
山荷枝6 小时前
Java学习第十天
java·学习
matlabgoodboy6 小时前
计算机毕设代做|Java Python Matlab APP 全套开发设计
java·python·课程设计
sky_8106136 小时前
Oracle ERP 各模块业务管理功能及底层表说明
数据库·oracle
程序员雷欧7 小时前
环形缓冲区深度解析:从基础原理到Disruptor源码的全面剖析
java