码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-07-13 21:58:25      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:summary   integer   sel   app   post   记录   into   insert   注解   

原文地址: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属性就会被默认赋值。

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

标签:summary   integer   sel   app   post   记录   into   insert   注解   

原文地址:http://www.cnblogs.com/davidwang456/p/7163062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!