refactor(webmvc): Optimize pagination query logic

- Introduce resolvePageable method to parse page requests and return Pageable objects
- Subclasses can now override resolvePageable method to customize paging logic
- Modify the findAll and findAllMapped methods and use the new resolvePageable method for pagination query
This commit is contained in:
gewuyou 2025-05-31 22:43:32 +08:00
parent 5c74c42a24
commit 6e6099b02b

View File

@ -7,6 +7,7 @@ import com.gewuyou.forgeboot.webmvc.dto.extension.toPageable
import com.gewuyou.forgeboot.webmvc.dto.request.PageQueryReq
import com.gewuyou.forgeboot.webmvc.spec.repository.CrudRepositorySpec
import com.gewuyou.forgeboot.webmvc.spec.service.CrudServiceSpec
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.domain.Specification
@ -40,11 +41,23 @@ abstract class CrudServiceImplSpec<Entity : Any, Id : Any, Filter : Any>(
return repository
.findAll(
buildSpecification(query),
query.toPageable()
resolvePageable(query)
)
.toPageResult()
}
/**
* 将分页请求解析为 Pageable 对象
* 子类可重写此方法来自定义分页逻辑例如默认排序
*
* @param query 分页查询请求
* @return 分页参数
*/
protected open fun resolvePageable(query: PageQueryReq<Filter>): Pageable {
return query.toPageable()
}
/**
* 构建 JPA Specification 查询条件
*
@ -154,7 +167,7 @@ abstract class CrudServiceImplSpec<Entity : Any, Id : Any, Filter : Any>(
): PageResult<V> {
val page = repository.findAll(
buildSpecification(query),
query.toPageable()
resolvePageable(query)
)
return page.toPageResult().map(mapping)
}