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

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

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

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

添加该注解后

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

上一篇:web负载均衡【总结归纳所有看过的资料的理论】


下一篇:Python前端html基础