From 3e08a8e1c1ea923035c09af6cf1f802c1f6dd25b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Sat, 15 May 2021 14:27:29 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E9=80=9A=E7=94=A8Service=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=20=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89vo?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/core/page/IServicePlus.java | 114 +++++++++++++++++- .../com/ruoyi/common/core/page/PagePlus.java | 93 ++++++++------ 2 files changed, 164 insertions(+), 43 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/page/IServicePlus.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/page/IServicePlus.java index 864a38e8..aeaee0b5 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/page/IServicePlus.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/page/IServicePlus.java @@ -9,6 +9,7 @@ import java.io.Serializable; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; /** @@ -30,6 +31,18 @@ public interface IServicePlus extends IService { return BeanUtil.toBean(t, kClass); } + /** + * 根据 ID 查询 + * + * @param id 主键ID + * @param convertor 转换函数 + * @param vo类型 + */ + default K getVoById(Serializable id, Function convertor) { + T t = getBaseMapper().selectById(id); + return convertor.apply(t); + } + /** * 查询(根据ID 批量查询) * @@ -46,6 +59,21 @@ public interface IServicePlus extends IService { .collect(Collectors.toList()); } + /** + * 查询(根据ID 批量查询) + * + * @param convertor 转换函数 + * @param idList 主键ID列表 + */ + default List listVoByIds(Collection idList, + Function, List> convertor) { + List list = getBaseMapper().selectBatchIds(idList); + if (list == null) { + return null; + } + return convertor.apply(list); + } + /** * 查询(根据 columnMap 条件) * @@ -62,6 +90,21 @@ public interface IServicePlus extends IService { .collect(Collectors.toList()); } + /** + * 查询(根据 columnMap 条件) + * + * @param convertor 转换函数 + * @param columnMap 表字段 map 对象 + */ + default List listVoByMap(Map columnMap, + Function, List> convertor) { + List list = getBaseMapper().selectByMap(columnMap); + if (list == null) { + return null; + } + return convertor.apply(list); + } + /** * 根据 Wrapper,查询一条记录
*

结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")

@@ -73,6 +116,17 @@ public interface IServicePlus extends IService { return BeanUtil.toBean(getOne(queryWrapper, true), kClass); } + /** + * 根据 Wrapper,查询一条记录
+ *

结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")

+ * + * @param convertor 转换函数 + * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} + */ + default K getVoOne(Wrapper queryWrapper, Function convertor) { + return convertor.apply(getOne(queryWrapper, true)); + } + /** * 查询列表 * @@ -89,6 +143,20 @@ public interface IServicePlus extends IService { .collect(Collectors.toList()); } + /** + * 查询列表 + * + * @param convertor 转换函数 + * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} + */ + default List listVo(Wrapper queryWrapper, Function, List> convertor) { + List list = getBaseMapper().selectList(queryWrapper); + if (list == null) { + return null; + } + return convertor.apply(list); + } + /** * 查询所有 * @@ -99,28 +167,64 @@ public interface IServicePlus extends IService { return listVo(Wrappers.emptyWrapper(), kClass); } + /** + * 查询所有 + * + * @param convertor 转换函数 + * @see Wrappers#emptyWrapper() + */ + default List listVo(Function, List> convertor) { + return listVo(Wrappers.emptyWrapper(), convertor); + } + /** * 翻页查询 * * @param page 翻页对象 * @param queryWrapper 实体对象封装操作类 - * @param kClass vo类型 + * @param kClass vo类型 */ default PagePlus pageVo(PagePlus page, Wrapper queryWrapper, Class kClass) { - PagePlus e = getBaseMapper().selectPage(page, queryWrapper); - page.recordsToVo(kClass); - return page; + PagePlus result = getBaseMapper().selectPage(page, queryWrapper); + List volist = result.getRecords().stream() + .map(any -> BeanUtil.toBean(any, kClass)) + .collect(Collectors.toList()); + result.setRecordsVo(volist); + return result; + } + + /** + * 翻页查询 + * + * @param page 翻页对象 + * @param queryWrapper 实体对象封装操作类 + * @param convertor 转换函数 + */ + default PagePlus pageVo(PagePlus page, Wrapper queryWrapper, + Function, List> convertor) { + PagePlus result = getBaseMapper().selectPage(page, queryWrapper); + return result.setRecordsVo(convertor.apply(result.getRecords())); } /** * 无条件翻页查询 * - * @param page 翻页对象 + * @param page 翻页对象 * @param kClass vo类型 */ default PagePlus pageVo(PagePlus page, Class kClass) { return pageVo(page, Wrappers.emptyWrapper(), kClass); } + /** + * 无条件翻页查询 + * + * @param page 翻页对象 + * @param convertor 转换函数 + */ + default PagePlus pageVo(PagePlus page, Function, List> convertor) { + return pageVo(page, Wrappers.emptyWrapper(), convertor); + } + } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/page/PagePlus.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/page/PagePlus.java index 7200fc51..889d549f 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/page/PagePlus.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/page/PagePlus.java @@ -1,6 +1,5 @@ package com.ruoyi.common.core.page; -import cn.hutool.core.bean.BeanUtil; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.OrderItem; import lombok.Data; @@ -10,34 +9,67 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; @Data @Accessors(chain = true) public class PagePlus implements IPage { - protected List records; - protected List recordsVo; - protected long total; - protected long size; - protected long current; - protected List orders; - protected boolean optimizeCountSql; - protected boolean isSearchCount; - protected boolean hitCount; - protected String countId; - protected Long maxLimit; + /** + * domain实体列表 + */ + private List records = Collections.emptyList(); + + /** + * vo实体列表 + */ + private List recordsVo = Collections.emptyList(); + + /** + * 总数 + */ + private long total = 0L; + + /** + * 页长度 + */ + private long size = 10L; + + /** + * 当前页 + */ + private long current = 1L; + + /** + * 排序字段信息 + */ + private List orders = new ArrayList<>(); + + /** + * 自动优化 COUNT SQL + */ + private boolean optimizeCountSql = true; + + /** + * 是否进行 count 查询 + */ + private boolean isSearchCount = true; + + /** + * 是否命中count缓存 + */ + private boolean hitCount = false; + + /** + * countId + */ + private String countId; + + /** + * 最大limit + */ + private Long maxLimit; public PagePlus() { - this.records = Collections.emptyList(); - this.recordsVo = Collections.emptyList(); - this.total = 0L; - this.size = 10L; - this.current = 1L; - this.orders = new ArrayList(); - this.optimizeCountSql = true; - this.isSearchCount = true; - this.hitCount = false; } public PagePlus(long current, long size) { @@ -53,18 +85,9 @@ public class PagePlus implements IPage { } public PagePlus(long current, long size, long total, boolean isSearchCount) { - this.records = Collections.emptyList(); - this.total = 0L; - this.size = 10L; - this.current = 1L; - this.orders = new ArrayList(); - this.optimizeCountSql = true; - this.isSearchCount = true; - this.hitCount = false; if (current > 1L) { this.current = current; } - this.size = size; this.total = total; this.isSearchCount = isSearchCount; @@ -78,12 +101,6 @@ public class PagePlus implements IPage { return this.current < this.getPages(); } - public void recordsToVo(Class kClass) { - this.recordsVo = this.records.stream() - .map(any -> BeanUtil.toBean(any, kClass)) - .collect(Collectors.toList()); - } - @Override public String countId() { return this.getCountId(); @@ -116,7 +133,7 @@ public class PagePlus implements IPage { @Override public boolean isSearchCount() { - return this.total < 0L ? false : this.isSearchCount; + return this.total >= 0L && this.isSearchCount; } public PagePlus setSearchCount(boolean isSearchCount) {