Skip to content

Commit 9d6a184

Browse files
yindzabel533
authored andcommitted
feat: 为ExampleService添加findFirst方法,用于查询条件匹配到多个结果导致findOne不适用的场景
1 parent eaa78e8 commit 9d6a184

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

service/src/main/java/io/mybatis/service/AbstractService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,18 @@ public T findOne(Example<T> example) {
259259
return baseMapper.selectOneByExample(example).orElse(null);
260260
}
261261

262+
/**
263+
* 根据 example 条件查询并返回第1个结果(当结果多于1个时不抛出异常)
264+
*
265+
* @param example 查询条件
266+
* @return 实体
267+
*/
268+
@Override
269+
public T findFirst(Example<T> example) {
270+
List<T> list = baseMapper.selectByExample(example);
271+
return (list == null || list.isEmpty()) ? null : list.get(0);
272+
}
273+
262274
/**
263275
* 根据 example 条件查询
264276
*

service/src/main/java/io/mybatis/service/ExampleService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ default Example<T> example() {
7373
*/
7474
T findOne(Example<T> example);
7575

76+
/**
77+
* 根据 example 条件查询并返回第1个结果(当结果多于1个时不抛出异常)
78+
*
79+
* @param example 查询条件
80+
* @return 实体
81+
*/
82+
T findFirst(Example<T> example);
83+
7684
/**
7785
* 根据 example 条件查询
7886
*

0 commit comments

Comments
 (0)