Skip to content

Commit 24b0eb7

Browse files
committed
SelectOne should return Optional
1 parent bfd9bf6 commit 24b0eb7

File tree

2 files changed

+47
-36
lines changed

2 files changed

+47
-36
lines changed

src/test/java/examples/simple/newstyle/SimpleTableAnnotatedMapperNewStyle.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.mybatis.dynamic.sql.SqlBuilder.*;
2020

2121
import java.util.List;
22+
import java.util.Optional;
2223

2324
import org.apache.ibatis.annotations.DeleteProvider;
2425
import org.apache.ibatis.annotations.InsertProvider;
@@ -28,7 +29,6 @@
2829
import org.apache.ibatis.annotations.Results;
2930
import org.apache.ibatis.annotations.SelectProvider;
3031
import org.apache.ibatis.annotations.UpdateProvider;
31-
import org.apache.ibatis.session.RowBounds;
3232
import org.apache.ibatis.type.JdbcType;
3333
import org.mybatis.dynamic.sql.SqlBuilder;
3434
import org.mybatis.dynamic.sql.delete.DeleteDSL;
@@ -82,11 +82,7 @@ public interface SimpleTableAnnotatedMapperNewStyle {
8282

8383
@SelectProvider(type=SqlProviderAdapter.class, method="select")
8484
@ResultMap("SimpleTableResult")
85-
List<SimpleTableRecord> selectManyWithRowbounds(SelectStatementProvider selectStatement, RowBounds rowBounds);
86-
87-
@SelectProvider(type=SqlProviderAdapter.class, method="select")
88-
@ResultMap("SimpleTableResult")
89-
SimpleTableRecord selectOne(SelectStatementProvider selectStatement);
85+
Optional<SimpleTableRecord> selectOne(SelectStatementProvider selectStatement);
9086

9187
@DeleteProvider(type=SqlProviderAdapter.class, method="delete")
9288
int delete(DeleteStatementProvider deleteStatement);
@@ -166,7 +162,7 @@ default List<SimpleTableRecord> selectDistinct(MyBatis3SelectHelper<SimpleTableR
166162
.execute();
167163
}
168164

169-
default SimpleTableRecord selectByPrimaryKey(Integer id_) {
165+
default Optional<SimpleTableRecord> selectByPrimaryKey(Integer id_) {
170166
return SelectDSL.selectWithMapper(this::selectOne, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
171167
.from(simpleTable)
172168
.where(id, isEqualTo(id_))

src/test/java/examples/simple/newstyle/SimpleTableAnnotatedNewStyleMapperTest.java

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.ArrayList;
2828
import java.util.Date;
2929
import java.util.List;
30+
import java.util.Optional;
3031

3132
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
3233
import org.apache.ibatis.jdbc.ScriptRunner;
@@ -74,8 +75,8 @@ public void testSelect() {
7475
try (SqlSession session = sqlSessionFactory.openSession()) {
7576
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
7677

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))
7980
.or(occupation, isNull()));
8081

8182
assertThat(rows.size()).isEqualTo(3);
@@ -114,8 +115,8 @@ public void testSelectDistinct() {
114115
try (SqlSession session = sqlSessionFactory.openSession()) {
115116
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
116117

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))
119120
.or(occupation, isNull()));
120121

121122
assertThat(rows.size()).isEqualTo(5);
@@ -127,8 +128,8 @@ public void testSelectWithTypeHandler() {
127128
try (SqlSession session = sqlSessionFactory.openSession()) {
128129
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
129130

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))
132133
.orderBy(id));
133134

134135
assertAll(
@@ -139,13 +140,23 @@ public void testSelectWithTypeHandler() {
139140
}
140141
}
141142

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+
142153
@Test
143154
public void testFirstNameIn() {
144155
try (SqlSession session = sqlSessionFactory.openSession()) {
145156
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
146157

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")));
149160

150161
assertAll(
151162
() -> assertThat(rows.size()).isEqualTo(2),
@@ -159,8 +170,8 @@ public void testFirstNameIn() {
159170
public void testDelete() {
160171
try (SqlSession session = sqlSessionFactory.openSession()) {
161172
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()));
164175
assertThat(rows).isEqualTo(2);
165176
}
166177
}
@@ -267,8 +278,9 @@ public void testUpdateByPrimaryKey() {
267278
rows = mapper.updateByPrimaryKey(record);
268279
assertThat(rows).isEqualTo(1);
269280

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");
272284
}
273285
}
274286

@@ -293,9 +305,10 @@ public void testUpdateByPrimaryKeySelective() {
293305
rows = mapper.updateByPrimaryKeySelective(updateRecord);
294306
assertThat(rows).isEqualTo(1);
295307

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");
299312
}
300313
}
301314

@@ -316,15 +329,16 @@ public void testUpdate() {
316329

317330
record.setOccupation("Programmer");
318331

319-
rows = mapper.update(dsl ->
320-
SimpleTableAnnotatedMapperNewStyle.setAll(record, dsl)
332+
rows = mapper.update(h ->
333+
SimpleTableAnnotatedMapperNewStyle.setAll(record, h)
321334
.where(id, isEqualTo(100))
322335
.and(firstName, isEqualTo("Joe")));
323336

324337
assertThat(rows).isEqualTo(1);
325338

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");
328342
}
329343
}
330344

@@ -345,22 +359,23 @@ public void testUpdateAll() {
345359

346360
SimpleTableRecord updateRecord = new SimpleTableRecord();
347361
updateRecord.setOccupation("Programmer");
348-
rows = mapper.update(dsl ->
349-
SimpleTableAnnotatedMapperNewStyle.setSelective(updateRecord, dsl));
362+
rows = mapper.update(h ->
363+
SimpleTableAnnotatedMapperNewStyle.setSelective(updateRecord, h));
350364

351365
assertThat(rows).isEqualTo(7);
352366

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");
355370
}
356371
}
357372

358373
@Test
359374
public void testCount() {
360375
try (SqlSession session = sqlSessionFactory.openSession()) {
361376
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()));
364379

365380
assertThat(rows).isEqualTo(2L);
366381
}
@@ -381,8 +396,8 @@ public void testTypeHandledLike() {
381396
try (SqlSession session = sqlSessionFactory.openSession()) {
382397
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
383398

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%")))
386401
.orderBy(id));
387402

388403
assertThat(rows.size()).isEqualTo(3);
@@ -395,8 +410,8 @@ public void testTypeHandledNotLike() {
395410
try (SqlSession session = sqlSessionFactory.openSession()) {
396411
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
397412

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%")))
400415
.orderBy(id));
401416

402417
assertThat(rows.size()).isEqualTo(3);

0 commit comments

Comments
 (0)