广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Mybatis 在 insert 插入操作后返回主键 id的操作方法
  • 411
分享到

Mybatis 在 insert 插入操作后返回主键 id的操作方法

mybatis Insert返回插入的主键mybatis Insert返回主键 2022-12-15 12:12:05 411人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录mybatis 在 insert 插入操作后返回主键 id前提条件问题提出解决方法方法一方法二(推荐)方法三mybatis中Insert语句如何返回插入的主键方法一: 

Mybatis 在 insert 插入操作后返回主键 id

前提条件

假设我们这里有一个 Student 表,结构如下

sidnameage
101Jone18
102Jack20
103Tom28

其中主键 sid 是自增的,那么我们插入数据时就不用插入 sid,它会生成一个自增的 sid。

问题提出

这里有一个问题,我们执行插入语句之后,并不能获取到生成的 sid。

StudentDao 接口中的 insert 方法

boolean insertStudent(Student student);

StudentDao.xml 中的 insert 标签

<insert id="insertStudent" parameterType="Student">
    insert into student(name, age)
    VALUES (#{name} , #{age})
</insert>

单元测试类中的方法

@Test
public void insertUser() {
    Student student = new Student(0,"xxx", 28);

    boolean flag = studentDao.insertStudent(student);
    System.out.println(flag);
    System.out.println(student);
}

执行后的结果

这里并不能获取到生成的 sid,如果要获取这个 sid,还要根据 name 来查询数据库,而且 name 也需要是 unique 唯一性的。

那么,有没有办法让我们能够执行插入语句后,直接获取到生成的 sid 呢,当然是有的。

解决方法

方法一

修改 StudentDao.xml 中的 insert 标签,配置 useGeneratedKeys 和 keyProperty

<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="sid">
    insert into student(name, age)
    VALUES (#{name} , #{age})
</insert>

说明:

1、useGeneratedKeys=“true” 表示给主键设置自增长。

2、keyProperty=“sid” 表示将自增长后的 Id 赋值给实体类中的 sid 字段。

运行结果:成功返回了主键 sid

方法二(推荐)

修改 StudentDao.xml 中的 insert 标签,在 insert 标签中编写 selectKey 标签

<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="sid">
    insert into student(name, age)
    VALUES (#{name} , #{age})
</insert>

说明:

1、< insert> 标签中没有 resultType 属性,但是 < selecTKEy> 标签是有的。
2、order=“AFTER” 表示先执行插入语句,之后再执行查询语句。
3、keyProperty=“sid” 表示将自增长后的 Id 赋值给实体类中的 sid 字段。
4、SELECT LAST_INSERT_ID() 表示 Mysql 语法中查询出刚刚插入的记录自增长 Id。

运行结果:成功返回了主键 sid

方法三

这种方法需要在一定条件下才能使用,就是 name 需要是 unique,不可重复的。

这样才能在插入后,根据 name 来查询出主键 sid。

同样是修改 StudentDao.xml 中的 insert 标签,在 insert 标签中编写 selectKey 标签

<insert id="insertStudent" parameterType="Student">
    insert into student(name, age)
    VALUES (#{name} , #{age})

    <selectKey keyProperty="sid" order="AFTER" resultType="int">
        select sid from student where name = #{name}
    </selectKey>
</insert>

原理上面也讲了,就是在执行插入语句之后,再执行查询语句,将 sid 查出来。

不过我这里 name 并未设置 unique,所以不在这里进行测试,有兴趣可以自行测试。

补充知识点:

mybatis中Insert语句如何返回插入的主键

方法一:

mapper.java

    
    Integer insertDep(Department department);

xml

    <insert id="insertDep" parameterType="com.example.pojo.entity.Department">
        <selectKey keyProperty="departmentId" order="AFTER" resultType="java.lang.Integer ">
            select LAST_INSERT_ID()
        </selectKey>
        insert into sys_department(name,parentId,enabled,isParent)
        VALUES(#{name},#{parentId},1,0)
    </insert>

其中:

selectKey标签:将插入到数据库的某条记录的主键,返回到指定对象(user)对应属性中。
keyProperty:  指定返回的主键,存储在对象中(user)的哪个属性
order:相对于insert语句,selectKey标签中的sql的执行顺序。由于mysql的自增原理,执行完insert语句之后才将主键生成,所以这里selectKey的执行顺序为after。
resultType:  返回的主键对应的JAVA类型
LAST_INSERT_ID():  是mysql的函数,返回auto_increment自增列新记录id值。

 方法二

使用:

useGeneratedKeys="true" keyProperty="departmentId"
    <insert id="insertDep" parameterType="com.example.pojo.entity.Department" useGeneratedKeys="true" keyProperty="departmentId">
        insert into sys_department(departmentId,name,parentId,enabled,isParent)
        VALUES(uuid(),#{name},#{parentId},1,0)
    </insert>

到此这篇关于mybatis中Insert语句如何返回插入的主键的文章就介绍到这了,更多相关mybatis Insert返回插入的主键内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Mybatis 在 insert 插入操作后返回主键 id的操作方法

本文链接: https://www.lsjlt.com/news/174947.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作