From 5929f7d6acee8841f63895aa4fff5efd72b1f1f7 Mon Sep 17 00:00:00 2001 From: jasmine Date: Tue, 23 Jan 2024 10:16:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E4=BC=A0=E5=85=A5=E7=9A=84=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E5=AD=97=E6=AE=B5=E5=81=9A=E6=A0=A1=E9=AA=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../extension/crud/base/BaseServiceImpl.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/base/BaseServiceImpl.java b/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/base/BaseServiceImpl.java index 4b9f1331..a29416dd 100644 --- a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/base/BaseServiceImpl.java +++ b/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/base/BaseServiceImpl.java @@ -20,6 +20,7 @@ import cn.crane4j.core.support.OperateTemplate; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.copier.CopyOptions; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.lang.Opt; import cn.hutool.core.lang.tree.Tree; import cn.hutool.core.lang.tree.TreeNodeConfig; @@ -33,9 +34,11 @@ import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.transaction.annotation.Transactional; +import top.charles7c.continew.starter.core.constant.StringConstants; import top.charles7c.continew.starter.core.util.ClassUtils; import top.charles7c.continew.starter.core.util.ReflectUtils; import top.charles7c.continew.starter.core.util.validate.CheckUtils; +import top.charles7c.continew.starter.core.util.validate.ValidationUtils; import top.charles7c.continew.starter.data.mybatis.plus.base.BaseMapper; import top.charles7c.continew.starter.data.mybatis.plus.query.QueryHelper; import top.charles7c.continew.starter.extension.crud.annotation.TreeField; @@ -48,7 +51,9 @@ import top.charles7c.continew.starter.file.excel.util.ExcelUtils; import java.io.Serializable; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Optional; /** * 业务实现基类 @@ -71,6 +76,7 @@ public abstract class BaseServiceImpl, T extends BaseDO, protected final Class entityClass = this.currentEntityClass(); protected final Class listClass = this.currentListClass(); protected final Class detailClass = this.currentDetailClass(); + private final Field[] entityFields = this.entityClass.getDeclaredFields(); @Override public PageResp page(Q query, PageQuery pageQuery) { @@ -208,7 +214,19 @@ public abstract class BaseServiceImpl, T extends BaseDO, Sort sort = Opt.ofNullable(sortQuery).orElseGet(SortQuery::new).getSort(); for (Sort.Order order : sort) { if (null != order) { - queryWrapper.orderBy(true, order.isAscending(), StrUtil.toUnderlineCase(order.getProperty())); + String property = order.getProperty(); + String checkProperty; + // 携带表别名,获取.后面的字段名 + if (property.contains(StringConstants.DOT)) { + checkProperty = CollectionUtil.getLast(StrUtil.split(property, StringConstants.DOT)); + } else { + checkProperty = property; + } + Optional optional = Arrays.stream(entityFields) + .filter(field -> checkProperty.equals(field.getName())) + .findFirst(); + ValidationUtils.throwIf(optional.isEmpty(), "无效的排序字段 [{}]。", property); + queryWrapper.orderBy(true, order.isAscending(), StrUtil.toUnderlineCase(property)); } } } -- Gitee