分页方法改造

This commit is contained in:
Wang.Huan
2025-02-25 13:27:50 +08:00
parent f53635b340
commit 345e71f7cf
2 changed files with 80 additions and 8 deletions

View File

@@ -0,0 +1,74 @@
package com.openhis.common.utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.stream.Collectors;
public class HisPageUtils {
/**
* 执行分页查询并转换为目标类型
*
* @param mapper MyBatis Plus Mapper 接口
* @param queryWrapper 查询条件
* @param pageNo 当前页
* @param pageSize 每页大小
* @param targetClass 目标类(如 MedicationDto.class
* @param <T> 源对象类型(数据库实体类)
* @param <R> 目标对象类型DTO 类)
* @return 分页结果(目标类型)
*/
public static <T, R> Page<R> selectPage(BaseMapper<T> mapper, QueryWrapper<T> queryWrapper,
int pageNo, int pageSize, Class<R> targetClass) {
// 构建分页对象
Page<T> page = new Page<>(pageNo, pageSize);
// 执行分页查询
Page<T> sourcePage = mapper.selectPage(page, queryWrapper);
// 转换为目标类型的分页对象
Page<R> targetPage = new Page<>();
// 复制分页信息
BeanUtils.copyProperties(sourcePage, targetPage);
// 转换记录列表
targetPage.setRecords(convertToDtoList(sourcePage.getRecords(), targetClass));
return targetPage;
}
/**
* 将源对象列表转换为目标对象列表
*
* @param sourceList 源对象列表
* @param targetClass 目标类
* @param <T> 源对象类型
* @param <R> 目标对象类型
* @return 目标对象列表
*/
private static <T, R> List<R> convertToDtoList(List<T> sourceList, Class<R> targetClass) {
return sourceList.stream()
.map(source -> convertToDto(source, targetClass))
.collect(Collectors.toList());
}
/**
* 将源对象转换为目标对象
*
* @param source 源对象
* @param targetClass 目标类
* @param <T> 源对象类型
* @param <R> 目标对象类型
* @return 目标对象
*/
private static <T, R> R convertToDto(T source, Class<R> targetClass) {
try {
// 创建目标对象实例
R target = targetClass.getDeclaredConstructor().newInstance();
BeanUtils.copyProperties(source, target); // 复制属性
return target;
} catch (Exception e) {
throw new RuntimeException("Failed to convert object to DTO", e);
}
}
}

View File

@@ -24,14 +24,14 @@ public class HisQueryUtils {
/** /**
* 条件查询构造器 * 条件查询构造器
* *
* @param entity 传参实体 * @param entity 传参实体
* @param searchKey 模糊查询关键字 * @param searchKey 模糊查询关键字
* @param searchFields 支持模糊查询的字段集合 ; 不需要模糊查询传 null 即可 * @param searchFields 支持模糊查询的字段集合 ; 不需要模糊查询传 null 即可
* @param request 请求 * @param request 请求
* @return 构造条件 * @return 构造条件
*/ */
public static <T> QueryWrapper<T> buildQueryWrapper(T entity, String searchKey, HashSet<String> searchFields, public static <T> QueryWrapper<T> buildQueryWrapper(Object entity, String searchKey, HashSet<String> searchFields,
HttpServletRequest request) { HttpServletRequest request) {
QueryWrapper<T> queryWrapper = new QueryWrapper<>(); QueryWrapper<T> queryWrapper = new QueryWrapper<>();
// 添加租户id查询条件 // 添加租户id查询条件
queryWrapper.eq(CommonConstants.Common.TENANT_ID, getCurrentTenantId()); queryWrapper.eq(CommonConstants.Common.TENANT_ID, getCurrentTenantId());
@@ -45,7 +45,6 @@ public class HisQueryUtils {
try { try {
Object value = field.get(entity); Object value = field.get(entity);
if (value != null && !value.toString().equals("")) { if (value != null && !value.toString().equals("")) {
// String fieldName = field.getName();
// 将驼峰命名的字段名转换为下划线命名的数据库字段名 // 将驼峰命名的字段名转换为下划线命名的数据库字段名
String fieldName = camelToUnderline(field.getName()); String fieldName = camelToUnderline(field.getName());
// 处理等于条件 // 处理等于条件
@@ -65,13 +64,12 @@ public class HisQueryUtils {
} }
// 处理时间段查询 // 处理时间段查询
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD); DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD);
// DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Map<String, String[]> parameterMap = request.getParameterMap(); Map<String, String[]> parameterMap = request.getParameterMap();
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) { for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
String paramName = entry.getKey(); String paramName = entry.getKey();
// 检查参数名是否以 "STime" 或 "ETime" 结尾 // 检查参数名是否以 "STime" 或 "ETime" 结尾
if (paramName.endsWith(CommonConstants.Common.S_TIME) if (paramName.endsWith(CommonConstants.Common.S_TIME)
|| paramName.endsWith(CommonConstants.Common.E_TIME)) { || paramName.endsWith(CommonConstants.Common.E_TIME)) {
// 提取字段名(去掉 "STime" 或 "ETime" 后缀) // 提取字段名(去掉 "STime" 或 "ETime" 后缀)
String fieldName = paramName.substring(0, paramName.length() - 5); String fieldName = paramName.substring(0, paramName.length() - 5);
// 驼峰转下划线 // 驼峰转下划线