update 通用Service接口 增加自定义vo转换函数
This commit is contained in:
parent
3c57f468c6
commit
3e08a8e1c1
|
@ -9,6 +9,7 @@ import java.io.Serializable;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,6 +31,18 @@ public interface IServicePlus<T> extends IService<T> {
|
||||||
return BeanUtil.toBean(t, kClass);
|
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 批量查询)
|
* 查询(根据ID 批量查询)
|
||||||
*
|
*
|
||||||
|
@ -46,6 +59,21 @@ public interface IServicePlus<T> extends IService<T> {
|
||||||
.collect(Collectors.toList());
|
.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 条件)
|
* 查询(根据 columnMap 条件)
|
||||||
*
|
*
|
||||||
|
@ -62,6 +90,21 @@ public interface IServicePlus<T> extends IService<T> {
|
||||||
.collect(Collectors.toList());
|
.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/>
|
* 根据 Wrapper,查询一条记录 <br/>
|
||||||
* <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
|
* <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
|
||||||
|
@ -73,6 +116,17 @@ public interface IServicePlus<T> extends IService<T> {
|
||||||
return BeanUtil.toBean(getOne(queryWrapper, true), kClass);
|
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());
|
.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,6 +167,16 @@ public interface IServicePlus<T> extends IService<T> {
|
||||||
return listVo(Wrappers.emptyWrapper(), kClass);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 翻页查询
|
* 翻页查询
|
||||||
*
|
*
|
||||||
|
@ -107,9 +185,25 @@ public interface IServicePlus<T> extends IService<T> {
|
||||||
* @param kClass vo类型
|
* @param kClass vo类型
|
||||||
*/
|
*/
|
||||||
default <K> PagePlus<T, K> pageVo(PagePlus<T, K> page, Wrapper<T> queryWrapper, Class<K> kClass) {
|
default <K> PagePlus<T, K> pageVo(PagePlus<T, K> page, Wrapper<T> queryWrapper, Class<K> kClass) {
|
||||||
PagePlus<T, K> e = getBaseMapper().selectPage(page, queryWrapper);
|
PagePlus<T, K> result = getBaseMapper().selectPage(page, queryWrapper);
|
||||||
page.recordsToVo(kClass);
|
List<K> volist = result.getRecords().stream()
|
||||||
return page;
|
.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()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -122,5 +216,15 @@ public interface IServicePlus<T> extends IService<T> {
|
||||||
return pageVo(page, Wrappers.emptyWrapper(), 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;
|
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.IPage;
|
||||||
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
@ -10,34 +9,67 @@ import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Accessors(chain = true)
|
@Accessors(chain = true)
|
||||||
public class PagePlus<T,K> implements IPage<T> {
|
public class PagePlus<T,K> implements IPage<T> {
|
||||||
|
|
||||||
protected List<T> records;
|
/**
|
||||||
protected List<K> recordsVo;
|
* domain实体列表
|
||||||
protected long total;
|
*/
|
||||||
protected long size;
|
private List<T> records = Collections.emptyList();
|
||||||
protected long current;
|
|
||||||
protected List<OrderItem> orders;
|
/**
|
||||||
protected boolean optimizeCountSql;
|
* vo实体列表
|
||||||
protected boolean isSearchCount;
|
*/
|
||||||
protected boolean hitCount;
|
private List<K> recordsVo = Collections.emptyList();
|
||||||
protected String countId;
|
|
||||||
protected Long maxLimit;
|
/**
|
||||||
|
* 总数
|
||||||
|
*/
|
||||||
|
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() {
|
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) {
|
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) {
|
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) {
|
if (current > 1L) {
|
||||||
this.current = current;
|
this.current = current;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.size = size;
|
this.size = size;
|
||||||
this.total = total;
|
this.total = total;
|
||||||
this.isSearchCount = isSearchCount;
|
this.isSearchCount = isSearchCount;
|
||||||
|
@ -78,12 +101,6 @@ public class PagePlus<T,K> implements IPage<T> {
|
||||||
return this.current < this.getPages();
|
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
|
@Override
|
||||||
public String countId() {
|
public String countId() {
|
||||||
return this.getCountId();
|
return this.getCountId();
|
||||||
|
@ -116,7 +133,7 @@ public class PagePlus<T,K> implements IPage<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isSearchCount() {
|
public boolean isSearchCount() {
|
||||||
return this.total < 0L ? false : this.isSearchCount;
|
return this.total >= 0L && this.isSearchCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PagePlus<T, K> setSearchCount(boolean isSearchCount) {
|
public PagePlus<T, K> setSearchCount(boolean isSearchCount) {
|
||||||
|
|
Loading…
Reference in New Issue