springBoot jpa 表单关联查询
1、创建两个实体类
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.*; @Entity @Table(name = "link") public class Link { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; private String name; private String link; private String img; private Integer code; private String shijian; public Link(){ } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getImg() { return img; } public void setImg(String img) { this.img = img; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getShijian() { return shijian; } public void setShijian(String shijian) { this.shijian = shijian; } }
import javax.persistence.*; @Entity @Table(name = "Link_url") public class LinkUrl{ @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @ManyToOne private Link link; public LinkUrl(){ } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Link getLink() { return link; } public void setLink(Link link) { this.link = link; } }
其中在第二个实体类中
@ManyToOne private Link link;
是其关联字段
2、controller 查询
Link <LinkUrl> datas = linkUrlRepository.findAll();
查询结果中 link中的对象就是关联的Link对象
查询 数据中 link对象里面的id为6的数据
Link <LinkUrl> datas = linkUrlRepository.findByLinkId(6);