Bläddra i källkod

优化数据业务增强操作逻辑

woody 1 år sedan
förälder
incheckning
d3ea04db64

+ 9 - 6
framework-common/src/main/java/com/chelvc/framework/common/model/Pagination.java

@@ -3,6 +3,7 @@ package com.chelvc.framework.common.model;
 import java.io.Serializable;
 import java.io.Serializable;
 import java.util.Collections;
 import java.util.Collections;
 import java.util.List;
 import java.util.List;
+import java.util.function.Function;
 
 
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.ObjectUtils;
 import lombok.AllArgsConstructor;
 import lombok.AllArgsConstructor;
@@ -10,6 +11,7 @@ import lombok.Builder;
 import lombok.EqualsAndHashCode;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.NoArgsConstructor;
+import lombok.NonNull;
 import lombok.ToString;
 import lombok.ToString;
 import lombok.experimental.SuperBuilder;
 import lombok.experimental.SuperBuilder;
 
 
@@ -63,22 +65,23 @@ public class Pagination<T> implements Serializable {
     /**
     /**
      * 数据转换
      * 数据转换
      *
      *
-     * @param type 目标数据对象类型
+     * @param type 目标对象类型
      * @param <M>  目标数据类型
      * @param <M>  目标数据类型
      * @return 数据列表分页实例
      * @return 数据列表分页实例
      */
      */
-    public <M> Pagination<M> convert(Class<M> type) {
-        return this.convert(ObjectUtils.copying(this.records, type));
+    public <M> Pagination<M> convert(@NonNull Class<M> type) {
+        return Pagination.<M>builder().total(this.total).pages(this.pages)
+                .records(ObjectUtils.copying(this.records, type)).build();
     }
     }
 
 
     /**
     /**
      * 数据转换
      * 数据转换
      *
      *
-     * @param records 目标数据列表
+     * @param adapter 对象转换适配器
      * @param <M>     目标数据类型
      * @param <M>     目标数据类型
      * @return 数据列表分页实例
      * @return 数据列表分页实例
      */
      */
-    public <M> Pagination<M> convert(List<M> records) {
-        return Pagination.<M>builder().total(this.total).pages(this.pages).records(records).build();
+    public <M> Pagination<M> convert(@NonNull Function<List<T>, List<M>> adapter) {
+        return Pagination.<M>builder().total(this.total).pages(this.pages).records(adapter.apply(this.records)).build();
     }
     }
 }
 }

+ 2 - 2
framework-database/src/main/java/com/chelvc/framework/database/config/DatabaseConfigurer.java

@@ -17,7 +17,7 @@ import com.chelvc.framework.common.util.IdentityUtils;
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.database.context.Transactor;
 import com.chelvc.framework.database.context.Transactor;
 import com.chelvc.framework.database.interceptor.DeletedExcludeHandler;
 import com.chelvc.framework.database.interceptor.DeletedExcludeHandler;
-import com.chelvc.framework.database.support.CustomerSqlInjector;
+import com.chelvc.framework.database.support.EnhanceSqlInjector;
 import com.chelvc.framework.redis.context.RedisContextHolder;
 import com.chelvc.framework.redis.context.RedisContextHolder;
 import lombok.NonNull;
 import lombok.NonNull;
 import lombok.RequiredArgsConstructor;
 import lombok.RequiredArgsConstructor;
@@ -73,7 +73,7 @@ public class DatabaseConfigurer {
 
 
     @Bean
     @Bean
     public ISqlInjector sqlInjector() {
     public ISqlInjector sqlInjector() {
-        return new CustomerSqlInjector();
+        return new EnhanceSqlInjector();
     }
     }
 
 
     @Bean
     @Bean

+ 35 - 16
framework-database/src/main/java/com/chelvc/framework/database/context/DatabaseContextHolder.java

@@ -667,6 +667,35 @@ public final class DatabaseContextHolder {
         return new Page<>(number, size, counting);
         return new Page<>(number, size, counting);
     }
     }
 
 
