美文网首页
springboot中有关一对多的介绍

springboot中有关一对多的介绍

作者: 一粒有梦想的痞老板 | 来源:发表于2018-11-21 11:12 被阅读0次

我们在很多阅读网站中都看过一个作者会编写很多文章并且发布出去,一个作者不可能只写一篇文章。所以我们在建立关系时,要把很多文章和一个用户绑在一起

  • 我们需要建立一个用户表,一个文章表
  • 这个时候,用户和文章就是一个一对多的关系
  • 我们为了方便建立数据库,就会在一的一方写几行代码,这样就可以自动生成数据库中他们之间的关系。
@Entity
@Data
public class User {
    @GeneratedValue
    @Id
private Integer userId;
private String userAccount;
private String userName;
private String userPassword;
private String userAvatar;
private String userIntroduction;
//文章
@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.REMOVE)
  @JoinColumn(name = "article_id")
 private List<Article> articleList=new ArrayList<>();

这样运行之后,数据库中文章表里就会出现一个article_id,它填的就是与这篇文章对应的用户id.

相关文章

网友评论

      本文标题:springboot中有关一对多的介绍

      本文链接:https://www.haomeiwen.com/subject/qqpqqqtx.html