宇筱博客

  • 解决办法
  • 学无止境
  • 记录时光
  • 百宝箱
宇筱博客
点滴记忆,汇聚成海。
  1. 首页
  2. 解决办法
  3. 正文

mybatis 中 useGeneratedKeys 和 keyProperty 含义

2021年1月1日 447点热度 0人点赞 0条评论

MyBatis如何获取插入记录的自增长字段值:

第一步:

   在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名!

<insert id="insert" parameterType="Spares" useGeneratedKeys="true" keyProperty="id">
    insert into spares(spares_id,spares_name,spares_type_id,spares_spec) values(#{id},#{name},#{typeId},#{spec})
</insert>

第二步:

   Mybatis执行完插入语句后,自动将自增长值赋值给对象Spares的属性id。因此,可通过Spares对应的getter方法获取!

/**
 * 新增备件
 * @author hellostory
 * @param spares
 * @return
 */
@RequestMapping(value = "/insert")
@ResponseBody
public JsonResponse insert(Spares spares) {
    int count = sparesService.insert(spares);      //count>0 表示成功
    System.out.println( "刚刚插入记录的主键自增长值为:" + spares.getId());

    //如果没有useGeneratedKeys = "true"和keyProperty = "id", 下面 insert 之后的 user.getId() 是无法获取 id 值的
    public void insert(User user) {
        int count = userMapper.insert(user);
        System.out.println("共插入" + count + "条记录!"  + "\n刚刚插入记录的主键自增长值为:" + user.getId());
    }

原文链接:https://www.cnblogs.com/leeego-123/p/10724488.html

标签: 暂无
最后更新:2021年1月1日

小渔民

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2025 宇筱博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备15017825号-2