spring data 介紹
spring data 出現(xiàn)目的 為了簡化、統(tǒng)一 持久層 各種實現(xiàn)技術(shù) api ,所以 spring data 提供一套標(biāo)準(zhǔn) api 和 不同持久層整合技術(shù)實現(xiàn) .
自己開發(fā) repository 只需要繼承 jparepository 接口crudrepository
save、 delete、 deteleall、 findall、 findone、 count
pagingandsortingrepository
findall(sort) 基于排序的查詢、 findall(pageable) 基于分頁的查詢
spring data query 使用 實現(xiàn)條件查詢
第一種 根據(jù)方法命名規(guī)則自動生成
基于一列查詢等值查詢 findby 列名 例如: findbyname(string name);
基于一列模糊查詢 findby 列名 like 例如: findbynamelike(string name)
基于兩列等值查詢 findby 列名 and 列名 例如: findbyusernameandpassword(string username, string password )
第二種 不按命名規(guī)則寫的查詢方法,可以配置@query 綁定 jpal 語句或者 sql 語句
第三種 不按命名規(guī)則寫的查詢方法 配置@query 沒寫語句 , 實體類 @namedquery 定義
帶有條件 修改和刪除操作
使用@query 注解完成 , 搭配使用@modifying 標(biāo)記修改、刪除操作
將記錄 1 的 最小長度改為 15
注意:使用單體測試,測試 dao ,要添加事務(wù),設(shè)置事務(wù)不回滾
代碼實現(xiàn)
1.前端頁面端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//首先需要使用到表單序列化為json對象的方法 //將表單序列化為json對象 $.fn.serializejson=function(){ var serializeobj={}; var array= this .serializearray(); var str= this .serialize(); $(array).each(function(){ if (serializeobj[ this .name]){ if ($.isarray(serializeobj[ this .name])){ serializeobj[ this .name].push( this .value); } else { serializeobj[ this .name]=[serializeobj[ this .name], this .value]; } } else { serializeobj[ this .name]= this .value; } }); return serializeobj; }; |
2.獲取到giid表單信息
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
|
$(function(){ // 先將body隱藏,再顯示,不會出現(xiàn)頁面刷新效果 $( "body" ).css({visibility: "visible" }); // 信息表格 $( '#grid' ).datagrid( { iconcls : 'icon-forward' , fit : true , border : false , rownumbers : true , striped : true , pagelist: [ 30 , 50 , 100 ], pagination : true , toolbar : toolbar, url : "../../courier_pagequery.action" , idfield : 'id' , columns : columns, ondblclickrow : dodblclickrow //按條件查詢 $( "#searchbtn" ).click(function(){ //將searchfrom表單中的數(shù)據(jù)轉(zhuǎn)成json數(shù)據(jù) var params = $( "#searchform" ).serializejson(); //將json對象,綁定到datagrid上,完成帶有條件查詢的請求 $( "#grid" ).datagrid( 'load' ,params); //關(guān)閉查詢窗口 $( "#searchwindow" ).window( 'close' ); }); }); }); |
3.后臺代碼
action操作
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
|
@suppresswarnings ( "all" ) @parentpackage ( "json-default" ) @namespace ( "/" ) @controller @scope ( "prototype" ) public class courieraction extends actionsupport implements modeldriven<courier>{ //模型驅(qū)動 private courier courier = new courier(); @override public courier getmodel() { return courier; } //注入courierservice @autowired private courierservice courierserive; /** * 保存 */ @action (value= "curier_save" ,results={ @result (name= "success" , location= "./pages/base/courier.html" ,type= "redirect" )}) public string save(){ //調(diào)用業(yè)務(wù)層 courierserive.save(courier); return success; } //--分頁查詢所有取派員數(shù)據(jù)--- //接收參數(shù) 屬性驅(qū)動 private int page; private int rows; public void setpage( int page) { this .page = page; } public void setrows( int rows) { this .rows = rows; } /** * 分頁查詢所有的信息 */ @action (value= "courier_pagequery" ,results={ @result (name= "success" ,type= "json" )}) public string pagequery(){ //調(diào)用spring的方法 pageable pageable = new pagerequest(page- 1 , rows); //根據(jù)查詢條件,構(gòu)造specification條件查詢對象 specification<courier> specification = new specification<courier>() { /** * 構(gòu)造條件查詢方法,如果方法返回null 代表無條件查詢 * root 參數(shù) 獲取條件表達(dá)式 * criteriaquery 參數(shù) 構(gòu)造簡單查詢條件返回,提供where方法 * criteriabuilder 參數(shù) 構(gòu)造predicate對象,條件對象,構(gòu)造復(fù)雜查詢效果 */ @override public predicate topredicate(root<courier> root, criteriaquery<?> query, criteriabuilder cb) { //將查詢到的結(jié)果放到集合中 list<predicate> list = new arraylist<predicate>(); //單表查詢(查詢當(dāng)前對象對應(yīng)的數(shù)據(jù)表),查詢工號,等值查詢 if (stringutils.isnotblank(courier.getcouriernum())){ predicate p1 =cb.equal(root.get( "couriernum" ).as(string. class ), courier.getcouriernum()); list.add(p1); } if (stringutils.isnotblank(courier.getcompany())){ //查詢所屬單位,模糊查詢 predicate p2 = cb.like(root.get( "company" ).as(string. class ), "%" +courier.getcompany()+ "%" ); list.add(p2); } if (stringutils.isnotblank(courier.gettype())){ //查詢類型,等值查詢 predicate p3 = cb.equal(root.get( "type" ).as(string. class ),courier.gettype()); list.add(p3); } //多表查詢,查詢標(biāo)準(zhǔn) join<courier, standard> standardroot = root.join( "standard" ,jointype.inner); if (courier.getstandard() != null && stringutils.isnotblank(courier.getstandard().getname())){ //進(jìn)行模糊查詢 predicate p4 = cb.like(standardroot.get( "name" ).as(string. class ), "%" +courier.getstandard().getname()+ "%" ); list.add(p4); } //new predicate[0] 代表一個泛型,返回類型是predicate return cb.and(list.toarray( new predicate[ 0 ])); } }; //調(diào)用業(yè)務(wù)層 page<courier> pagedata=courierserive.findall(specification,pageable); //壓入值棧對象 //根據(jù)查詢結(jié)果封裝datagrid需要的數(shù)據(jù)格式 map<string,object> result = new hashmap<string,object>(); result.put( "total" , pagedata.gettotalelements()); result.put( "rows" , pagedata.getcontent()); //壓入值棧返回 actioncontext.getcontext().getvaluestack().push(result); return success; } } |
service層操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * 條件查詢所有的快遞員信息 */ @override public page<courier> findall(specification<courier> specification, pageable pageable) { return courierrepository.findall(specification,pageable); } |
dao操作,dao接口需要繼承兩個接口
spring data 還是非常強(qiáng)大的,封裝了hibernate的很多方法,我們可以直接拿來用
1
2
3
4
5
|
public interface courierrepository extends jparepository<courier, integer>, jpaspecificationexecutor<courier>{ } |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/mrccjj/p/7253497.html?utm_source=tuicool&utm_medium=referral