15
15
import java .util .stream .Collectors ;
16
16
import java .util .stream .Stream ;
17
17
import org .seasar .doma .jdbc .Config ;
18
- import org .seasar .doma .jdbc .criteria .Entityql ;
19
- import org .seasar .doma .jdbc .criteria .NativeSql ;
18
+ import org .seasar .doma .jdbc .criteria .QueryDsl ;
20
19
import org .seasar .doma .jdbc .criteria .expression .Expressions ;
21
20
import org .seasar .doma .jdbc .criteria .option .LikeOption ;
22
21
import org .seasar .doma .jdbc .criteria .statement .StreamMappable ;
23
22
import org .seasar .doma .jdbc .criteria .tuple .Tuple2 ;
24
23
25
24
public class EmployeeRepository {
26
25
27
- private final Entityql entityql ;
28
-
29
- private final NativeSql nativeSql ;
26
+ private final QueryDsl queryDsl ;
30
27
31
28
public EmployeeRepository (Config config ) {
32
29
Objects .requireNonNull (config );
33
- entityql = new Entityql (config );
34
- nativeSql = new NativeSql (config );
30
+ queryDsl = new QueryDsl (config );
35
31
}
36
32
37
33
public Employee selectById (Integer id ) {
38
34
Employee_ e = new Employee_ ();
39
- return entityql .from (e ).where (c -> c .eq (e .id , id )).fetchOne ();
35
+ return queryDsl .from (e ).where (c -> c .eq (e .id , id )).fetchOne ();
40
36
}
41
37
42
38
public List <Employee > selectByAgeRange (Age min , Age max ) {
43
39
Employee_ e = new Employee_ ();
44
- return entityql
40
+ return queryDsl
45
41
.from (e )
46
42
.where (
47
43
c -> {
@@ -53,22 +49,22 @@ public List<Employee> selectByAgeRange(Age min, Age max) {
53
49
54
50
public List <Employee > selectByName (String name ) {
55
51
Employee_ e = new Employee_ ();
56
- return entityql .from (e ).where (c -> c .eq (e .name , name )).fetch ();
52
+ return queryDsl .from (e ).where (c -> c .eq (e .name , name )).fetch ();
57
53
}
58
54
59
55
public List <Employee > selectByNames (List <String > names ) {
60
56
Employee_ e = new Employee_ ();
61
- return entityql .from (e ).where (c -> c .in (e .name , names )).fetch ();
57
+ return queryDsl .from (e ).where (c -> c .in (e .name , names )).fetch ();
62
58
}
63
59
64
60
public List <Employee > selectByAges (List <Age > ages ) {
65
61
Employee_ e = new Employee_ ();
66
- return entityql .from (e ).where (c -> c .in (e .age , ages )).fetch ();
62
+ return queryDsl .from (e ).where (c -> c .in (e .age , ages )).fetch ();
67
63
}
68
64
69
65
public List <Employee > selectByNotEmptyName (String name ) {
70
66
Employee_ e = new Employee_ ();
71
- return entityql
67
+ return queryDsl
72
68
.from (e )
73
69
.where (
74
70
c -> {
@@ -81,24 +77,24 @@ public List<Employee> selectByNotEmptyName(String name) {
81
77
82
78
public List <Employee > selectByNameWithPrefixMatching (String prefix ) {
83
79
Employee_ e = new Employee_ ();
84
- return entityql .from (e ).where (c -> c .like (e .name , prefix , LikeOption .prefix ())).fetch ();
80
+ return queryDsl .from (e ).where (c -> c .like (e .name , prefix , LikeOption .prefix ())).fetch ();
85
81
}
86
82
87
83
public List <Employee > selectByNameWithSuffixMatching (String suffix ) {
88
84
Employee_ e = new Employee_ ();
89
- return entityql .from (e ).where (c -> c .like (e .name , suffix , LikeOption .suffix ())).fetch ();
85
+ return queryDsl .from (e ).where (c -> c .like (e .name , suffix , LikeOption .suffix ())).fetch ();
90
86
}
91
87
92
88
public List <Employee > selectByNameWithInfixMatching (String inside ) {
93
89
Employee_ e = new Employee_ ();
94
- return entityql .from (e ).where (c -> c .like (e .name , inside , LikeOption .infix ())).fetch ();
90
+ return queryDsl .from (e ).where (c -> c .like (e .name , inside , LikeOption .infix ())).fetch ();
95
91
}
96
92
97
93
public List <Employee > selectByHiredateRange (LocalDateTime from , LocalDateTime to ) {
98
94
LocalDate fromDate = from .toLocalDate ();
99
95
LocalDate toDate = to .toLocalDate ().plusDays (1 );
100
96
Employee_ e = new Employee_ ();
101
- return entityql
97
+ return queryDsl
102
98
.from (e )
103
99
.where (
104
100
c -> {
@@ -110,17 +106,17 @@ public List<Employee> selectByHiredateRange(LocalDateTime from, LocalDateTime to
110
106
111
107
public List <Employee > selectBySalary (Salary salary ) {
112
108
Employee_ e = new Employee_ ();
113
- return entityql .from (e ).where (c -> c .gt (e .salary , salary )).fetch ();
109
+ return queryDsl .from (e ).where (c -> c .gt (e .salary , salary )).fetch ();
114
110
}
115
111
116
112
public Optional <Salary > selectSummedSalary () {
117
113
Employee_ e = new Employee_ ();
118
- return nativeSql .from (e ).select (Expressions .sum (e .salary )).fetchOptional ();
114
+ return queryDsl .from (e ).select (Expressions .sum (e .salary )).fetchOptional ();
119
115
}
120
116
121
117
public List <Employee > selectByExample (Employee employee ) {
122
118
Employee_ e = new Employee_ ();
123
- return entityql .from (e ).where (c -> c .eq (e .name , employee .getName ())).fetch ();
119
+ return queryDsl .from (e ).where (c -> c .eq (e .name , employee .getName ())).fetch ();
124
120
}
125
121
126
122
public List <Employee > selectAll () {
@@ -130,31 +126,31 @@ public List<Employee> selectAll() {
130
126
public Tuple2 <List <Employee >, Long > selectAndCount (Integer offset , Integer limit ) {
131
127
List <Employee > list = select (offset , limit );
132
128
Employee_ e = new Employee_ ();
133
- long count = nativeSql .from (e ).select (Expressions .count ()).fetchOptional ().orElse (0L );
129
+ long count = queryDsl .from (e ).select (Expressions .count ()).fetchOptional ().orElse (0L );
134
130
return new Tuple2 <>(list , count );
135
131
}
136
132
137
133
public List <Employee > select (Integer offset , Integer limit ) {
138
134
Employee_ e = new Employee_ ();
139
- return nativeSql .from (e ).orderBy (c -> c .asc (e .id )).offset (offset ).limit (limit ).fetch ();
135
+ return queryDsl .from (e ).orderBy (c -> c .asc (e .id )).offset (offset ).limit (limit ).fetch ();
140
136
}
141
137
142
138
public <R > R selectByAge (Age age , Function <Stream <Employee >, R > mapper ) {
143
139
Employee_ e = new Employee_ ();
144
140
StreamMappable <Employee > stmt =
145
- nativeSql .from (e ).where (c -> c .gt (e .age , age )).orderBy (c -> c .asc (e .age ));
141
+ queryDsl .from (e ).where (c -> c .gt (e .age , age )).orderBy (c -> c .asc (e .age ));
146
142
return stmt .mapStream (mapper );
147
143
}
148
144
149
145
public long selectCount () {
150
146
Employee_ e = new Employee_ ();
151
- return nativeSql .from (e ).select (Expressions .count ()).fetchOptional ().orElse (0L );
147
+ return queryDsl .from (e ).select (Expressions .count ()).fetchOptional ().orElse (0L );
152
148
}
153
149
154
150
public List <Employee > selectAllWithAssociation () {
155
151
Employee_ e = new Employee_ ();
156
152
Department_ d = new Department_ ();
157
- return entityql
153
+ return queryDsl
158
154
.from (e )
159
155
.leftJoin (d , on -> on .eq (e .departmentId , d .id ))
160
156
.orderBy (c -> c .asc (e .id ))
@@ -171,7 +167,7 @@ public List<Employee> selectAllWithAssociation() {
171
167
public List <Employee > selectByDepartmentName_in (String departmentName ) {
172
168
Employee_ e = new Employee_ ();
173
169
Department_ d = new Department_ ();
174
- return entityql
170
+ return queryDsl
175
171
.from (e )
176
172
.where (
177
173
c ->
@@ -184,7 +180,7 @@ public List<Employee> selectByDepartmentName_in(String departmentName) {
184
180
public List <Employee > selectByDepartmentName_exists (String departmentName ) {
185
181
Employee_ e = new Employee_ ();
186
182
Department_ d = new Department_ ();
187
- return entityql
183
+ return queryDsl
188
184
.from (e )
189
185
.where (
190
186
c ->
@@ -201,7 +197,7 @@ public List<Employee> selectByDepartmentName_exists(String departmentName) {
201
197
public List <Employee > selectByDepartmentName_join (String departmentName ) {
202
198
Employee_ e = new Employee_ ();
203
199
Department_ d = new Department_ ();
204
- return entityql
200
+ return queryDsl
205
201
.from (e )
206
202
.innerJoin (d , on -> on .eq (e .departmentId , d .id ))
207
203
.where (c -> c .eq (d .name , departmentName ))
@@ -210,29 +206,29 @@ public List<Employee> selectByDepartmentName_join(String departmentName) {
210
206
211
207
public List <Employee > selectNameAndSalaryAsEntityList () {
212
208
Employee_ e = new Employee_ ();
213
- return entityql .from (e ).selectTo (e , e .name , e .salary ).fetch ();
209
+ return queryDsl .from (e ).selectTo (e , e .name , e .salary ).fetch ();
214
210
}
215
211
216
212
public List <Tuple2 <String , Salary >> selectNameAndSalaryAsTuple2List () {
217
213
Employee_ e = new Employee_ ();
218
- return nativeSql .from (e ).select (e .name , e .salary ).fetch ();
214
+ return queryDsl .from (e ).select (e .name , e .salary ).fetch ();
219
215
}
220
216
221
217
public List <NameAndSalaryDto > selectNameAndSalaryAsNameAndSalaryDtoList () {
222
218
Employee_ e = new Employee_ ();
223
- return nativeSql .from (e ).select (e .name , e .salary ).stream ()
219
+ return queryDsl .from (e ).select (e .name , e .salary ).stream ()
224
220
.map (tuple -> new NameAndSalaryDto (tuple .getItem1 (), tuple .getItem2 ()))
225
221
.collect (Collectors .toList ());
226
222
}
227
223
228
224
public void insert (Employee employee ) {
229
225
Employee_ e = new Employee_ ();
230
- entityql .insert (e , employee ).execute ();
226
+ queryDsl .insert (e ). single ( employee ).execute ();
231
227
}
232
228
233
- public void insertByNativeSql (Employee employee ) {
229
+ public void insertWithSpecifiedValues (Employee employee ) {
234
230
Employee_ e = new Employee_ ();
235
- nativeSql
231
+ queryDsl
236
232
.insert (e )
237
233
.values (
238
234
c -> {
@@ -252,12 +248,12 @@ public void insertByNativeSql(Employee employee) {
252
248
253
249
public void update (Employee employee ) {
254
250
Employee_ e = new Employee_ ();
255
- entityql .update (e , employee ).execute ();
251
+ queryDsl .update (e ). single ( employee ).execute ();
256
252
}
257
253
258
- public void updateByNativeSql (Employee employee ) {
254
+ public void updateByWhereExpression (Employee employee ) {
259
255
Employee_ e = new Employee_ ();
260
- nativeSql
256
+ queryDsl
261
257
.update (e )
262
258
.set (
263
259
c -> {
@@ -276,26 +272,26 @@ public void updateByNativeSql(Employee employee) {
276
272
277
273
public void delete (Employee employee ) {
278
274
Employee_ e = new Employee_ ();
279
- entityql .delete (e , employee ).execute ();
275
+ queryDsl .delete (e ). single ( employee ).execute ();
280
276
}
281
277
282
- public void deleteByNativeSql (Employee employee ) {
278
+ public void deleteByWhereExpression (Employee employee ) {
283
279
Employee_ e = new Employee_ ();
284
- nativeSql .delete (e ).where (c -> c .eq (e .id , employee .getId ())).execute ();
280
+ queryDsl .delete (e ).where (c -> c .eq (e .id , employee .getId ())).execute ();
285
281
}
286
282
287
283
public void batchInsert (List <Employee > employees ) {
288
284
Employee_ e = new Employee_ ();
289
- entityql .insert (e , employees ).execute ();
285
+ queryDsl .insert (e ). batch ( employees ).execute ();
290
286
}
291
287
292
288
public void batchUpdate (List <Employee > employees ) {
293
289
Employee_ e = new Employee_ ();
294
- entityql .update (e , employees ).execute ();
290
+ queryDsl .update (e ). batch ( employees ).execute ();
295
291
}
296
292
297
293
public void batchDelete (List <Employee > employees ) {
298
294
Employee_ e = new Employee_ ();
299
- entityql .delete (e , employees ).execute ();
295
+ queryDsl .delete (e ). batch ( employees ).execute ();
300
296
}
301
297
}
0 commit comments