+    /**
+     * 数据分页处理
+     *
+     * @param paging   分页信息
+     * @param function 分页处理函数
+     * @param <T>      数据类型
+     * @return 分页信息
+     */
+    public static <T> Pagination<T> paging(@NonNull Paging paging, @NonNull Function<IPage<T>, IPage<T>> function) {
+        IPage<T> page = function.apply(page(paging));
+        return page == null || ObjectUtils.isEmpty(page.getRecords()) ? Pagination.empty() : pagination(page);
+    }
+
+    /**
+     * 数据分页处理
+     *
+     * @param paging   分页信息
+     * @param function 分页处理函数
+     * @param adapter  数据转换适配器
+     * @param <T>      原始数据类型
+     * @param <R>      目标数据类型
+     * @return 分页信息
+     */
+    public static <T, R> Pagination<R> paging(@NonNull Paging paging, @NonNull Function<IPage<T>, IPage<T>> function,
+                                              @NonNull Function<List<T>, List<R>> adapter) {
+        IPage<T> page = function.apply(page(paging));
+        return page == null || ObjectUtils.isEmpty(page.getRecords()) ? Pagination.empty() : pagination(page, adapter);
+    }
+
     /**
     /**
      * 将MyBatis Plus分页参数转换成分页返回参数
      * 将MyBatis Plus分页参数转换成分页返回参数
      *
      *
@@ -686,7 +715,7 @@ public final class DatabaseContextHolder {
      * @param <T>  目标对象类型泛型
      * @param <T>  目标对象类型泛型
      * @return 分页返回参数
      * @return 分页返回参数
      */
      */
-    public static <T> Pagination<T> pagination(IPage<?> page, Class<T> type) {
+    public static <T> Pagination<T> pagination(IPage<?> page, @NonNull Class<T> type) {
         return pagination(page, ObjectUtils.copying((List<?>) ObjectUtils.ifNull(page, IPage::getRecords), type));
         return pagination(page, ObjectUtils.copying((List<?>) ObjectUtils.ifNull(page, IPage::getRecords), type));
     }
     }
 
 
@@ -701,7 +730,7 @@ public final class DatabaseContextHolder {
      */
      */
     public static <T, R> Pagination<R> pagination(IPage<T> page, @NonNull Function<List<T>, List<R>> adapter) {
     public static <T, R> Pagination<R> pagination(IPage<T> page, @NonNull Function<List<T>, List<R>> adapter) {
         List<T> records = ObjectUtils.ifNull(page, IPage::getRecords);
         List<T> records = ObjectUtils.ifNull(page, IPage::getRecords);
-        return pagination(page, CollectionUtils.isEmpty(records) ? Collections.emptyList() : adapter.apply(records));
+        return ObjectUtils.isEmpty(records) ? Pagination.empty() : pagination(page, adapter.apply(records));
     }
     }
 
 
     /**
     /**
@@ -713,19 +742,9 @@ public final class DatabaseContextHolder {
      * @return 分页返回参数
      * @return 分页返回参数
      */
      */
     public static <T> Pagination<T> pagination(IPage<?> page, List<T> records) {
     public static <T> Pagination<T> pagination(IPage<?> page, List<T> records) {
-        return page == null ? null : Pagination.<T>builder().total(page.getTotal()).pages(page.getPages())
-                .records(records).build();
-    }
-
-    /**
-     * 分页记录转换
-     *
-     * @param page    原始分页信息
-     * @param records 分页数据列表
-     * @param <T>     目标对象类型泛型
-     * @return 分页返回参数
-     */
-    public static <T> Pagination<T> pagination(Pagination<?> page, List<T> records) {
-        return page == null ? null : page.convert(records);
+        if (page == null || ObjectUtils.isEmpty(records)) {
+            return Pagination.empty();
+        }
+        return Pagination.<T>builder().total(page.getTotal()).pages(page.getPages()).records(records).build();
     }
     }
 }
 }

+ 0 - 46
framework-database/src/main/java/com/chelvc/framework/database/support/BasicService.java

