mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 08:13:44 +08:00 
			
		
		
		
	update 通用Service接口 增加自定义vo转换函数
This commit is contained in:
		@@ -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<T> extends IService<T> {
 | 
			
		||||
        return BeanUtil.toBean(t, kClass);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据 ID 查询
 | 
			
		||||
     *
 | 
			
		||||
     * @param id        主键ID
 | 
			
		||||
     * @param convertor 转换函数
 | 
			
		||||
     * @param <K>       vo类型
 | 
			
		||||
     */
 | 
			
		||||
    default <K> K getVoById(Serializable id, Function<T, K> convertor) {
 | 
			
		||||
        T t = getBaseMapper().selectById(id);
 | 
			
		||||
        return convertor.apply(t);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询(根据ID 批量查询)
 | 
			
		||||
     *
 | 
			
		||||
@@ -46,6 +59,21 @@ public interface IServicePlus<T> extends IService<T> {
 | 
			
		||||
                .collect(Collectors.toList());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询(根据ID 批量查询)
 | 
			
		||||
     *
 | 
			
		||||
     * @param convertor 转换函数
 | 
			
		||||
     * @param idList    主键ID列表
 | 
			
		||||
     */
 | 
			
		||||
    default <K> List<K> listVoByIds(Collection<? extends Serializable> idList,
 | 
			
		||||
                                    Function<Collection<T>, List<K>> convertor) {
 | 
			
		||||
        List<T> list = getBaseMapper().selectBatchIds(idList);
 | 
			
		||||
        if (list == null) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        return convertor.apply(list);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询(根据 columnMap 条件)
 | 
			
		||||
     *
 | 
			
		||||
@@ -62,6 +90,21 @@ public interface IServicePlus<T> extends IService<T> {
 | 
			
		||||
                .collect(Collectors.toList());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询(根据 columnMap 条件)
 | 
			
		||||
     *
 | 
			
		||||
     * @param convertor 转换函数
 | 
			
		||||
     * @param columnMap 表字段 map 对象
 | 
			
		||||
     */
 | 
			
		||||
    default <K> List<K> listVoByMap(Map<String, Object> columnMap,
 | 
			
		||||
                                    Function<Collection<T>, List<K>> convertor) {
 | 
			
		||||
        List<T> list = getBaseMapper().selectByMap(columnMap);
 | 
			
		||||
        if (list == null) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        return convertor.apply(list);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据 Wrapper,查询一条记录 <br/>
 | 
			
		||||
     * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
 | 
			
		||||
@@ -73,6 +116,17 @@ public interface IServicePlus<T> extends IService<T> {
 | 
			
		||||
        return BeanUtil.toBean(getOne(queryWrapper, true), kClass);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据 Wrapper,查询一条记录 <br/>
 | 
			
		||||
     * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
 | 
			
		||||
     *
 | 
			
		||||
     * @param convertor    转换函数
 | 
			
		||||
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
 | 
			
		||||
     */
 | 
			
		||||
    default <K> K getVoOne(Wrapper<T> queryWrapper, Function<T, K> convertor) {
 | 
			
		||||
        return convertor.apply(getOne(queryWrapper, true));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询列表
 | 
			
		||||
     *
 | 
			
		||||
@@ -89,6 +143,20 @@ public interface IServicePlus<T> extends IService<T> {
 | 
			
		||||
                .collect(Collectors.toList());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询列表
 | 
			
		||||
     *
 | 
			
		||||
     * @param convertor    转换函数
 | 
			
		||||
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
 | 
			
		||||
     */
 | 
			
		||||
    default <K> List<K> listVo(Wrapper<T> queryWrapper, Function<Collection<T>, List<K>> convertor) {
 | 
			
		||||
        List<T> list = getBaseMapper().selectList(queryWrapper);
 | 
			
		||||
        if (list == null) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        return convertor.apply(list);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询所有
 | 
			
		||||
     *
 | 
			
		||||
@@ -99,28 +167,64 @@ public interface IServicePlus<T> extends IService<T> {
 | 
			
		||||
        return listVo(Wrappers.emptyWrapper(), kClass);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询所有
 | 
			
		||||
     *
 | 
			
		||||
     * @param convertor 转换函数
 | 
			
		||||
     * @see Wrappers#emptyWrapper()
 | 
			
		||||
     */
 | 
			
		||||
    default <K> List<K> listVo(Function<Collection<T>, List<K>> convertor) {
 | 
			
		||||
        return listVo(Wrappers.emptyWrapper(), convertor);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 翻页查询
 | 
			
		||||
     *
 | 
			
		||||
     * @param page         翻页对象
 | 
			
		||||
     * @param queryWrapper 实体对象封装操作类
 | 
			
		||||
     * @param kClass vo类型
 | 
			
		||||
     * @param kClass       vo类型
 | 
			
		||||
     */
 | 
			
		||||
    default <K> PagePlus<T, K> pageVo(PagePlus<T, K> page, Wrapper<T> queryWrapper, Class<K> kClass) {
 | 
			
		||||
        PagePlus<T, K> e = getBaseMapper().selectPage(page, queryWrapper);
 | 
			
		||||
        page.recordsToVo(kClass);
 | 
			
		||||
        return page;
 | 
			
		||||
        PagePlus<T, K> result = getBaseMapper().selectPage(page, queryWrapper);
 | 
			
		||||
        List<K> 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 <K> PagePlus<T, K> pageVo(PagePlus<T, K> page, Wrapper<T> queryWrapper,
 | 
			
		||||
                                      Function<Collection<T>, List<K>> convertor) {
 | 
			
		||||
        PagePlus<T, K> result = getBaseMapper().selectPage(page, queryWrapper);
 | 
			
		||||
        return result.setRecordsVo(convertor.apply(result.getRecords()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 无条件翻页查询
 | 
			
		||||
     *
 | 
			
		||||
     * @param page 翻页对象
 | 
			
		||||
     * @param page   翻页对象
 | 
			
		||||
     * @param kClass vo类型
 | 
			
		||||
     */
 | 
			
		||||
    default <K> PagePlus<T, K> pageVo(PagePlus<T, K> page, Class<K> kClass) {
 | 
			
		||||
        return pageVo(page, Wrappers.emptyWrapper(), kClass);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 无条件翻页查询
 | 
			
		||||
     *
 | 
			
		||||
     * @param page      翻页对象
 | 
			
		||||
     * @param convertor 转换函数
 | 
			
		||||
     */
 | 
			
		||||
    default <K> PagePlus<T, K> pageVo(PagePlus<T, K> page, Function<Collection<T>, List<K>> convertor) {
 | 
			
		||||
        return pageVo(page, Wrappers.emptyWrapper(), convertor);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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<T,K> implements IPage<T> {
 | 
			
		||||
 | 
			
		||||
    protected List<T> records;
 | 
			
		||||
    protected List<K> recordsVo;
 | 
			
		||||
    protected long total;
 | 
			
		||||
    protected long size;
 | 
			
		||||
    protected long current;
 | 
			
		||||
    protected List<OrderItem> orders;
 | 
			
		||||
    protected boolean optimizeCountSql;
 | 
			
		||||
    protected boolean isSearchCount;
 | 
			
		||||
    protected boolean hitCount;
 | 
			
		||||
    protected String countId;
 | 
			
		||||
    protected Long maxLimit;
 | 
			
		||||
    /**
 | 
			
		||||
     * domain实体列表
 | 
			
		||||
     */
 | 
			
		||||
    private List<T> records = Collections.emptyList();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * vo实体列表
 | 
			
		||||
     */
 | 
			
		||||
    private List<K> recordsVo = Collections.emptyList();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 总数
 | 
			
		||||
     */
 | 
			
		||||
    private long total = 0L;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 页长度
 | 
			
		||||
     */
 | 
			
		||||
    private long size = 10L;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 当前页
 | 
			
		||||
     */
 | 
			
		||||
    private long current = 1L;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 排序字段信息
 | 
			
		||||
     */
 | 
			
		||||
    private List<OrderItem> 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<T,K> implements IPage<T> {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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<T,K> implements IPage<T> {
 | 
			
		||||
        return this.current < this.getPages();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void recordsToVo(Class<K> 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<T,K> implements IPage<T> {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isSearchCount() {
 | 
			
		||||
        return this.total < 0L ? false : this.isSearchCount;
 | 
			
		||||
        return this.total >= 0L && this.isSearchCount;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public PagePlus<T, K> setSearchCount(boolean isSearchCount) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user