spring jpa 获取自增id

1.为实体类的id注解 @GeneratedValue(strategy=GenerationType.IDENTITY) 指定id的生成策略

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "article_id", nullable = false)
    public int getArticleId() {
        return articleId;
    }

    public void setArticleId(int articleId) {
        this.articleId = articleId;
    }

2.获取自增id

 ArticleEntity article=articleRepository.saveAndFlush(articleEntity);
 int id=article.getArticleId();


注意:如果不加注解,调用savaAndFlush()函数后id为默认id

关于id的注解,推荐以下博客:http://blog.csdn.net/fancylovejava/article/details/7438660

你可能感兴趣的:(spring,mvc开发问题)