@@ -1,46 +0,0 @@
-package com.chelvc.framework.database.support;
-
-import java.util.Collection;
-
-import com.baomidou.mybatisplus.extension.service.IService;
-
-/**
- * 基础业务操作接口
- *
- * @param <T> 数据模型类型
- * @author Woody
- * @date 2024/3/10
- */
-public interface BasicService<T> extends IService<T> {
-    /**
-     * 创建或忽略实体
-     *
-     * @param entity 实体对象实例
-     * @return true/false
-     */
-    boolean createIgnore(T entity);
-
-    /**
-     * 创建或修改实体
-     *
-     * @param entity 实体对象实例
-     * @return true/false
-     */
-    boolean createUpdate(T entity);
-
-    /**
-     * 批量创建或忽略实体
-     *
-     * @param entities 实体对象实例集合
-     * @return true/false
-     */
-    boolean batchCreateIgnore(Collection<T> entities);
-
-    /**
-     * 批量创建或修改实体
-     *
-     * @param entities 实体对象实例集合
-     * @return true/false
-     */
-    boolean batchCreateUpdate(Collection<T> entities);
-}

+ 0 - 37
framework-database/src/main/java/com/chelvc/framework/database/support/BasicServiceImpl.java

@@ -1,37 +0,0 @@
-package com.chelvc.framework.database.support;
-
-import java.util.Collection;
-
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.chelvc.framework.common.util.ObjectUtils;
-import lombok.NonNull;
-
-/**
- * 基础业务操作实现
- *
- * @param <M> 数据操作接口类型
- * @param <T> 数据模型类型
- * @author Woody
- * @date 2024/3/10
- */
-public class BasicServiceImpl<M extends BasicMapper<T>, T> extends ServiceImpl<M, T> implements BasicService<T> {
-    @Override
-    public boolean createIgnore(T entity) {
-        return entity != null && this.baseMapper.insertIgnore(entity) > 0;
-    }
-
-    @Override
-    public boolean createUpdate(T entity) {
-        return entity != null && this.baseMapper.insertUpdate(entity) > 0;
-    }
-
-    @Override
-    public boolean batchCreateIgnore(Collection<T> entities) {
-        return ObjectUtils.notEmpty(entities) && this.baseMapper.batchInsertIgnore(entities) > 0;
-    }
-
-    @Override
-    public boolean batchCreateUpdate(@NonNull Collection<T> entities) {
-        return ObjectUtils.notEmpty(entities) && this.baseMapper.batchInsertUpdate(entities) > 0;
-    }
-}

+ 45 - 0
framework-database/src/main/java/com/chelvc/framework/database/support/EnhanceLambdaQueryWrapper.java

@@ -0,0 +1,45 @@
+package com.chelvc.framework.database.support;
+
+import java.util.List;
+import java.util.function.Function;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
+import com.chelvc.framework.common.model.Pagination;
+import com.chelvc.framework.common.model.Paging;
+import com.chelvc.framework.database.context.DatabaseContextHolder;
+import lombok.NonNull;
+
+/**
+ * Lambda查询包装器增强实现
+ *
+ * @author Woody
+ * @date 2024/3/18
+ */
+public class EnhanceLambdaQueryWrapper<T> extends LambdaQueryChainWrapper<T> {
+    public EnhanceLambdaQueryWrapper(BaseMapper<T> mapper) {
+        super(mapper);
+    }
+
+    /**
+     * 分页查询
+     *
+     * @param paging 查询分页信息
+     * @return 数据分页信息
+     */
+    public Pagination<T> paging(@NonNull Paging paging) {
+        return DatabaseContextHolder.paging(paging, this::page);
+    }
+
+    /**
+     * 分页查询
+     *
+     * @param paging  查询分页信息
+     * @param adapter 数据转换适配器
+     * @param <R>     目标数据类型
+     * @return 数据分页信息
+     */
+    public <R> Pagination<R> paging(@NonNull Paging paging, @NonNull Function<List<T>, List<R>> adapter) {
+        return DatabaseContextHolder.paging(paging, this::page, adapter);
+    }
+}

