国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringBoot JPA 表關(guān)聯(lián)查詢實(shí)例

SpringBoot JPA 表關(guān)聯(lián)查詢實(shí)例

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

本篇文章主要介紹了SpringBoot JPA 表關(guān)聯(lián)查詢實(shí)例,使用JPA原生的findBy語句實(shí)現(xiàn),具有一定的參考價(jià)值,有興趣的可以了解一下。

今天給大家介紹一下如何利用JPA實(shí)現(xiàn)表關(guān)聯(lián)查詢

今天給大家舉一個(gè)一對多的關(guān)聯(lián)查詢,并且是使用JPA原生的findBy語句實(shí)現(xiàn)的。

例子中總共有兩個(gè)實(shí)體類,一個(gè)是Floor(商品樓層類),另一個(gè)是FloorContent(商品樓層內(nèi)容表)。下面看兩張表的源代碼:

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;
 
  /**
   * 發(fā)布用戶ID
   */
  @Column(length = 36,name = "user_id")
  private String userId;
 
  /**
   * 樓層名稱
   */
  private String name;
 
  /**
   * 樓層的模板路徑
   */
  private String templateUrl;
 
  /**
   * 類型
   * 1.管理端
   * 2.供應(yīng)商
   */
  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.
 * 樓層的內(nèi)容
 */
 
@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;
 
  /**
   * 發(fā)布用戶ID
   */
  @Column(length = 36, name = "user_id")
  private String userId;
 
  /**
   * 內(nèi)容名稱
   */
  private String name;
 
  /**
   *
   * 內(nèi)容圖片
   */
  @Column(length = 256)
  private String contentImageUrl;
 
  /**
   * 類型
   * 1.超鏈接
   * 2.圖片檢索
   */
  private Integer type;
 
  /**
   * 超鏈接url
   */
  private String linkUrl;
 
  /**
   * 圖片檢索內(nèi)容
   */
  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;
  }
}

實(shí)體類已經(jīng)出來了,現(xiàn)在具體說說怎么利用JPA中findBy來實(shí)現(xiàn)關(guān)聯(lián)查詢:

?
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.
 * 樓層內(nèi)容管理dao類
 */
public interface FloorContentRepos extends JpaRepository<FloorContent,String>{
  public Page<FloorContent> findByFloor_IdAndIsDeleteOrderByShowIndexAsc(String floorId,boolean b, Pageable pageable);
}

從例子中就可以看出JPA關(guān)聯(lián)查詢主要在“_”這個(gè)符號(hào)的使用,下面來給大家具體的介紹一下這個(gè)符號(hào)到底代表什么含義。

首先findBy是必須寫的,表示使用JPA規(guī)則進(jìn)行查詢。

如果查詢的是本張表中的內(nèi)容,例如查詢本張表中的name字段就可以這么寫:findByName()。

如果查詢的是樓層中的name字段就可以這么寫:findByFloor_Name()。

如果是既要查詢本張表中的name字段,也要查詢樓層中的name字段,就可以這么寫:findByFloor_NameAndName()。

從上面的案例就可以看出可以在findBy后面添加要關(guān)聯(lián)的實(shí)體類,然后在實(shí)體類后面寫上“_”,"_"符號(hào)后面是添加關(guān)聯(lián)表的字段而不是本身表的字段,這點(diǎn)要記住。如何還想關(guān)聯(lián)更多的表可以在后面添加:And+表名字+“_”+表中要查詢的字段。或者只是想關(guān)聯(lián)本身的查詢字段可以在后面添加:And+查詢的字段。

千萬不要寫錯(cuò)了,寫錯(cuò)的話運(yùn)行都運(yùn)行不起來的。所以寫的時(shí)候要多看看是否符合規(guī)則。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久国产视频 | 欧美一区二区三区在线视频 | 日韩精品99久久久久中文字幕 | av中文字幕在线 | 黄a在线观看 | 欧美黄色网 | 中文字幕亚洲一区二区va在线 | 欧美在线观看一区 | 精品久久中文字幕 | 免费日韩一级片 | 91视频8mav| av在线免费观看网站 | 天天拍拍天天干 | 干片网| 国产一级一级国产 | 国产综合视频在线观看 | 欧美视频第一区 | 国产成人久久精品麻豆二区 | 天天干天天操 | www.久久视频 | 国产毛片欧美毛片久久久 | 久色网 | 久久精品国产清自在天天线 | 国产黄色小视频 | 中文在线观看视频 | 亚洲成人精品在线观看 | 国产精品美女久久久久久久网站 | 亚洲精品二区 | 99久久国语露脸精品对白 | 激情毛片| 亚洲精品一区二区三区在线 | 一级看片 | 亚洲视频在线免费观看 | 成年人免费在线观看视频网站 | 日韩中文字幕在线观看视频 | 最新中文字幕视频 | 日韩在线观看第一页 | 日韩不卡一区二区三区 | www日韩| 天操天天干 | 欧美日韩视频第一页 |