27
27
import java .util .ArrayList ;
28
28
import java .util .Date ;
29
29
import java .util .List ;
30
+ import java .util .Optional ;
30
31
31
32
import org .apache .ibatis .datasource .unpooled .UnpooledDataSource ;
32
33
import org .apache .ibatis .jdbc .ScriptRunner ;
@@ -74,8 +75,8 @@ public void testSelect() {
74
75
try (SqlSession session = sqlSessionFactory .openSession ()) {
75
76
SimpleTableAnnotatedMapperNewStyle mapper = session .getMapper (SimpleTableAnnotatedMapperNewStyle .class );
76
77
77
- List <SimpleTableRecord > rows = mapper .select (q ->
78
- q .where (id , isEqualTo (1 ))
78
+ List <SimpleTableRecord > rows = mapper .select (h ->
79
+ h .where (id , isEqualTo (1 ))
79
80
.or (occupation , isNull ()));
80
81
81
82
assertThat (rows .size ()).isEqualTo (3 );
@@ -114,8 +115,8 @@ public void testSelectDistinct() {
114
115
try (SqlSession session = sqlSessionFactory .openSession ()) {
115
116
SimpleTableAnnotatedMapperNewStyle mapper = session .getMapper (SimpleTableAnnotatedMapperNewStyle .class );
116
117
117
- List <SimpleTableRecord > rows = mapper .selectDistinct (q ->
118
- q .where (id , isGreaterThan (1 ))
118
+ List <SimpleTableRecord > rows = mapper .selectDistinct (h ->
119
+ h .where (id , isGreaterThan (1 ))
119
120
.or (occupation , isNull ()));
120
121
121
122
assertThat (rows .size ()).isEqualTo (5 );
@@ -127,8 +128,8 @@ public void testSelectWithTypeHandler() {
127
128
try (SqlSession session = sqlSessionFactory .openSession ()) {
128
129
SimpleTableAnnotatedMapperNewStyle mapper = session .getMapper (SimpleTableAnnotatedMapperNewStyle .class );
129
130
130
- List <SimpleTableRecord > rows = mapper .select (q ->
131
- q .where (employed , isEqualTo (false ))
131
+ List <SimpleTableRecord > rows = mapper .select (h ->
132
+ h .where (employed , isEqualTo (false ))
132
133
.orderBy (id ));
133
134
134
135
assertAll (
@@ -139,13 +140,23 @@ public void testSelectWithTypeHandler() {
139
140
}
140
141
}
141
142
143
+ @ Test
144
+ public void testSelectByPrimaryKeyWithMissingRecord () {
145
+ try (SqlSession session = sqlSessionFactory .openSession ()) {
146
+ SimpleTableAnnotatedMapperNewStyle mapper = session .getMapper (SimpleTableAnnotatedMapperNewStyle .class );
147
+
148
+ Optional <SimpleTableRecord > record = mapper .selectByPrimaryKey (300 );
149
+ assertThat (record .isPresent ()).isFalse ();
150
+ }
151
+ }
152
+
142
153
@ Test
143
154
public void testFirstNameIn () {
144
155
try (SqlSession session = sqlSessionFactory .openSession ()) {
145
156
SimpleTableAnnotatedMapperNewStyle mapper = session .getMapper (SimpleTableAnnotatedMapperNewStyle .class );
146
157
147
- List <SimpleTableRecord > rows = mapper .select (q ->
148
- q .where (firstName , isIn ("Fred" , "Barney" )));
158
+ List <SimpleTableRecord > rows = mapper .select (h ->
159
+ h .where (firstName , isIn ("Fred" , "Barney" )));
149
160
150
161
assertAll (
151
162
() -> assertThat (rows .size ()).isEqualTo (2 ),
@@ -159,8 +170,8 @@ public void testFirstNameIn() {
159
170
public void testDelete () {
160
171
try (SqlSession session = sqlSessionFactory .openSession ()) {
161
172
SimpleTableAnnotatedMapperNewStyle mapper = session .getMapper (SimpleTableAnnotatedMapperNewStyle .class );
162
- int rows = mapper .delete (q ->
163
- q .where (occupation , isNull ()));
173
+ int rows = mapper .delete (h ->
174
+ h .where (occupation , isNull ()));
164
175
assertThat (rows ).isEqualTo (2 );
165
176
}
166
177
}
@@ -267,8 +278,9 @@ public void testUpdateByPrimaryKey() {
267
278
rows = mapper .updateByPrimaryKey (record );
268
279
assertThat (rows ).isEqualTo (1 );
269
280
270
- SimpleTableRecord newRecord = mapper .selectByPrimaryKey (100 );
271
- assertThat (newRecord .getOccupation ()).isEqualTo ("Programmer" );
281
+ Optional <SimpleTableRecord > newRecord = mapper .selectByPrimaryKey (100 );
282
+ assertThat (newRecord .isPresent ()).isTrue ();
283
+ assertThat (newRecord .get ().getOccupation ()).isEqualTo ("Programmer" );
272
284
}
273
285
}
274
286
@@ -293,9 +305,10 @@ public void testUpdateByPrimaryKeySelective() {
293
305
rows = mapper .updateByPrimaryKeySelective (updateRecord );
294
306
assertThat (rows ).isEqualTo (1 );
295
307
296
- SimpleTableRecord newRecord = mapper .selectByPrimaryKey (100 );
297
- assertThat (newRecord .getOccupation ()).isEqualTo ("Programmer" );
298
- assertThat (newRecord .getFirstName ()).isEqualTo ("Joe" );
308
+ Optional <SimpleTableRecord > newRecord = mapper .selectByPrimaryKey (100 );
309
+ assertThat (newRecord .isPresent ()).isTrue ();
310
+ assertThat (newRecord .get ().getOccupation ()).isEqualTo ("Programmer" );
311
+ assertThat (newRecord .get ().getFirstName ()).isEqualTo ("Joe" );
299
312
}
300
313
}
301
314
@@ -316,15 +329,16 @@ public void testUpdate() {
316
329
317
330
record .setOccupation ("Programmer" );
318
331
319
- rows = mapper .update (dsl ->
320
- SimpleTableAnnotatedMapperNewStyle .setAll (record , dsl )
332
+ rows = mapper .update (h ->
333
+ SimpleTableAnnotatedMapperNewStyle .setAll (record , h )
321
334
.where (id , isEqualTo (100 ))
322
335
.and (firstName , isEqualTo ("Joe" )));
323
336
324
337
assertThat (rows ).isEqualTo (1 );
325
338
326
- SimpleTableRecord newRecord = mapper .selectByPrimaryKey (100 );
327
- assertThat (newRecord .getOccupation ()).isEqualTo ("Programmer" );
339
+ Optional <SimpleTableRecord > newRecord = mapper .selectByPrimaryKey (100 );
340
+ assertThat (newRecord .isPresent ()).isTrue ();
341
+ assertThat (newRecord .get ().getOccupation ()).isEqualTo ("Programmer" );
328
342
}
329
343
}
330
344
@@ -345,22 +359,23 @@ public void testUpdateAll() {
345
359
346
360
SimpleTableRecord updateRecord = new SimpleTableRecord ();
347
361
updateRecord .setOccupation ("Programmer" );
348
- rows = mapper .update (dsl ->
349
- SimpleTableAnnotatedMapperNewStyle .setSelective (updateRecord , dsl ));
362
+ rows = mapper .update (h ->
363
+ SimpleTableAnnotatedMapperNewStyle .setSelective (updateRecord , h ));
350
364
351
365
assertThat (rows ).isEqualTo (7 );
352
366
353
- SimpleTableRecord newRecord = mapper .selectByPrimaryKey (100 );
354
- assertThat (newRecord .getOccupation ()).isEqualTo ("Programmer" );
367
+ Optional <SimpleTableRecord > newRecord = mapper .selectByPrimaryKey (100 );
368
+ assertThat (newRecord .isPresent ()).isTrue ();
369
+ assertThat (newRecord .get ().getOccupation ()).isEqualTo ("Programmer" );
355
370
}
356
371
}
357
372
358
373
@ Test
359
374
public void testCount () {
360
375
try (SqlSession session = sqlSessionFactory .openSession ()) {
361
376
SimpleTableAnnotatedMapperNewStyle mapper = session .getMapper (SimpleTableAnnotatedMapperNewStyle .class );
362
- long rows = mapper .count (q ->
363
- q .where (occupation , isNull ()));
377
+ long rows = mapper .count (h ->
378
+ h .where (occupation , isNull ()));
364
379
365
380
assertThat (rows ).isEqualTo (2L );
366
381
}
@@ -381,8 +396,8 @@ public void testTypeHandledLike() {
381
396
try (SqlSession session = sqlSessionFactory .openSession ()) {
382
397
SimpleTableAnnotatedMapperNewStyle mapper = session .getMapper (SimpleTableAnnotatedMapperNewStyle .class );
383
398
384
- List <SimpleTableRecord > rows = mapper .select (q ->
385
- q .where (lastName , isLike (LastName .of ("Fl%" )))
399
+ List <SimpleTableRecord > rows = mapper .select (h ->
400
+ h .where (lastName , isLike (LastName .of ("Fl%" )))
386
401
.orderBy (id ));
387
402
388
403
assertThat (rows .size ()).isEqualTo (3 );
@@ -395,8 +410,8 @@ public void testTypeHandledNotLike() {
395
410
try (SqlSession session = sqlSessionFactory .openSession ()) {
396
411
SimpleTableAnnotatedMapperNewStyle mapper = session .getMapper (SimpleTableAnnotatedMapperNewStyle .class );
397
412
398
- List <SimpleTableRecord > rows = mapper .select (q ->
399
- q .where (lastName , isNotLike (LastName .of ("Fl%" )))
413
+ List <SimpleTableRecord > rows = mapper .select (h ->
414
+ h .where (lastName , isNotLike (LastName .of ("Fl%" )))
400
415
.orderBy (id ));
401
416
402
417
assertThat (rows .size ()).isEqualTo (3 );
0 commit comments