+ 2 - 2
framework-database/src/main/java/com/chelvc/framework/database/support/BasicMapper.java → framework-database/src/main/java/com/chelvc/framework/database/support/EnhanceMapper.java

@@ -5,12 +5,12 @@ import java.util.Collection;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 
 
 /**
 /**
- * 数据模型操作接口
+ * 数据模型增强操作接口
  *
  *
  * @author Woody
  * @author Woody
  * @date 2024/2/27
  * @date 2024/2/27
  */
  */
-public interface BasicMapper<T> extends BaseMapper<T> {
+public interface EnhanceMapper<T> extends BaseMapper<T> {
     /**
     /**
      * 创建或忽略实体
      * 创建或忽略实体
      *
      *

+ 46 - 0
framework-database/src/main/java/com/chelvc/framework/database/support/EnhanceQueryWrapper.java

@@ -0,0 +1,46 @@
+package com.chelvc.framework.database.support;
+
+import java.util.List;
+import java.util.function.Function;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
+import com.chelvc.framework.common.model.Pagination;
+import com.chelvc.framework.common.model.Paging;
+import com.chelvc.framework.database.context.DatabaseContextHolder;
+import lombok.NonNull;
+
+/**
+ * 查询包装器增强实现
+ *
+ * @param <T> 数据类型
+ * @author Woody
+ * @date 2024/3/18
+ */
+public class EnhanceQueryWrapper<T> extends QueryChainWrapper<T> {
+    public EnhanceQueryWrapper(BaseMapper<T> mapper) {
+        super(mapper);
+    }
+
+    /**
+     * 分页查询
+     *
+     * @param paging 查询分页信息
+     * @return 数据分页信息
+     */
+    public Pagination<T> paging(@NonNull Paging paging) {
+        return DatabaseContextHolder.paging(paging, this::page);
+    }
+
+    /**
+     * 分页查询
+     *
+     * @param paging  查询分页信息
+     * @param adapter 数据转换适配器
+     * @param <R>     目标数据类型
+     * @return 数据分页信息
+     */
+    public <R> Pagination<R> paging(@NonNull Paging paging, @NonNull Function<List<T>, List<R>> adapter) {
+        return DatabaseContextHolder.paging(paging, this::page, adapter);
+    }
+}

+ 119 - 0
framework-database/src/main/java/com/chelvc/framework/database/support/EnhanceService.java

@@ -0,0 +1,119 @@
+package com.chelvc.framework.database.support;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.function.Function;
+
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.chelvc.framework.common.model.Pagination;
+import com.chelvc.framework.common.model.Paging;
+import com.chelvc.framework.database.context.DatabaseContextHolder;
+import lombok.NonNull;
+
+/**
+ * 业务增强操作接口
+ *
+ * @param <T> 数据模型类型
+ * @author Woody
+ * @date 2024/3/10
+ */
+public interface EnhanceService<T> extends IService<T> {
+    /**
+     * 创建或忽略实体
+     *
+     * @param entity 实体对象实例
+     * @return true/false
+     */
+    default boolean createIgnore(@NonNull T entity) {
+        return ((EnhanceMapper<T>) this.getBaseMapper()).insertIgnore(entity) > 0;
+    }
+
+    /**
+     * 创建或修改实体
+     *
+     * @param entity 实体对象实例
+     * @return true/false
+     */
+    default boolean createUpdate(@NonNull T entity) {
+        return ((EnhanceMapper<T>) this.getBaseMapper()).insertUpdate(entity) > 0;
+    }
+
+    /**
+     * 批量创建或忽略实体
+     *
+     * @param entities 实体对象实例集合
+     * @return true/false
+     */
+    default boolean batchCreateIgnore(@NonNull Collection<T> entities) {
+        return !entities.isEmpty() && ((EnhanceMapper<T>) this.getBaseMapper()).batchInsertIgnore(entities) > 0;
+    }
+
+    /**
+     * 批量创建或修改实体
+     *
+     * @param entities 实体对象实例集合
+     * @return true/false
+     */
+    default boolean batchCreateUpdate(@NonNull Collection<T> entities) {
+        return !entities.isEmpty() && ((EnhanceMapper<T>) this.getBaseMapper()).batchInsertUpdate(entities) > 0;
+    }
+
+    @Override
+    default EnhanceQueryWrapper<T> query() {
+        return new EnhanceQueryWrapper<>(this.getBaseMapper());
+    }
+
+    @Override
+    default EnhanceLambdaQueryWrapper<T> lambdaQuery() {
+        return new EnhanceLambdaQueryWrapper<>(this.getBaseMapper());
+    }
+
+    /**
+     * 分页查询
+     *
+     * @param paging 查询分页信息
+     * @return 数据分页信息
+     */
+    default Pagination<T> paging(@NonNull Paging paging) {
+        return this.paging(Wrappers.emptyWrapper(), paging);
+    }
+
+    /**
+     * 分页查询
+     *
+     * @param paging  查询分页信息
+     * @param adapter 数据转换适配器
+     * @param <R>     目标数据类型
+     * @return 数据分页信息
+     */
+    default <R> Pagination<R> paging(@NonNull Paging paging, @NonNull Function<List<T>, @NonNull List<R>> adapter) {
+        return this.paging(Wrappers.emptyWrapper(), paging, adapter);
+    }
+
+    /**
+     * 分页查询
+     *
+     * @param query  查询对象
+     * @param paging 查询分页信息
+     * @return 数据分页信息
+     */
+    default Pagination<T> paging(@NonNull Wrapper<T> query, @NonNull Paging paging) {
+        return DatabaseContextHolder.paging(paging, page -> this.page(page, query));
+    }
+
+    /**
+     * 分页查询
+     *
+     * @param query   查询对象
+     * @param paging  查询分页信息
+     * @param adapter 数据转换适配器
+     * @param <R>     目标数据类型
+     * @return 数据分页信息
+     */
+    default <R> Pagination<R> paging(@NonNull Wrapper<T> query, @NonNull Paging paging,
+                                     @NonNull Function<List<T>, List<R>> adapter) {
+        return DatabaseContextHolder.paging(paging, page -> this.page(page, query), adapter);
+    }
+}

+ 2 - 2
framework-database/src/main/java/com/chelvc/framework/database/support/CustomerSqlInjector.java → framework-database/src/main/java/com/chelvc/framework/database/support/EnhanceSqlInjector.java

@@ -6,12 +6,12 @@ import com.baomidou.mybatisplus.core.injector.AbstractMethod;
 import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
 import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
 
 
 /**
 /**
- * 自定义SQL注入器实现
+ * SQL注入器增强实现
  *
  *
  * @author Woody
  * @author Woody
  * @date 2024/2/27
  * @date 2024/2/27
  */
  */
-public class CustomerSqlInjector extends DefaultSqlInjector {
+public class EnhanceSqlInjector extends DefaultSqlInjector {
     @Override
     @Override
     public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
     public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
         List<AbstractMethod> methods = super.getMethodList(mapperClass);
         List<AbstractMethod> methods = super.getMethodList(mapperClass);

+ 1 - 1
framework-security/src/main/java/com/chelvc/framework/security/interceptor/ControllerCryptoInterceptor.java

@@ -118,7 +118,7 @@ public class ControllerCryptoInterceptor extends RequestBodyAdviceAdapter implem
         String secret = Objects.requireNonNull(this.properties.getSecret()), iv = this.iv();
         String secret = Objects.requireNonNull(this.properties.getSecret()), iv = this.iv();
         if (body instanceof Result) {
         if (body instanceof Result) {
             Result<?> result = (Result<?>) body;
             Result<?> result = (Result<?>) body;
-            if (result.getData() == null) {
+            if (result.getData() == null || result.isFailure()) {
                 return result;
                 return result;
             }
             }
             String ciphertext = AESUtils.encode(JacksonContextHolder.serialize(result.getData()), secret, iv);
             String ciphertext = AESUtils.encode(JacksonContextHolder.serialize(result.getData()), secret, iv);