根据前两篇博文,相信大伙看完之后,应该对Spring Data JPA有了一个感性的认识。接下来,我们来看看Spring Data JPA的查询方法
1
|
UserInfo 实体:前文提到过,用myeclipse的生成JPA实体逆向生成:
|
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
package
com.example.entity;
import
java.sql.Timestamp;
import
javax.persistence.Column;
import
javax.persistence.Entity;
import
javax.persistence.Id;
import
javax.persistence.Table;
import
javax.persistence.UniqueConstraint;
/**
* UserInfo entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table
(name=
"user_info"
,catalog=
"rbac"
, uniqueConstraints =
@UniqueConstraint
(columnNames=
"username"
)
)
public
class
UserInfo
implements
java.io.Serializable {
// Fields
private
String id;
private
String username;
private
String password;
private
String sex;
private
Timestamp birthday;
private
String email;
private
String contact;
private
String onlineState;
private
Timestamp lastLoginTime;
private
Timestamp lastLogoutTime;
private
String lastLoginIp;
private
String userState;
private
Integer listOrder;
private
String createUser;
private
Timestamp createDatetime;
private
String modifyUser;
private
Timestamp modifyDatetime;
private
String deleteFlag;
// Constructors
/** default constructor */
public
UserInfo() {
}
/** minimal constructor */
public
UserInfo(String id, String username, String password) {
this
.id = id;
this
.username = username;
this
.password = password;
}
/** full constructor */
public
UserInfo(String id, String username, String password, String sex, Timestamp birthday, String email, String contact, String onlineState, Timestamp lastLoginTime, Timestamp lastLogoutTime, String lastLoginIp, String userState, Integer listOrder, String createUser, Timestamp createDatetime, String modifyUser, Timestamp modifyDatetime, String deleteFlag) {
this
.id = id;
this
.username = username;
this
.password = password;
this
.sex = sex;
this
.birthday = birthday;
this
.email = email;
this
.contact = contact;
this
.onlineState = onlineState;
this
.lastLoginTime = lastLoginTime;
this
.lastLogoutTime = lastLogoutTime;
this
.lastLoginIp = lastLoginIp;
this
.userState = userState;
this
.listOrder = listOrder;
this
.createUser = createUser;
this
.createDatetime = createDatetime;
this
.modifyUser = modifyUser;
this
.modifyDatetime = modifyDatetime;
this
.deleteFlag = deleteFlag;
}
// Property accessors
@Id
@Column
(name=
"id"
, unique=
true
, nullable=
false
, length=
50
)
public
String getId() {
return
this
.id;
}
public
void
setId(String id) {
this
.id = id;
}
@Column
(name=
"username"
, unique=
true
, nullable=
false
, length=
50
)
public
String getUsername() {
return
this
.username;
}
public
void
setUsername(String username) {
this
.username = username;
}
@Column
(name=
"password"
, nullable=
false
, length=
50
)
public
String getPassword() {
return
this
.password;
}
public
void
setPassword(String password) {
this
.password = password;
}
@Column
(name=
"sex"
, length=
1
)
public
String getSex() {
return
this
.sex;
}
public
void
setSex(String sex) {
this
.sex = sex;
}
@Column
(name=
"birthday"
, length=
0
)
public
Timestamp getBirthday() {
return
this
.birthday;
}
public
void
setBirthday(Timestamp birthday) {
this
.birthday = birthday;
}
@Column
(name=
"email"
, length=
50
)
public
String getEmail() {
return
this
.email;
}
public
void
setEmail(String email) {
this
.email = email;
}
@Column
(name=
"contact"
, length=
50
)
public
String getContact() {
return
this
.contact;
}
public
void
setContact(String contact) {
this
.contact = contact;
}
@Column
(name=
"online_state"
, length=
1
)
public
String getOnlineState() {
return
this
.onlineState;
}
public
void
setOnlineState(String onlineState) {
this
.onlineState = onlineState;
}
@Column
(name=
"last_login_time"
, length=
0
)
public
Timestamp getLastLoginTime() {
return
this
.lastLoginTime;
}
public
void
setLastLoginTime(Timestamp lastLoginTime) {
this
.lastLoginTime = lastLoginTime;
}
@Column
(name=
"last_logout_time"
, length=
0
)
public
Timestamp getLastLogoutTime() {
return
this
.lastLogoutTime;
}
public
void
setLastLogoutTime(Timestamp lastLogoutTime) {
this
.lastLogoutTime = lastLogoutTime;
}
@Column
(name=
"last_login_ip"
, length=
50
)
public
String getLastLoginIp() {
return
this
.lastLoginIp;
}
public
void
setLastLoginIp(String lastLoginIp) {
this
.lastLoginIp = lastLoginIp;
}
@Column
(name=
"user_state"
, length=
50
)
public
String getUserState() {
return
this
.userState;
}
public
void
setUserState(String userState) {
this
.userState = userState;
}
@Column
(name=
"list_order"
)
public
Integer getListOrder() {
return
this
.listOrder;
}
public
void
setListOrder(Integer listOrder) {
this
.listOrder = listOrder;
}
@Column
(name=
"create_user"
, length=
20
)
public
String getCreateUser() {
return
this
.createUser;
}
public
void
setCreateUser(String createUser) {
this
.createUser = createUser;
}
@Column
(name=
"create_datetime"
, length=
0
)
public
Timestamp getCreateDatetime() {
return
this
.createDatetime;
}
public
void
setCreateDatetime(Timestamp createDatetime) {
this
.createDatetime = createDatetime;
}
@Column
(name=
"modify_user"
, length=
20
)
public
String getModifyUser() {
return
this
.modifyUser;
}
public
void
setModifyUser(String modifyUser) {
this
.modifyUser = modifyUser;
}
@Column
(name=
"modify_datetime"
, length=
0
)
public
Timestamp getModifyDatetime() {
return
this
.modifyDatetime;
}
public
void
setModifyDatetime(Timestamp modifyDatetime) {
this
.modifyDatetime = modifyDatetime;
}
@Column
(name=
"delete_flag"
, length=
1
)
public
String getDeleteFlag() {
return
this
.deleteFlag;
}
public
void
setDeleteFlag(String deleteFlag) {
this
.deleteFlag = deleteFlag;
}
}
|
下面来看看完整的UserInfoService.java
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
|
package
com.example.service.impl;
import
java.util.List;
import
org.springframework.data.domain.Pageable;
import
org.springframework.data.domain.Slice;
import
org.springframework.data.domain.Sort;
import
com.example.common.BaseService;
import
com.example.entity.UserInfo;
public
interface
UserInfoService
extends
BaseService<UserInfo, String>{
//根据username查询用户信息
public
List<UserInfo> findByUsername(String username);
//根据用户名和密码查询用户信息
public
UserInfo findByUsernameAndPassword(String name, String password);
//原生SQL查询,且绑定参数的方式
//@Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1", nativeQuery = true)
//@Query("select u from UserInfo u where u.email = ?1")
List<UserInfo> findByEmail(String email);
//参数查询
// @Query("select u from User u where u.firstname = :firstname or u.lastname = :lastname")
// UserInfo findByLastnameOrFirstname(@Param("lastname") String lastname,
// @Param("firstname") String firstname);
// Enables the distinct flag for the query
List<UserInfo> findDistinctUserInfoByUsernameOrEmail(String name,
String email);
List<UserInfo> findUserInfoDistinctByUsernameOrEmail(String name,
String email);
List<UserInfo> findTop10ByUsername(String Username, Pageable pageable);
// Enabling ignoring case for an individual property
List<UserInfo> findByUsernameIgnoreCase(String name);
// Enabling ignoring case for all suitable properties
List<UserInfo> findByUsernameAndEmailAllIgnoreCase(String name,
String email);
// Enabling static ORDER BY for a query
List<UserInfo> findByUsernameOrderByUsernameAsc(String name);
List<UserInfo> findByUsernameOrderByUsernameDesc(String name);
//Between, LessThan, GreaterThan, Like
UserInfo findFirstByOrderByUsernameAsc();
//UserInfo findTopByOrderByAgeDesc();
//Page<UserInfo> queryFirst10ByUsername(String Username, Pageable pageable);
Slice<UserInfo> findTop3ByUsername(String Username, Pageable pageable);
List<UserInfo> findFirst10ByUsername(String Username, Sort sort);
}
|
上面的命名具有一定的规范,总结下来就是:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
find+全局修饰+By+实体属性名+Keyword+ .(其它实体属性)+OrderBy+
排序实体属性+排序方式
例如:
findDistnctByUsernameIgnoreCaseAndPasswordOrderByAgeDsc(String
firstName,String lastName){.}
其中:Distnct是全局修饰(非必须),Username和Password是实体的属性名,
And、IgnoreCase都是关键字,Age是排序属性,Desc是排序方式
常用词如下:
全局修饰:Distnct,Top,First
keyword:如图1,2所示
IgnoreCase,Betwen,Equals,LesThan,GreaterThan,After,Before.
排序方式:Asc,Desc
连接词:And,Or
|
关键字列表:
图1. 查询的关键字
上面的查询方式,详细大伙都已经非常的清楚了,毕竟上述查询有些也不能满足我们的需求,因此下一篇文章将讲述如何进行自定义的查询方式。
本文转自 w156445045 51CTO博客,原文链接:http://blog.51cto.com/enetq/1831029,如需转载请自行联系原作者