From 93ab4e50cc1957ab772b31c9c433f9fc3d29da33 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Fri, 6 Dec 2024 10:50:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor(data):=20=E4=BD=BF=E7=94=A8=20Hutool?= =?UTF-8?q?=20=E5=B7=A5=E5=85=B7=E6=96=B9=E6=B3=95=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E5=8F=8D=E5=B0=84=20API=EF=BC=8C=E4=BB=A5=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/data/mf/util/QueryWrapperHelper.java | 14 ++++++-------- .../starter/data/mp/util/QueryWrapperHelper.java | 14 ++++++-------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/continew-starter-data/continew-starter-data-mf/src/main/java/top/continew/starter/data/mf/util/QueryWrapperHelper.java b/continew-starter-data/continew-starter-data-mf/src/main/java/top/continew/starter/data/mf/util/QueryWrapperHelper.java index 8247171e..a47d314c 100644 --- a/continew-starter-data/continew-starter-data-mf/src/main/java/top/continew/starter/data/mf/util/QueryWrapperHelper.java +++ b/continew-starter-data/continew-starter-data-mf/src/main/java/top/continew/starter/data/mf/util/QueryWrapperHelper.java @@ -16,10 +16,12 @@ package top.continew.starter.data.mf.util; +import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.ReflectUtil; import com.mybatisflex.core.query.QueryWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -133,23 +135,21 @@ public class QueryWrapperHelper { * @return QueryWrapper Consumer */ private static List> buildWrapperConsumer(Q query, Field field) { - boolean accessible = field.canAccess(query); try { - field.setAccessible(true); // 如果字段值为空,直接返回 - Object fieldValue = field.get(query); + Object fieldValue = ReflectUtil.getFieldValue(query, field); if (ObjectUtil.isEmpty(fieldValue)) { return Collections.emptyList(); } // 设置了 @QueryIgnore 注解,直接忽略 - QueryIgnore queryIgnoreAnnotation = field.getAnnotation(QueryIgnore.class); + QueryIgnore queryIgnoreAnnotation = AnnotationUtil.getAnnotation(field, QueryIgnore.class); if (null != queryIgnoreAnnotation) { return Collections.emptyList(); } // 建议:数据库表列建议采用下划线连接法命名,程序变量建议采用驼峰法命名 - String fieldName = field.getName(); + String fieldName = ReflectUtil.getFieldName(field); // 没有 @Query 注解,默认等值查询 - Query queryAnnotation = field.getAnnotation(Query.class); + Query queryAnnotation = AnnotationUtil.getAnnotation(field, Query.class); if (null == queryAnnotation) { return Collections.singletonList(q -> q.eq(CharSequenceUtil.toUnderlineCase(fieldName), fieldValue)); } @@ -173,8 +173,6 @@ public class QueryWrapperHelper { } catch (Exception e) { log.error("Build query wrapper occurred an error: {}. Query: {}, Field: {}.", e .getMessage(), query, field, e); - } finally { - field.setAccessible(accessible); } return Collections.emptyList(); } diff --git a/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/util/QueryWrapperHelper.java b/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/util/QueryWrapperHelper.java index 1059a1bc..d0b2c1fc 100644 --- a/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/util/QueryWrapperHelper.java +++ b/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/util/QueryWrapperHelper.java @@ -16,10 +16,12 @@ package top.continew.starter.data.mp.util; +import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.ReflectUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -124,23 +126,21 @@ public class QueryWrapperHelper { * @return QueryWrapper Consumer */ private static List>> buildWrapperConsumer(Q query, Field field) { - boolean accessible = field.canAccess(query); try { - field.setAccessible(true); // 如果字段值为空,直接返回 - Object fieldValue = field.get(query); + Object fieldValue = ReflectUtil.getFieldValue(query, field); if (ObjectUtil.isEmpty(fieldValue)) { return Collections.emptyList(); } // 设置了 @QueryIgnore 注解,直接忽略 - QueryIgnore queryIgnoreAnnotation = field.getAnnotation(QueryIgnore.class); + QueryIgnore queryIgnoreAnnotation = AnnotationUtil.getAnnotation(field, QueryIgnore.class); if (null != queryIgnoreAnnotation) { return Collections.emptyList(); } // 建议:数据库表列建议采用下划线连接法命名,程序变量建议采用驼峰法命名 - String fieldName = field.getName(); + String fieldName = ReflectUtil.getFieldName(field); // 没有 @Query 注解,默认等值查询 - Query queryAnnotation = field.getAnnotation(Query.class); + Query queryAnnotation = AnnotationUtil.getAnnotation(field, Query.class); if (null == queryAnnotation) { return Collections.singletonList(q -> q.eq(CharSequenceUtil.toUnderlineCase(fieldName), fieldValue)); } @@ -164,8 +164,6 @@ public class QueryWrapperHelper { } catch (Exception e) { log.error("Build query wrapper occurred an error: {}. Query: {}, Field: {}.", e .getMessage(), query, field, e); - } finally { - field.setAccessible(accessible); } return Collections.emptyList(); }