需求:使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值。
方法:在mapper中指定keyProperty属性,示例如下:
<insert id="insertAndGetId" useGeneratedKeys="true" keyProperty="userId" parameterType="com.chenzhou.mybatis.User"> insert into user(userName,password,comment) values(#{userName},#{password},#{comment}) </insert>
如上所示,我们在insert中指定了keyProperty="userId",其中userId代表插入的User对象的主键属性。
User.java
public class User { private int userId; private String userName; private String password; private String comment; //setter and getter }
UserDao.java
public interface UserDao { public int insertAndGetId(User user); }
测试:
User user = new User(); user.setUserName("chenzhou"); user.setPassword("xxxx"); user.setComment("测试插入数据返回主键功能"); System.out.println("插入前主键为:"+user.getUserId()); userDao.insertAndGetId(user);//插入操作 System.out.println("插入后主键为:"+user.getUserId());
输出:
插入前主键为:0 插入后主键为:15
查询数据库:
如上所示,刚刚插入的记录主键id为15
相关推荐
SpringBoot+MyBatis+Mysql+Layui实现功能完善的原创文学CMS系统 SpringBoot+MyBatis+Mysql+Layui实现功能完善的原创文学CMS系统 SpringBoot+MyBatis+Mysql+Layui实现功能完善的原创文学CMS系统 SpringBoot+...
springboot+mybatis+mysql最简单demospringboot+mybatis+mysql最简单demospringboot+mybatis+mysql最简单demospringboot+mybatis+mysql最简单demospringboot+mybatis+mysql最简单demospringboot+mybatis+mysql最简单...
这是一个java WEB小项目,用 Maven Springboot+mybatis+mysql+redis集成的小项目例子,可供新手进行参考学习,数据库是mysql 5.5 ,只是一个简单的小参考例子,测试过可以正常运行
后端: SpringBoot + MyBatis + MySQL 前端: Vue + ElementUI 教育培训机构管理系统,此系统为前后端分离方式开发的系统,技术栈为: 后端: SpringBoot + MyBatis + MySQL 前端: Vue + ElementUI 教育...
Spring mvc + mybatis+mysql 很简单的增删改查页面 适合新手学习
maven+springMVC+mybatis+velocity+mysql+junit项目框架搭建
基于Springboot+Mybatis+Mysql 实现的成绩管理系统 基于Springboot+Mybatis+Mysql 实现的成绩管理系统 基于Springboot+Mybatis+Mysql 实现的成绩管理系统 基于Springboot+Mybatis+Mysql 实现的成绩管理系统 基于...
基于SpringBoot+Mybatis+mysql的毕业生就业信息分析系统 基于SpringBoot+Mybatis+mysql的毕业生就业信息分析系统 基于SpringBoot+Mybatis+mysql的毕业生就业信息分析系统 基于SpringBoot+Mybatis+mysql的毕业生就业...
毕设项目:基于SpringBoot+MyBatis+mysql的飞机订票系统 毕设项目:基于SpringBoot+MyBatis+mysql的飞机订票系统 毕设项目:基于SpringBoot+MyBatis+mysql的飞机订票系统 毕设项目:基于SpringBoot+MyBatis+mysql的...
springboot+mybatis+mysql+layUI+thymeleaf实现对学员信息的增删改查
Springboot+MyBatis+mysql+jsp页面跳转详细示例
该项目主要技术架构包括:spring+springMVC+Mybatis+MYSQL+Redis+Maven,适合初学者,简单易懂。
springboot+mybatis+mysql+jsp+bootstrap+web.xml例子
ORM:MyBatis 数据库:MySQL 5.7 分布式缓存:Redis 本地缓存:Caffeine 消息队列:Kafka 2.13-2.7.0 搜索引擎:Elasticsearch 6.4.3 安全:Spring Security 邮件任务:Spring Mail 分布式定时任务:Spring Quartz ...
spring-boot+mybatis+mysql+maven整合,有教程,可以去我的博客看
spring+mybatis+mysql实现的用户登录功能
后端: SpringBoot + MyBatis + MySQL 前端: Vue + ElementPlus + Echarts 此系统为前后端分离方式开发的系统,技术栈为 后端: SpringBoot + MyBatis + MySQL 前端: Vue + ElementPlus + Echarts 此系统为...
基于SpringBoot+Mybatis+Mysql技术栈的前后端分离外卖管理系统.zip基于SpringBoot+Mybatis+Mysql技术栈的前后端分离外卖管理系统.zip基于SpringBoot+Mybatis+Mysql技术栈的前后端分离外卖管理系统.zip基于SpringBoot...
基于Springboot+MyBatis+MySQL实现多功能个人博客系统 项目经过严格测试,确保可以运行! 功能点: 用户注册登录功能 Markdown文章发布功能 文章分页显示 文章分类 文章标签/标签云 文章点击量 阅读排行 赞...
Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境