服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringBoot JPA 表关联查询实例

SpringBoot JPA 表关联查询实例

2020-09-08 10:40漫步于成神之路男人 Java教程

本篇文章主要介绍了SpringBoot JPA 表关联查询实例,使用JPA原生的findBy语句实现,具有一定的参考价值,有兴趣的可以了解一下。

今天给大家介绍一下如何利用JPA实现表关联查询

今天给大家举一个一对多的关联查询,并且是使用JPA原生的findBy语句实现的。

例子中总共有两个实体类,一个是Floor(商品楼层类),另一个是FloorContent(商品楼层内容表)。下面看两张表的源代码:

Floor类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package cms.model;
import cms.model.base.BaseDomain;
import org.hibernate.annotations.GenericGenerator;
 
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
/**
 * Created by Roney on 2016/10/10.
 * 楼层管理
 *
 */
@Entity
@Table(indexes = {@Index(name = "idx_floor_user",columnList = "user_id")})
public class Floor extends BaseDomain implements Serializable {
 
  @Id
  @GenericGenerator(name = "PKUUID", strategy = "uuid2")
  @GeneratedValue(generator = "PKUUID")
  @Column(length = 36)
  protected String id;
 
  /**
   * 发布用户ID
   */
  @Column(length = 36,name = "user_id")
  private String userId;
 
  /**
   * 楼层名称
   */
  private String name;
 
  /**
   * 楼层的模板路径
   */
  private String templateUrl;
 
  /**
   * 类型
   * 1.管理端
   * 2.供应商
   */
  private Integer type;
 
 
  /**
   * 排序
   */
  @Column(name = "show_index", nullable = false)
  private Integer showIndex;
 
  /**
   * 是否禁用
   * */
 
 
  @Column(nullable = false)
  private Boolean isDisable=false;
 
  @OneToMany(fetch = FetchType.LAZY,mappedBy = "floor")
  private List<FloorContent> floorContents;
 
  public List<FloorContent> getFloorContents() {
    return floorContents;
  }
 
  public void setFloorContents(List<FloorContent> floorContents) {
    this.floorContents = floorContents;
  }
 
  public String getId() {
    return id;
  }
 
  public void setId(String id) {
    this.id = id;
  }
 
  public String getUserId() {
    return userId;
  }
 
  public void setUserId(String userId) {
    this.userId = userId;
  }
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
 
  public String getTemplateUrl() {
    return templateUrl;
  }
 
  public void setTemplateUrl(String templateUrl) {
    this.templateUrl = templateUrl;
  }
 
  public Integer getShowIndex() {
    return showIndex;
  }
 
  public void setShowIndex(Integer showIndex) {
    this.showIndex = showIndex;
  }
 
  public Boolean getDisable() {
    return isDisable;
  }
 
  public void setDisable(Boolean disable) {
    isDisable = disable;
  }
 
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
 
    Floor floor = (Floor) o;
 
    return id != null ? id.equals(floor.id) : floor.id == null;
 
  }
 
  @Override
  public int hashCode() {
    return id != null ? id.hashCode() : 0;
  }
}

FloorContent类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package cms.model;
 
import cms.model.base.BaseDomain;
import org.hibernate.annotations.GenericGenerator;
 
import javax.persistence.*;
import java.io.Serializable;
 
/**
 * Created by Roney on 2016/10/10.
 * 楼层的内容
 */
 
@Entity
@Table(indexes = {@Index(name = "idx_floor_content_user", columnList = "user_id")})
public class FloorContent extends BaseDomain implements Serializable {
 
  @Id
  @GenericGenerator(name = "PKUUID", strategy = "uuid2")
  @GeneratedValue(generator = "PKUUID")
  @Column(length = 36)
  protected String id;
 
  /**
   * 发布用户ID
   */
  @Column(length = 36, name = "user_id")
  private String userId;
 
  /**
   * 內容名稱
   */
  private String name;
 
