首页 > 其他分享 >springboot集成mybatis获取插入数据的主键

springboot集成mybatis获取插入数据的主键

时间:2022-09-19 00:23:29浏览次数:88  
标签:返回 insert springboot 主键 插入 student mybatis id

问题:

我们想在插入一条数据后同时能够返回这条数据在表中的id,Mybatis提供了@SelectKey注解。
student 为数据表,主键自增

SelectKey的四个属性:

  1. selectKey 会将 SELECT LAST_INSERT_ID()的结果放入到传入的实体类的主键里面,
  2. keyProperty对应的实体类中的主键的属性名,这里是 实体类中的id,因为它跟数据库的主键对应order
  3. AFTER 表示 SELECT LAST_INSERT_ID() 在insert执行之后执行,多用与自增主键,
  4. BEFORE 表示 SELECTLAST_INSERT_ID() 在insert执行之前执行,这样的话就拿不到主键了,这种适合那种主键不是自增的类型

mapper配置如下:

    <insert id="addGetKey" parameterType="com.javaone.passmybatis.entity.Student">
        <selectKey keyColumn="id" keyProperty="id" order="AFTER" resultType="int">
            select LAST_INSERT_ID()
        </selectKey>
        insert into student (name,age,email) values (#{name},#{age},#{email})
    </insert>

观察mybatis日志输出也是正常的

==>  Preparing: insert into student (name,age,email) values (?,?,?)
==> Parameters: string(String), 0(Integer), string(String)
<==    Updates: 1
==>  Preparing: select LAST_INSERT_ID()
==> Parameters: 
<==    Columns: LAST_INSERT_ID()
<==        Row: 22
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@8297b8d]
Student{id=22, name='string', email='string', age=0}

但是返回的结果一直是1,分析这个1可能是insert(student)执行成功返回1,失败返回0的结果,而不是我们想要的id。

解决办法:

我插入数据时插入的是一个bean,这个bean的类型就是上面我们提到的student,插入前它的id是空的;
当我们执行插入后,返回插入的结果,插入成功返回1,插入失败返回0,这就是为什么结果一直为1了。返回的id其实已经映射到了我们插入的bean中,即id值已经返回到user的id属性里了我们只要通过它的get方法就可以得到了。代码如下:

int result= studentDao.addGetKey(student);
System.out.println(student.toString());
System.out.println(student.getId());
return result;

标签:返回,insert,springboot,主键,插入,student,mybatis,id
From: https://www.cnblogs.com/zhouXX/p/16706344.html

相关文章

  • mybatis注解之@Mapper和@MapperScan的使用
    +目录mybatis注解之@Mapper和@MapperScan方式一:使用@Mapper注解方式二:使用@MapperScan注解@MapperScan和@Mapper区别及理解作用扫描一个包扫描多个包myb......
  • SpringBoot集成Mybatis 实现InsertOrUpdate功能
    需求场景在项目开发过程中,难免会遇到这样的场景:对一张表,当数据不存在的时候,进行insert插入操作;数据存在的时候,进行update更新操作;下面就来使用Mybatis的InsertOrUpdate功......
  • java框架——MyBatis
    MyBatis什么是MyBatis?MyBaits是一款优秀的持久层框架,用于简化JDBC开发MyBaits本事Apached的一个开源项目iBatis,2010年这个项目由apachesoftwarefoundation迁移到......
  • SSM框架之Mybatis基本概念及使用方法
    SSMSpingMVC+Spring+Mybatis SpringMVC充当Servlet可以理解为SpringMVC是Spring的WEB支持Mybatis充当Dao层Sping充当一个润滑油的角色 MybatisPlus国内......
  • springboot代码生成器
    一、使用springboot+mybatisplus+swagger完成如下操作1、创建数据库表如下channel字段名称中文类型长度主键外键自增约束cid栏目id......
  • springboot Condition 动态value
    packagecom.example.demo.condtion;importorg.springframework.context.annotation.Conditional;importjava.lang.annotation.*;@Target({ElementType.TYPE,Ele......
  • springboot Condition
    packagecom.example.demo.config;importcom.example.demo.condtion.ClassCondition;importcom.example.demo.domian.User;importorg.springframework.context.an......
  • 【博学谷学习记录】超强总结,用心分享 。SpringBoot 常用注解
    @RequestMapping@RequestMapping注解的主要用途是将Web请求与请求处理类中的方法进行映射。SpringMVC和SpringWebFlux都通过RquestMappingHandlerMapping和RequestMappi......
  • MyBatis标签之Select resultType和resultMap
    摘要:介绍MyBatis中Select标签的两个属性resultType和resultMap及其区别。1MyBatis动态SQL之if语句2MyBatis动态sql之where标签|转3MyBatis动态SQL之set标签|转4M......
  • Spring常用注解 以及 SpringBoot常用注解
    Spring常用注解以及SpringBoot常用注解既然提到了这个概念,那就从Spring先说起.SpringBoot就是封装后的Spring,相对于Spring,Boot省去了很多配置,也是很好的解决......