Skip to content

Commit 6315e94

Browse files
committed
Properly handle null row in sommon select mapper
1 parent ee2ccbf commit 6315e94

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

Diff for: src/main/java/org/mybatis/dynamic/sql/util/mybatis3/CommonSelectMapper.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.function.Function;
2323

2424
import org.apache.ibatis.annotations.SelectProvider;
25+
import org.jspecify.annotations.Nullable;
2526
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
2627
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
2728

@@ -58,7 +59,7 @@ public interface CommonSelectMapper {
5859
* @return A Map containing the row values.
5960
*/
6061
@SelectProvider(type = SqlProviderAdapter.class, method = "select")
61-
Map<String, Object> selectOneMappedRow(SelectStatementProvider selectStatement);
62+
@Nullable Map<String, Object> selectOneMappedRow(SelectStatementProvider selectStatement);
6263

6364
/**
6465
* Select a single row of values and then convert the values to a custom type. This is similar
@@ -74,9 +75,10 @@ public interface CommonSelectMapper {
7475
* @param <R> the datatype of the converted object
7576
* @return the converted object
7677
*/
77-
default <R> R selectOne(SelectStatementProvider selectStatement,
78+
default <R> @Nullable R selectOne(SelectStatementProvider selectStatement,
7879
Function<Map<String, Object>, R> rowMapper) {
79-
return rowMapper.apply(selectOneMappedRow(selectStatement));
80+
var result = selectOneMappedRow(selectStatement);
81+
return result == null ? null : rowMapper.apply(result);
8082
}
8183

8284
/**
@@ -122,7 +124,7 @@ default <R> List<R> selectMany(SelectStatementProvider selectStatement,
122124
* column is null
123125
*/
124126
@SelectProvider(type = SqlProviderAdapter.class, method = "select")
125-
BigDecimal selectOneBigDecimal(SelectStatementProvider selectStatement);
127+
@Nullable BigDecimal selectOneBigDecimal(SelectStatementProvider selectStatement);
126128

127129
/**
128130
* Retrieve a single {@link java.math.BigDecimal} from a result set. The result set must have
@@ -157,7 +159,7 @@ default <R> List<R> selectMany(SelectStatementProvider selectStatement,
157159
* column is null
158160
*/
159161
@SelectProvider(type = SqlProviderAdapter.class, method = "select")
160-
Double selectOneDouble(SelectStatementProvider selectStatement);
162+
@Nullable Double selectOneDouble(SelectStatementProvider selectStatement);
161163

162164
/**
163165
* Retrieve a single {@link java.lang.Double} from a result set. The result set must have
@@ -192,7 +194,7 @@ default <R> List<R> selectMany(SelectStatementProvider selectStatement,
192194
* column is null
193195
*/
194196
@SelectProvider(type = SqlProviderAdapter.class, method = "select")
195-
Integer selectOneInteger(SelectStatementProvider selectStatement);
197+
@Nullable Integer selectOneInteger(SelectStatementProvider selectStatement);
196198

197199
/**
198200
* Retrieve a single {@link java.lang.Integer} from a result set. The result set must have
@@ -227,7 +229,7 @@ default <R> List<R> selectMany(SelectStatementProvider selectStatement,
227229
* column is null
228230
*/
229231
@SelectProvider(type = SqlProviderAdapter.class, method = "select")
230-
Long selectOneLong(SelectStatementProvider selectStatement);
232+
@Nullable Long selectOneLong(SelectStatementProvider selectStatement);
231233

232234
/**
233235
* Retrieve a single {@link java.lang.Long} from a result set. The result set must have
@@ -262,7 +264,7 @@ default <R> List<R> selectMany(SelectStatementProvider selectStatement,
262264
* column is null
263265
*/
264266
@SelectProvider(type = SqlProviderAdapter.class, method = "select")
265-
String selectOneString(SelectStatementProvider selectStatement);
267+
@Nullable String selectOneString(SelectStatementProvider selectStatement);
266268

267269
/**
268270
* Retrieve a single {@link java.lang.String} from a result set. The result set must have

Diff for: src/test/java/examples/animal/data/CommonSelectMapperTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,29 @@ void testGeneralSelectOneWithRowMapper() {
106106

107107
AnimalData animal = mapper.selectOne(selectStatement, rowMapper);
108108

109+
assertThat(animal).isNotNull();
109110
assertThat(animal.getId()).isEqualTo(1);
110111
assertThat(animal.getAnimalName()).isEqualTo("Lesser short-tailed shrew");
111112
assertThat(animal.getBodyWeight()).isEqualTo(0.14);
112113
assertThat(animal.getBrainWeight()).isEqualTo(0.005);
113114
}
114115
}
115116

117+
@Test
118+
void testGeneralSelectOneWithRowMapperAndNullRow() {
119+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
120+
CommonSelectMapper mapper = sqlSession.getMapper(CommonSelectMapper.class);
121+
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
122+
.from(animalData)
123+
.where(id, isEqualTo(-237))
124+
.build()
125+
.render(RenderingStrategies.MYBATIS3);
126+
127+
AnimalData animal = mapper.selectOne(selectStatement, rowMapper);
128+
assertThat(animal).isNull();
129+
}
130+
}
131+
116132
@Test
117133
void testGeneralSelectMany() {
118134
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

0 commit comments

Comments
 (0)