  /**
   *
   * 內容圖片
   */
  @Column(length = 256)
  private String contentImageUrl;
 
  /**
   * 類型
   * 1.超鏈接
   * 2.圖片檢索
   */
  private Integer type;
 
  /**
   * 超鏈接url
   */
  private String linkUrl;
 
  /**
   * 圖片檢索內容
   */
  private String picSearchContent;
 
  /**
   * 排序
   */
  @Column(name = "show_index", nullable = false)
  private Integer showIndex;
 
  /**
   * 是否禁用
   */
 
  @Column(nullable = false)
  private Boolean isDisable = false;
 
  @ManyToOne
  @JoinColumn(name = "floor_id",foreignKey = @ForeignKey(name = "fk_floor_fc"))
  private Floor floor;
 
  public Floor getFloor() {
    return floor;
  }
 
  public void setFloor(Floor floor) {
    this.floor = floor;
  }
 
  public String getId() {
    return id;
  }
 
  public void setId(String id) {
    this.id = id;
  }
 
  public String getUserId() {
    return userId;
  }
 
  public void setUserId(String userId) {
    this.userId = userId;
  }
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
 
  public String getContentImageUrl() {
    return contentImageUrl;
  }
 
  public void setContentImageUrl(String contentImageUrl) {
    this.contentImageUrl = contentImageUrl;
  }
 
  public Integer getType() {
    return type;
  }
 
  public void setType(Integer type) {
    this.type = type;
  }
 
  public String getLinkUrl() {
    return linkUrl;
  }
 
  public void setLinkUrl(String linkUrl) {
    this.linkUrl = linkUrl;
  }
 
  public String getPicSearchContent() {
    return picSearchContent;
  }
 
  public void setPicSearchContent(String picSearchContent) {
    this.picSearchContent = picSearchContent;
  }
 
  public Integer getShowIndex() {
    return showIndex;
  }
 
  public void setShowIndex(Integer showIndex) {
    this.showIndex = showIndex;
  }
 
  public Boolean getDisable() {
    return isDisable;
  }
 
  public void setDisable(Boolean disable) {
    isDisable = disable;
  }
 
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
 
    FloorContent that = (FloorContent) o;
 
    return id != null ? id.equals(that.id) : that.id == null;
 
  }
 
  @Override
  public int hashCode() {
    return id != null ? id.hashCode() : 0;
  }
}

实体类已经出来了,现在具体说说怎么利用JPA中findBy来实现关联查询:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package cms.model.repository;
import cms.model.Floor;
import cms.model.FloorContent;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
/**
 * Created by Roney on 2016/10/10.
 * Created by Roney on 2016/10/10.
 * 楼层内容管理dao类
 */
public interface FloorContentRepos extends JpaRepository<FloorContent,String>{
  public Page<FloorContent> findByFloor_IdAndIsDeleteOrderByShowIndexAsc(String floorId,boolean b, Pageable pageable);
}

从例子中就可以看出JPA关联查询主要在“_”这个符号的使用,下面来给大家具体的介绍一下这个符号到底代表什么含义。

首先findBy是必须写的,表示使用JPA规则进行查询。

如果查询的是本张表中的内容,例如查询本张表中的name字段就可以这么写:findByName()。

如果查询的是楼层中的name字段就可以这么写:findByFloor_Name()。

如果是既要查询本张表中的name字段,也要查询楼层中的name字段,就可以这么写:findByFloor_NameAndName()。

从上面的案例就可以看出可以在findBy后面添加要关联的实体类,然后在实体类后面写上“_”,"_"符号后面是添加关联表的字段而不是本身表的字段,这点要记住。如何还想关联更多的表可以在后面添加:And+表名字+“_”+表中要查询的字段。或者只是想关联本身的查询字段可以在后面添加:And+查询的字段。

千万不要写错了,写错的话运行都运行不起来的。所以写的时候要多看看是否符合规则。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/linzhiqiang0316/article/details/53022683

延伸 · 阅读

精彩推荐