ITPub博客

首页 > > > ibatis annotations 注解方式返回刚插入的自增长主键ID的值--转

ibatis annotations 注解方式返回刚插入的自增长主键ID的值--转

作者:zhmw 时间:2020-02-04 11:56:20 0 删除 编辑

原文地址:http://www.blogs8.cn/posts/WWpt35l

mybatis提供了注解方式编写sql,省去了配置并编写xml mapper文件的麻烦,今天遇到了获取自增长主键返回值的问题,发现相关问答比较少,还好最后还是圆满解决了,现把重点记录一下,解决问题的关键就是以下几行代码:

1     @Insert("insert into Product(title, image, price, detail, summary, seller) values(#{title},#{image},#{price},#{detail},#{summary},#{seller})")
2     @Options(useGeneratedKeys=true, keyProperty="id")//添加该行,product中的id将被自动添加
3     public Integer insertProduct(Product product);

添加上面的第二行就可以了,其中第二个参数据说可以不需要

添加该注解后

在数据库中添加成功后,product的id属性就会被默认赋值。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69901620/viewspace-3157416/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-12-04

  • 博文量
    295
  • 访问量
    9532