refactor(mapper): 重构MyBatis Plus基础接口功能
- 添加StrUtil工具类导入用于字符串处理 - 优化getMapWithKv方法实现,支持下划线转驼峰并使用HashMap替代ConcurrentHashMap - 重命名queryFeatureClazzList为queryFeatureClazz提升方法名准确性 - 简化searchFeatureClazz方法参数列表 - 更新注释标注所有功能已测试完成 - 移除LambdaQueryChainWrapper相关方法以简化接口定义 - 统一代码格式化并添加必要的空行提升可读性 refactor(service): 重构服务层接口和实现类 - 重命名saveFeatureClazzList为batchSaveFeatureClazz提升语义清晰度 - 重命名modifyFeatureClazzById为updateFeatureClazzById统一更新方法命名 - 重命名queryFeatureClazzById为getFeatureClazzById统一查询方法命名 - 新增batchUpdateFeatureClazzById方法支持批量更新操作 - 重命名queryClazzByArgs为getClazzByArgs保持查询方法命名一致性 - 重命名searchClazzPageByArgs和searchFeatureClazzPageByArgs替换原分页查询方法 - 移除过时的queryFeatureClazzPageByArgs相关方法 - 更新注释标注所有功能已测试完成 refactor(serviceimpl): 重构服务实现类逻辑 - 完善batchSaveFeatureClazz方法实现逻辑 - 更新batchUpdateFeatureClazzById方法实现 - 使用KitValidUtil替代BadRequestException进行参数验证 - 简化getFeatureClazzById方法实现逻辑 - 优化getClazzByArgs和getFeatureClazzByArgs方法排序逻辑 - 更新updateClazzByArgs方法使用LambdaUpdateWrapper - 移除过时的queryFeatureClazzPageByArgs相关实现 - 统一代码格式化并移除无用的导入包 refactor(beanutil): 优化Bean转换工具类 - 在copyToClass方法中添加空值检查防止NPE - 在copyToTarget方法中添加源和目标对象空值检查 - 优化copyToList方法处理空集合情况返回固定大小列表 - 统一代码格式化并添加必要的空行提升可读性
This commit is contained in:
+20
-83
@@ -17,22 +17,19 @@ package cn.odboy.framework.mybatisplus.core.interfaces;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.odboy.base.KitPageResult;
|
import cn.odboy.base.KitPageResult;
|
||||||
import cn.odboy.util.KitBeanUtil;
|
import cn.odboy.util.KitBeanUtil;
|
||||||
import cn.odboy.util.KitPageUtil;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import org.apache.ibatis.annotations.Param;
|
|
||||||
import org.springframework.data.domain.Pageable;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 公共抽象Mapper接口类
|
* 公共抽象Mapper接口类
|
||||||
@@ -43,7 +40,7 @@ import org.springframework.data.domain.Pageable;
|
|||||||
public interface KitMpMapper<E> extends BaseMapper<E> {
|
public interface KitMpMapper<E> extends BaseMapper<E> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量插入
|
* 批量插入(已测试)
|
||||||
*
|
*
|
||||||
* @param entityList List<E>
|
* @param entityList List<E>
|
||||||
* @return 影响的行数
|
* @return 影响的行数
|
||||||
@@ -51,7 +48,7 @@ public interface KitMpMapper<E> extends BaseMapper<E> {
|
|||||||
int insertBatchSomeColumn(List<E> entityList);
|
int insertBatchSomeColumn(List<E> entityList);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据id全量更新
|
* 根据id全量更新,为空的字段也会被更新(已测试)
|
||||||
*
|
*
|
||||||
* @param entity E
|
* @param entity E
|
||||||
* @return 影响的行数
|
* @return 影响的行数
|
||||||
@@ -59,25 +56,24 @@ public interface KitMpMapper<E> extends BaseMapper<E> {
|
|||||||
int alwaysUpdateSomeColumnById(@Param("et") E entity);
|
int alwaysUpdateSomeColumnById(@Param("et") E entity);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取字典
|
* 获取字典(已测试)
|
||||||
*
|
*
|
||||||
* @param wrapper LambdaQueryWrapper<E>
|
* @param wrapper LambdaQueryWrapper<E>
|
||||||
* @param keyName 键对应的列名称
|
* @param keyName 键对应的列名称(下划线会自动转驼峰)
|
||||||
* @param valueName 值对应的列名称
|
* @param valueName 值对应的列名称
|
||||||
* @return Map<String, Object>
|
* @return Map<String, Object>
|
||||||
*/
|
*/
|
||||||
default Map<String, Object> getMapWithKv(LambdaQueryWrapper<E> wrapper, String keyName, String valueName) {
|
default Map<String, Object> getMapWithKv(LambdaQueryWrapper<E> wrapper, String keyName, String valueName) {
|
||||||
Map<String, Object> result = new ConcurrentHashMap<>(1);
|
Map<String, Object> result = new HashMap<>(1);
|
||||||
List<E> list = this.selectList(wrapper);
|
List<E> list = this.selectList(wrapper);
|
||||||
if (CollUtil.isEmpty(list)) {
|
if (CollUtil.isEmpty(list)) {
|
||||||
return new HashMap<>(3);
|
return new HashMap<>(1);
|
||||||
}
|
}
|
||||||
E tempObj = list.stream().findFirst().orElse(null);
|
E tempObj = list.stream().findFirst().orElse(null);
|
||||||
if (tempObj == null) {
|
if (tempObj == null) {
|
||||||
return new HashMap<>(3);
|
return new HashMap<>(1);
|
||||||
}
|
}
|
||||||
Field[] fields = ReflectUtil.getFields(tempObj.getClass());
|
Field[] fields = ReflectUtil.getFields(tempObj.getClass());
|
||||||
for (E e : list) {
|
|
||||||
Field keyField = null;
|
Field keyField = null;
|
||||||
Field valueField = null;
|
Field valueField = null;
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
@@ -89,99 +85,40 @@ public interface KitMpMapper<E> extends BaseMapper<E> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (E e : list) {
|
||||||
String fieldKey = String.valueOf(ReflectUtil.getFieldValue(e, keyField));
|
String fieldKey = String.valueOf(ReflectUtil.getFieldValue(e, keyField));
|
||||||
Object fieldValue = ReflectUtil.getFieldValue(e, valueField);
|
|
||||||
if (fieldKey != null) {
|
if (fieldKey != null) {
|
||||||
result.put(fieldKey, fieldValue);
|
// 下划线转驼峰
|
||||||
|
String camelFieldKey = StrUtil.toCamelCase(fieldKey);
|
||||||
|
Object fieldValue = ReflectUtil.getFieldValue(e, valueField);
|
||||||
|
result.putIfAbsent(camelFieldKey, fieldValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 条件查询列表, 并返回期望的对象
|
* 条件查询列表, 并返回期望的对象(已测试)
|
||||||
*
|
*
|
||||||
* @param wrapper LambdaQueryWrapper<E>
|
* @param wrapper LambdaQueryWrapper<E>
|
||||||
* @param clazz 期望的对象类型
|
* @param clazz 期望的对象类型
|
||||||
* @return List<T>
|
* @return List<T>
|
||||||
*/
|
*/
|
||||||
default <T> List<T> queryFeatureClazzList(LambdaQueryWrapper<E> wrapper, Class<T> clazz) {
|
default <T> List<T> queryFeatureClazz(LambdaQueryWrapper<E> wrapper, Class<T> clazz) {
|
||||||
List<E> list = this.selectList(wrapper);
|
List<E> list = this.selectList(wrapper);
|
||||||
if (CollUtil.isEmpty(list)) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
return KitBeanUtil.copyToList(list, clazz);
|
return KitBeanUtil.copyToList(list, clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 条件查询列表, 并返回期望的对象
|
* 条件查询分页列表, 并返回期望的对象(已测试)
|
||||||
*
|
*
|
||||||
* @param wrapper LambdaQueryWrapper<E>
|
|
||||||
* @param clazz 期望的对象类型
|
|
||||||
* @return List<T>
|
|
||||||
*/
|
|
||||||
default <T> List<T> queryFeatureClazzList(LambdaQueryChainWrapper<E> wrapper, Class<T> clazz) {
|
|
||||||
List<E> list = wrapper.list();
|
|
||||||
if (CollUtil.isEmpty(list)) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
return KitBeanUtil.copyToList(list, clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 条件查询分页列表, 并返回期望的对象
|
|
||||||
*
|
|
||||||
* @param pageable 分页参数
|
|
||||||
* @param wrapper LambdaQueryWrapper<E> wrapper
|
* @param wrapper LambdaQueryWrapper<E> wrapper
|
||||||
* @param clazz 期望的对象类型
|
* @param clazz 期望的对象类型
|
||||||
* @return IPage<T>
|
|
||||||
*/
|
|
||||||
default <T> KitPageResult<T> queryFeatureClazzPage(Pageable pageable, LambdaQueryWrapper<E> wrapper,
|
|
||||||
Class<T> clazz) {
|
|
||||||
int pageNumber = pageable.getPageNumber();
|
|
||||||
int pageSize = pageable.getPageSize();
|
|
||||||
pageNumber = pageNumber <= 0 ? 1 : pageNumber;
|
|
||||||
pageSize = pageSize <= 0 ? 10 : pageSize;
|
|
||||||
IPage<E> pageInfo = this.selectPage(new Page<>(pageNumber, pageSize), wrapper);
|
|
||||||
if (CollUtil.isEmpty(pageInfo.getRecords())) {
|
|
||||||
return new KitPageResult<>(new ArrayList<>(), pageInfo.getTotal());
|
|
||||||
}
|
|
||||||
return new KitPageResult<>(KitBeanUtil.copyToList(pageInfo.getRecords(), clazz), pageInfo.getTotal());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 条件查询分页列表, 并返回期望的对象
|
|
||||||
*
|
|
||||||
* @param pageable 分页参数
|
* @param pageable 分页参数
|
||||||
* @param wrapper LambdaQueryWrapper<E> wrapper
|
|
||||||
* @param clazz 期望的对象类型
|
|
||||||
* @return IPage<T>
|
* @return IPage<T>
|
||||||
*/
|
*/
|
||||||
default <T> KitPageResult<T> queryFeatureClazzPage(Pageable pageable, LambdaQueryChainWrapper<E> wrapper,
|
default <T> KitPageResult<T> searchFeatureClazz(LambdaQueryWrapper<E> wrapper, Class<T> clazz, Page<E> pageable) {
|
||||||
Class<T> clazz) {
|
IPage<E> pageInfo = this.selectPage(pageable, wrapper);
|
||||||
int pageNumber = pageable.getPageNumber();
|
|
||||||
int pageSize = pageable.getPageSize();
|
|
||||||
pageNumber = pageNumber <= 0 ? 1 : pageNumber;
|
|
||||||
pageSize = pageSize <= 0 ? 10 : pageSize;
|
|
||||||
IPage<E> pageInfo = wrapper.page(new Page<>(pageNumber, pageSize));
|
|
||||||
if (CollUtil.isEmpty(pageInfo.getRecords())) {
|
|
||||||
return KitPageUtil.emptyData();
|
|
||||||
}
|
|
||||||
return new KitPageResult<>(KitBeanUtil.copyToList(pageInfo.getRecords(), clazz), pageInfo.getTotal());
|
return new KitPageResult<>(KitBeanUtil.copyToList(pageInfo.getRecords(), clazz), pageInfo.getTotal());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取目标类
|
|
||||||
*
|
|
||||||
* @param wrapper LambdaQueryChainWrapper<E> wrapper
|
|
||||||
* @param clazz 期望的对象类型
|
|
||||||
* @return T
|
|
||||||
*/
|
|
||||||
default <T> T getFeatureClazz(LambdaQueryChainWrapper<E> wrapper, Class<T> clazz) {
|
|
||||||
E e = this.selectOne(wrapper);
|
|
||||||
if (e == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return KitBeanUtil.copyToClass(e, clazz);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+90
-22
@@ -20,6 +20,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -32,40 +33,107 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public interface KitMpService<T> extends IService<T> {
|
public interface KitMpService<T> extends IService<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存数据(已测试)
|
||||||
|
*
|
||||||
|
* @param resources 非Tb对象
|
||||||
|
* @return 影响的行数
|
||||||
|
*/
|
||||||
<G> int saveFeatureClazz(G resources);
|
<G> int saveFeatureClazz(G resources);
|
||||||
|
|
||||||
<G> int saveFeatureClazzList(List<G> resources);
|
/**
|
||||||
|
* 批量保存数据(已测试)
|
||||||
|
*
|
||||||
|
* @param resources 非Tb对象集合
|
||||||
|
* @return 影响的行数
|
||||||
|
*/
|
||||||
|
<G> int batchSaveFeatureClazz(List<G> resources);
|
||||||
|
|
||||||
<G> int modifyFeatureClazzById(G resources);
|
/**
|
||||||
|
* 根据id更新数据(已测试)
|
||||||
|
*
|
||||||
|
* @param resources 非Tb对象
|
||||||
|
* @return 影响的行数
|
||||||
|
*/
|
||||||
|
<G> int updateFeatureClazzById(G resources);
|
||||||
|
|
||||||
<G> boolean modifyFeatureClazzListById(Collection<G> resources, int batchSize);
|
/**
|
||||||
|
* 根据id更新数据(已测试)
|
||||||
|
*
|
||||||
|
* @param resources 非Tb对象
|
||||||
|
* @return 影响的行数
|
||||||
|
*/
|
||||||
|
<G> boolean batchUpdateFeatureClazzById(Collection<G> resources, int batchSize);
|
||||||
|
|
||||||
<G> G queryFeatureClazzById(Serializable id, Class<G> targetClazz);
|
/**
|
||||||
|
* 通过id查询(已测试)
|
||||||
|
*
|
||||||
|
* @param id 数据id
|
||||||
|
* @param targetClazz 目标类型
|
||||||
|
*/
|
||||||
|
<G> G getFeatureClazzById(Serializable id, Class<G> targetClazz);
|
||||||
|
|
||||||
T queryClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn);
|
/**
|
||||||
|
* 根据条件查询一条数据(已测试)
|
||||||
|
*
|
||||||
|
* @param wrapper 查询条件
|
||||||
|
* @param orderColumn 排序字段
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
T getClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn);
|
||||||
|
|
||||||
<G> G queryFeatureClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn, Class<G> clazz);
|
/**
|
||||||
|
* 根据条件查询一条数据(已测试)
|
||||||
|
*
|
||||||
|
* @param wrapper 查询条件
|
||||||
|
* @param orderColumn 排序字段
|
||||||
|
* @param targetClazz 目标类型
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
<G> G getFeatureClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn, Class<G> targetClazz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id集合查询目标数据(已测试)
|
||||||
|
*
|
||||||
|
* @param ids id集合
|
||||||
|
* @param targetClazz 目标类型
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
<G> List<G> queryFeatureClazzListByIds(List<Serializable> ids, Class<G> targetClazz);
|
<G> List<G> queryFeatureClazzListByIds(List<Serializable> ids, Class<G> targetClazz);
|
||||||
|
|
||||||
<Q> List<T> queryClazzListByArgs(Q args);
|
/**
|
||||||
|
* 根据条件查询目标数据(已测试)
|
||||||
List<T> queryClazzListByArgs(LambdaQueryWrapper<T> wrapper);
|
*
|
||||||
|
* @param wrapper 查询条件
|
||||||
<G, Q> List<G> queryFeatureClazzListByArgs(Q args, Class<G> targetClazz);
|
* @param targetClazz 目标类型
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
<G> List<G> queryFeatureClazzListByArgs(LambdaQueryWrapper<T> wrapper, Class<G> targetClazz);
|
<G> List<G> queryFeatureClazzListByArgs(LambdaQueryWrapper<T> wrapper, Class<G> targetClazz);
|
||||||
|
|
||||||
KitPageResult<T> queryClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable);
|
/**
|
||||||
|
* 根据条件分页查询数据(已测试)
|
||||||
|
*
|
||||||
|
* @param wrapper 查询条件
|
||||||
|
* @param pageable 分页参数
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
KitPageResult<T> searchClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable);
|
||||||
|
|
||||||
<G> KitPageResult<G> queryFeatureClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable,
|
/**
|
||||||
Class<G> targetClazz);
|
* 根据条件分页查询目标数据(已测试)
|
||||||
|
*
|
||||||
|
* @param wrapper 查询条件
|
||||||
|
* @param pageable 分页参数
|
||||||
|
* @param targetClazz 目标类型
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
<G> KitPageResult<G> searchFeatureClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable, Class<G> targetClazz);
|
||||||
|
|
||||||
<G, Q> KitPageResult<G> queryFeatureClazzPageByArgs(Q args, IPage<T> pageable, Class<G> targetClazz);
|
/**
|
||||||
|
* 根据条件更新数据(已测试)
|
||||||
<Q> KitPageResult<T> queryClazzPageByArgs(Q args, IPage<T> pageable);
|
*
|
||||||
|
* @param wrapper 查询条件与值
|
||||||
int modifyClazzByArgs(LambdaQueryWrapper<T> wrapper, T entity);
|
* @return 影响的行数
|
||||||
|
*/
|
||||||
int removeClazzByArgs(LambdaQueryWrapper<T> wrapper);
|
int updateClazzByArgs(LambdaQueryWrapper<T> wrapper);
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-172
@@ -15,21 +15,18 @@
|
|||||||
*/
|
*/
|
||||||
package cn.odboy.framework.mybatisplus.core.interfaces;
|
package cn.odboy.framework.mybatisplus.core.interfaces;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
|
||||||
import cn.odboy.base.KitPageResult;
|
import cn.odboy.base.KitPageResult;
|
||||||
import cn.odboy.framework.exception.BadRequestException;
|
|
||||||
import cn.odboy.framework.mybatisplus.core.KitMpQUtil;
|
|
||||||
import cn.odboy.util.KitBeanUtil;
|
import cn.odboy.util.KitBeanUtil;
|
||||||
|
import cn.odboy.util.KitValidUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.core.enums.SqlMethod;
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.apache.ibatis.binding.MapperMethod;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 公共抽象Mapper接口类
|
* 公共抽象Mapper接口类
|
||||||
@@ -39,147 +36,59 @@ import org.apache.ibatis.binding.MapperMethod;
|
|||||||
*/
|
*/
|
||||||
public class KitMpServiceImpl<M extends KitMpMapper<T>, T> extends ServiceImpl<M, T> {
|
public class KitMpServiceImpl<M extends KitMpMapper<T>, T> extends ServiceImpl<M, T> {
|
||||||
|
|
||||||
/**
|
|
||||||
* 快速插入
|
|
||||||
*
|
|
||||||
* @param resources /
|
|
||||||
*/
|
|
||||||
public <G> int saveFeatureClazz(G resources) {
|
public <G> int saveFeatureClazz(G resources) {
|
||||||
return getBaseMapper().insert(KitBeanUtil.copyToClass(resources, this.getEntityClass()));
|
return getBaseMapper().insert(KitBeanUtil.copyToClass(resources, this.getEntityClass()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public <G> int batchSaveFeatureClazz(List<G> resources) {
|
||||||
* 快速批量插入
|
return getBaseMapper().insert(KitBeanUtil.copyToList(resources, this.getEntityClass())).size();
|
||||||
*
|
|
||||||
* @param resources /
|
|
||||||
*/
|
|
||||||
public <G> int saveFeatureClazzList(List<G> resources) {
|
|
||||||
return getBaseMapper().insertBatchSomeColumn(KitBeanUtil.copyToList(resources, this.getEntityClass()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public <G> int updateFeatureClazzById(G resources) {
|
||||||
* 快速更新
|
|
||||||
*
|
|
||||||
* @param resources /
|
|
||||||
*/
|
|
||||||
public <G> int modifyFeatureClazzById(G resources) {
|
|
||||||
return getBaseMapper().updateById(KitBeanUtil.copyToClass(resources, this.getEntityClass()));
|
return getBaseMapper().updateById(KitBeanUtil.copyToClass(resources, this.getEntityClass()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public <G> int batchUpdateFeatureClazzById(Collection<G> resources, int batchSize) {
|
||||||
* 快速批量更新
|
return getBaseMapper().updateById(KitBeanUtil.copyToList(resources, this.getEntityClass()), batchSize).size();
|
||||||
*
|
// String sqlStatement = this.getSqlStatement(SqlMethod.UPDATE_BY_ID);
|
||||||
* @param resources /
|
// return this.executeBatch(
|
||||||
* @param batchSize 每批数量
|
// resources, batchSize, (sqlSession, entity) -> {
|
||||||
*/
|
// MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
|
||||||
public <G> boolean modifyFeatureClazzListById(Collection<G> resources, int batchSize) {
|
// param.put("et", KitBeanUtil.copyToClass(entity, this.getEntityClass()));
|
||||||
String sqlStatement = this.getSqlStatement(SqlMethod.UPDATE_BY_ID);
|
// sqlSession.update(sqlStatement, param);
|
||||||
return this.executeBatch(resources, batchSize, (sqlSession, entity) -> {
|
// }
|
||||||
MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
|
// );
|
||||||
param.put("et", KitBeanUtil.copyToClass(entity, this.getEntityClass()));
|
|
||||||
sqlSession.update(sqlStatement, param);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public <G> G getFeatureClazzById(Serializable id, Class<G> targetClazz) {
|
||||||
* 通过id查询
|
return KitBeanUtil.copyToClass(this.getById(id), targetClazz);
|
||||||
*
|
|
||||||
* @param id /
|
|
||||||
* @param targetClazz 目标类型
|
|
||||||
*/
|
|
||||||
public <G> G queryFeatureClazzById(Serializable id, Class<G> targetClazz) {
|
|
||||||
T entity = this.getById(id);
|
|
||||||
return entity == null ? null : KitBeanUtil.copyToClass(entity, targetClazz);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public T getClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn) {
|
||||||
* 查一条数据
|
KitValidUtil.isNull(orderColumn, "排序列", "orderColumn");
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
*/
|
|
||||||
public T queryClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn) {
|
|
||||||
if (orderColumn == null) {
|
|
||||||
throw new BadRequestException("参数orderColumn(排序的列)必填");
|
|
||||||
}
|
|
||||||
if (wrapper == null) {
|
if (wrapper == null) {
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
}
|
}
|
||||||
wrapper.last("limit 1");
|
|
||||||
wrapper.orderByDesc(orderColumn);
|
wrapper.orderByDesc(orderColumn);
|
||||||
List<T> ts = baseMapper.selectList(wrapper);
|
wrapper.last("limit 1");
|
||||||
if (ts.isEmpty()) {
|
return baseMapper.selectOne(wrapper);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return ts.stream().findFirst().get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <G> G queryFeatureClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn, Class<G> clazz) {
|
public <G> G getFeatureClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn, Class<G> targetClazz) {
|
||||||
if (orderColumn == null) {
|
KitValidUtil.isNull(orderColumn, "排序列", "orderColumn");
|
||||||
throw new BadRequestException("参数orderColumn(排序的列)必填");
|
|
||||||
}
|
|
||||||
if (wrapper == null) {
|
if (wrapper == null) {
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
}
|
}
|
||||||
wrapper.last("limit 1");
|
|
||||||
wrapper.orderByDesc(orderColumn);
|
wrapper.orderByDesc(orderColumn);
|
||||||
List<T> ts = baseMapper.selectList(wrapper);
|
wrapper.last("limit 1");
|
||||||
if (ts.isEmpty()) {
|
return KitBeanUtil.copyToClass(baseMapper.selectOne(wrapper), targetClazz);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return KitBeanUtil.copyToClass(ts.stream().findFirst().get(), clazz);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 通过id集合查询
|
|
||||||
*
|
|
||||||
* @param ids /
|
|
||||||
* @param targetClazz 目标类型
|
|
||||||
*/
|
|
||||||
public <G> List<G> queryFeatureClazzListByIds(List<Serializable> ids, Class<G> targetClazz) {
|
public <G> List<G> queryFeatureClazzListByIds(List<Serializable> ids, Class<G> targetClazz) {
|
||||||
List<T> entitys = this.listByIds(ids);
|
List<T> entitys = this.listByIds(ids);
|
||||||
return entitys == null || entitys.isEmpty() ? CollUtil.newArrayList() : KitBeanUtil.copyToList(entitys, targetClazz);
|
return KitBeanUtil.copyToList(entitys, targetClazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 查有关的数据
|
|
||||||
*
|
|
||||||
* @param args 动态条件
|
|
||||||
*/
|
|
||||||
public <Q> List<T> queryClazzListByArgs(Q args) {
|
|
||||||
QueryWrapper<T> predicate = KitMpQUtil.build(args);
|
|
||||||
return baseMapper.selectList(predicate);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查有关的数据
|
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
*/
|
|
||||||
public List<T> queryClazzListByArgs(LambdaQueryWrapper<T> wrapper) {
|
|
||||||
if (wrapper == null) {
|
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
|
||||||
}
|
|
||||||
return baseMapper.selectList(wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查有关的数据并转成目标类型的数据
|
|
||||||
*
|
|
||||||
* @param args 动态条件
|
|
||||||
* @param targetClazz 目标类型
|
|
||||||
*/
|
|
||||||
public <G, Q> List<G> queryFeatureClazzListByArgs(Q args, Class<G> targetClazz) {
|
|
||||||
QueryWrapper<T> predicate = KitMpQUtil.build(args);
|
|
||||||
List<T> data = baseMapper.selectList(predicate);
|
|
||||||
return KitBeanUtil.copyToList(data, targetClazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查有关的数据并转换为目标类型
|
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
* @param targetClazz 目标类型
|
|
||||||
*/
|
|
||||||
public <G> List<G> queryFeatureClazzListByArgs(LambdaQueryWrapper<T> wrapper, Class<G> targetClazz) {
|
public <G> List<G> queryFeatureClazzListByArgs(LambdaQueryWrapper<T> wrapper, Class<G> targetClazz) {
|
||||||
if (wrapper == null) {
|
if (wrapper == null) {
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
@@ -187,13 +96,7 @@ public class KitMpServiceImpl<M extends KitMpMapper<T>, T> extends ServiceImpl<M
|
|||||||
return KitBeanUtil.copyToList(baseMapper.selectList(wrapper), targetClazz);
|
return KitBeanUtil.copyToList(baseMapper.selectList(wrapper), targetClazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public KitPageResult<T> searchClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable) {
|
||||||
* 查有关的分页数据
|
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
* @param pageable 分页参数
|
|
||||||
*/
|
|
||||||
public KitPageResult<T> queryClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable) {
|
|
||||||
if (wrapper == null) {
|
if (wrapper == null) {
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
}
|
}
|
||||||
@@ -201,58 +104,19 @@ public class KitMpServiceImpl<M extends KitMpMapper<T>, T> extends ServiceImpl<M
|
|||||||
return new KitPageResult<>(originPageData.getRecords(), originPageData.getTotal());
|
return new KitPageResult<>(originPageData.getRecords(), originPageData.getTotal());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public <G> KitPageResult<G> searchFeatureClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable, Class<G> targetClazz) {
|
||||||
* 查有关的分页数据
|
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
* @param pageable 分页参数
|
|
||||||
* @param targetClazz 目标类型
|
|
||||||
*/
|
|
||||||
public <G> KitPageResult<G> queryFeatureClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable,
|
|
||||||
Class<G> targetClazz) {
|
|
||||||
if (wrapper == null) {
|
if (wrapper == null) {
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
}
|
}
|
||||||
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
||||||
return new KitPageResult<>(KitBeanUtil.copyToList(originPageData.getRecords(), targetClazz),
|
return new KitPageResult<>(
|
||||||
originPageData.getTotal());
|
KitBeanUtil.copyToList(originPageData.getRecords(), targetClazz),
|
||||||
|
originPageData.getTotal()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <G, Q> KitPageResult<G> queryFeatureClazzPageByArgs(Q args, IPage<T> pageable, Class<G> targetClazz) {
|
public int updateClazzByArgs(LambdaUpdateWrapper<T> wrapper) {
|
||||||
QueryWrapper<T> wrapper = KitMpQUtil.build(args);
|
KitValidUtil.isNull(wrapper, "查询条件与值", "wrapper");
|
||||||
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
return baseMapper.update(null, wrapper);
|
||||||
return new KitPageResult<>(KitBeanUtil.copyToList(originPageData.getRecords(), targetClazz),
|
|
||||||
originPageData.getTotal());
|
|
||||||
}
|
|
||||||
|
|
||||||
public <Q> KitPageResult<T> queryClazzPageByArgs(Q args, IPage<T> pageable) {
|
|
||||||
QueryWrapper<T> wrapper = KitMpQUtil.build(args);
|
|
||||||
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
|
||||||
return new KitPageResult<>(originPageData.getRecords(), originPageData.getTotal());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 条件更新
|
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
* @param entity 更新内容
|
|
||||||
*/
|
|
||||||
public int modifyClazzByArgs(LambdaQueryWrapper<T> wrapper, T entity) {
|
|
||||||
if (wrapper == null) {
|
|
||||||
throw new BadRequestException("参数wrapper必填");
|
|
||||||
}
|
|
||||||
return baseMapper.update(entity, wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 条件删除
|
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
*/
|
|
||||||
public int removeClazzByArgs(LambdaQueryWrapper<T> wrapper) {
|
|
||||||
if (wrapper == null) {
|
|
||||||
throw new BadRequestException("参数wrapper必填");
|
|
||||||
}
|
|
||||||
return baseMapper.delete(wrapper);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-3
@@ -18,6 +18,7 @@ package cn.odboy.util;
|
|||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.bean.copier.CopyOptions;
|
import cn.hutool.core.bean.copier.CopyOptions;
|
||||||
import lombok.experimental.UtilityClass;
|
import lombok.experimental.UtilityClass;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -32,18 +33,26 @@ import java.util.List;
|
|||||||
public class KitBeanUtil {
|
public class KitBeanUtil {
|
||||||
|
|
||||||
public static <T> T copyToClass(Object source, Class<T> tClass) {
|
public static <T> T copyToClass(Object source, Class<T> tClass) {
|
||||||
|
if (source == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return BeanUtil.copyProperties(source, tClass);
|
return BeanUtil.copyProperties(source, tClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void copyToTarget(Object source, Object target) {
|
public static void copyToTarget(Object source, Object target) {
|
||||||
|
if (source == null || target == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
BeanUtil.copyProperties(source, target, CopyOptions.create().setIgnoreNullValue(true));
|
BeanUtil.copyProperties(source, target, CopyOptions.create().setIgnoreNullValue(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType) {
|
public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType) {
|
||||||
List<T> ts = BeanUtil.copyToList(collection, targetType);
|
if (collection == null) {
|
||||||
if (ts == null) {
|
|
||||||
return new ArrayList<>(0);
|
return new ArrayList<>(0);
|
||||||
}
|
}
|
||||||
return ts;
|
if (collection.isEmpty()) {
|
||||||
|
return new ArrayList<>(0);
|
||||||
|
}
|
||||||
|
return BeanUtil.copyToList(collection, targetType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-77
@@ -17,6 +17,7 @@ package cn.odboy.framework.mybatisplus.core.interfaces;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.odboy.base.KitPageResult;
|
import cn.odboy.base.KitPageResult;
|
||||||
import cn.odboy.util.KitBeanUtil;
|
import cn.odboy.util.KitBeanUtil;
|
||||||
import cn.odboy.util.KitPageUtil;
|
import cn.odboy.util.KitPageUtil;
|
||||||
@@ -25,12 +26,14 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
|
||||||
@@ -43,7 +46,7 @@ import org.springframework.data.domain.Pageable;
|
|||||||
public interface KitMpMapper<E> extends BaseMapper<E> {
|
public interface KitMpMapper<E> extends BaseMapper<E> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量插入
|
* 批量插入(已测试)
|
||||||
*
|
*
|
||||||
* @param entityList List<E>
|
* @param entityList List<E>
|
||||||
* @return 影响的行数
|
* @return 影响的行数
|
||||||
@@ -51,7 +54,7 @@ public interface KitMpMapper<E> extends BaseMapper<E> {
|
|||||||
int insertBatchSomeColumn(List<E> entityList);
|
int insertBatchSomeColumn(List<E> entityList);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据id全量更新
|
* 根据id全量更新,为空的字段也会被更新(已测试)
|
||||||
*
|
*
|
||||||
* @param entity E
|
* @param entity E
|
||||||
* @return 影响的行数
|
* @return 影响的行数
|
||||||
@@ -59,25 +62,24 @@ public interface KitMpMapper<E> extends BaseMapper<E> {
|
|||||||
int alwaysUpdateSomeColumnById(@Param("et") E entity);
|
int alwaysUpdateSomeColumnById(@Param("et") E entity);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取字典
|
* 获取字典(已测试)
|
||||||
*
|
*
|
||||||
* @param wrapper LambdaQueryWrapper<E>
|
* @param wrapper LambdaQueryWrapper<E>
|
||||||
* @param keyName 键对应的列名称
|
* @param keyName 键对应的列名称(下划线会自动转驼峰)
|
||||||
* @param valueName 值对应的列名称
|
* @param valueName 值对应的列名称
|
||||||
* @return Map<String, Object>
|
* @return Map<String, Object>
|
||||||
*/
|
*/
|
||||||
default Map<String, Object> getMapWithKv(LambdaQueryWrapper<E> wrapper, String keyName, String valueName) {
|
default Map<String, Object> getMapWithKv(LambdaQueryWrapper<E> wrapper, String keyName, String valueName) {
|
||||||
Map<String, Object> result = new ConcurrentHashMap<>(1);
|
Map<String, Object> result = new HashMap<>(1);
|
||||||
List<E> list = this.selectList(wrapper);
|
List<E> list = this.selectList(wrapper);
|
||||||
if (CollUtil.isEmpty(list)) {
|
if (CollUtil.isEmpty(list)) {
|
||||||
return new HashMap<>(3);
|
return new HashMap<>(1);
|
||||||
}
|
}
|
||||||
E tempObj = list.stream().findFirst().orElse(null);
|
E tempObj = list.stream().findFirst().orElse(null);
|
||||||
if (tempObj == null) {
|
if (tempObj == null) {
|
||||||
return new HashMap<>(3);
|
return new HashMap<>(1);
|
||||||
}
|
}
|
||||||
Field[] fields = ReflectUtil.getFields(tempObj.getClass());
|
Field[] fields = ReflectUtil.getFields(tempObj.getClass());
|
||||||
for (E e : list) {
|
|
||||||
Field keyField = null;
|
Field keyField = null;
|
||||||
Field valueField = null;
|
Field valueField = null;
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
@@ -89,99 +91,40 @@ public interface KitMpMapper<E> extends BaseMapper<E> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (E e : list) {
|
||||||
String fieldKey = String.valueOf(ReflectUtil.getFieldValue(e, keyField));
|
String fieldKey = String.valueOf(ReflectUtil.getFieldValue(e, keyField));
|
||||||
Object fieldValue = ReflectUtil.getFieldValue(e, valueField);
|
|
||||||
if (fieldKey != null) {
|
if (fieldKey != null) {
|
||||||
result.put(fieldKey, fieldValue);
|
// 下划线转驼峰
|
||||||
|
String camelFieldKey = StrUtil.toCamelCase(fieldKey);
|
||||||
|
Object fieldValue = ReflectUtil.getFieldValue(e, valueField);
|
||||||
|
result.putIfAbsent(camelFieldKey, fieldValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 条件查询列表, 并返回期望的对象
|
* 条件查询列表, 并返回期望的对象(已测试)
|
||||||
*
|
*
|
||||||
* @param wrapper LambdaQueryWrapper<E>
|
* @param wrapper LambdaQueryWrapper<E>
|
||||||
* @param clazz 期望的对象类型
|
* @param clazz 期望的对象类型
|
||||||
* @return List<T>
|
* @return List<T>
|
||||||
*/
|
*/
|
||||||
default <T> List<T> queryFeatureClazzList(LambdaQueryWrapper<E> wrapper, Class<T> clazz) {
|
default <T> List<T> queryFeatureClazz(LambdaQueryWrapper<E> wrapper, Class<T> clazz) {
|
||||||
List<E> list = this.selectList(wrapper);
|
List<E> list = this.selectList(wrapper);
|
||||||
if (CollUtil.isEmpty(list)) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
return KitBeanUtil.copyToList(list, clazz);
|
return KitBeanUtil.copyToList(list, clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 条件查询列表, 并返回期望的对象
|
* 条件查询分页列表, 并返回期望的对象(已测试)
|
||||||
*
|
*
|
||||||
* @param wrapper LambdaQueryWrapper<E>
|
|
||||||
* @param clazz 期望的对象类型
|
|
||||||
* @return List<T>
|
|
||||||
*/
|
|
||||||
default <T> List<T> queryFeatureClazzList(LambdaQueryChainWrapper<E> wrapper, Class<T> clazz) {
|
|
||||||
List<E> list = wrapper.list();
|
|
||||||
if (CollUtil.isEmpty(list)) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
return KitBeanUtil.copyToList(list, clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 条件查询分页列表, 并返回期望的对象
|
|
||||||
*
|
|
||||||
* @param pageable 分页参数
|
|
||||||
* @param wrapper LambdaQueryWrapper<E> wrapper
|
* @param wrapper LambdaQueryWrapper<E> wrapper
|
||||||
* @param clazz 期望的对象类型
|
* @param clazz 期望的对象类型
|
||||||
* @return IPage<T>
|
|
||||||
*/
|
|
||||||
default <T> KitPageResult<T> queryFeatureClazzPage(Pageable pageable, LambdaQueryWrapper<E> wrapper,
|
|
||||||
Class<T> clazz) {
|
|
||||||
int pageNumber = pageable.getPageNumber();
|
|
||||||
int pageSize = pageable.getPageSize();
|
|
||||||
pageNumber = pageNumber <= 0 ? 1 : pageNumber;
|
|
||||||
pageSize = pageSize <= 0 ? 10 : pageSize;
|
|
||||||
IPage<E> pageInfo = this.selectPage(new Page<>(pageNumber, pageSize), wrapper);
|
|
||||||
if (CollUtil.isEmpty(pageInfo.getRecords())) {
|
|
||||||
return new KitPageResult<>(new ArrayList<>(), pageInfo.getTotal());
|
|
||||||
}
|
|
||||||
return new KitPageResult<>(KitBeanUtil.copyToList(pageInfo.getRecords(), clazz), pageInfo.getTotal());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 条件查询分页列表, 并返回期望的对象
|
|
||||||
*
|
|
||||||
* @param pageable 分页参数
|
* @param pageable 分页参数
|
||||||
* @param wrapper LambdaQueryWrapper<E> wrapper
|
|
||||||
* @param clazz 期望的对象类型
|
|
||||||
* @return IPage<T>
|
* @return IPage<T>
|
||||||
*/
|
*/
|
||||||
default <T> KitPageResult<T> queryFeatureClazzPage(Pageable pageable, LambdaQueryChainWrapper<E> wrapper,
|
default <T> KitPageResult<T> searchFeatureClazz(LambdaQueryWrapper<E> wrapper, Class<T> clazz, Page<E> pageable) {
|
||||||
Class<T> clazz) {
|
IPage<E> pageInfo = this.selectPage(pageable, wrapper);
|
||||||
int pageNumber = pageable.getPageNumber();
|
|
||||||
int pageSize = pageable.getPageSize();
|
|
||||||
pageNumber = pageNumber <= 0 ? 1 : pageNumber;
|
|
||||||
pageSize = pageSize <= 0 ? 10 : pageSize;
|
|
||||||
IPage<E> pageInfo = wrapper.page(new Page<>(pageNumber, pageSize));
|
|
||||||
if (CollUtil.isEmpty(pageInfo.getRecords())) {
|
|
||||||
return KitPageUtil.emptyData();
|
|
||||||
}
|
|
||||||
return new KitPageResult<>(KitBeanUtil.copyToList(pageInfo.getRecords(), clazz), pageInfo.getTotal());
|
return new KitPageResult<>(KitBeanUtil.copyToList(pageInfo.getRecords(), clazz), pageInfo.getTotal());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取目标类
|
|
||||||
*
|
|
||||||
* @param wrapper LambdaQueryChainWrapper<E> wrapper
|
|
||||||
* @param clazz 期望的对象类型
|
|
||||||
* @return T
|
|
||||||
*/
|
|
||||||
default <T> T getFeatureClazz(LambdaQueryChainWrapper<E> wrapper, Class<T> clazz) {
|
|
||||||
E e = this.selectOne(wrapper);
|
|
||||||
if (e == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return KitBeanUtil.copyToClass(e, clazz);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+90
-22
@@ -20,6 +20,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -32,40 +33,107 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public interface KitMpService<T> extends IService<T> {
|
public interface KitMpService<T> extends IService<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存数据(已测试)
|
||||||
|
*
|
||||||
|
* @param resources 非Tb对象
|
||||||
|
* @return 影响的行数
|
||||||
|
*/
|
||||||
<G> int saveFeatureClazz(G resources);
|
<G> int saveFeatureClazz(G resources);
|
||||||
|
|
||||||
<G> int saveFeatureClazzList(List<G> resources);
|
/**
|
||||||
|
* 批量保存数据(已测试)
|
||||||
|
*
|
||||||
|
* @param resources 非Tb对象集合
|
||||||
|
* @return 影响的行数
|
||||||
|
*/
|
||||||
|
<G> int batchSaveFeatureClazz(List<G> resources);
|
||||||
|
|
||||||
<G> int modifyFeatureClazzById(G resources);
|
/**
|
||||||
|
* 根据id更新数据(已测试)
|
||||||
|
*
|
||||||
|
* @param resources 非Tb对象
|
||||||
|
* @return 影响的行数
|
||||||
|
*/
|
||||||
|
<G> int updateFeatureClazzById(G resources);
|
||||||
|
|
||||||
<G> boolean modifyFeatureClazzListById(Collection<G> resources, int batchSize);
|
/**
|
||||||
|
* 根据id更新数据(已测试)
|
||||||
|
*
|
||||||
|
* @param resources 非Tb对象
|
||||||
|
* @return 影响的行数
|
||||||
|
*/
|
||||||
|
<G> boolean batchUpdateFeatureClazzById(Collection<G> resources, int batchSize);
|
||||||
|
|
||||||
<G> G queryFeatureClazzById(Serializable id, Class<G> targetClazz);
|
/**
|
||||||
|
* 通过id查询(已测试)
|
||||||
|
*
|
||||||
|
* @param id 数据id
|
||||||
|
* @param targetClazz 目标类型
|
||||||
|
*/
|
||||||
|
<G> G getFeatureClazzById(Serializable id, Class<G> targetClazz);
|
||||||
|
|
||||||
T queryClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn);
|
/**
|
||||||
|
* 根据条件查询一条数据(已测试)
|
||||||
|
*
|
||||||
|
* @param wrapper 查询条件
|
||||||
|
* @param orderColumn 排序字段
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
T getClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn);
|
||||||
|
|
||||||
<G> G queryFeatureClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn, Class<G> clazz);
|
/**
|
||||||
|
* 根据条件查询一条数据(已测试)
|
||||||
|
*
|
||||||
|
* @param wrapper 查询条件
|
||||||
|
* @param orderColumn 排序字段
|
||||||
|
* @param targetClazz 目标类型
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
<G> G getFeatureClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn, Class<G> targetClazz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id集合查询目标数据(已测试)
|
||||||
|
*
|
||||||
|
* @param ids id集合
|
||||||
|
* @param targetClazz 目标类型
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
<G> List<G> queryFeatureClazzListByIds(List<Serializable> ids, Class<G> targetClazz);
|
<G> List<G> queryFeatureClazzListByIds(List<Serializable> ids, Class<G> targetClazz);
|
||||||
|
|
||||||
<Q> List<T> queryClazzListByArgs(Q args);
|
/**
|
||||||
|
* 根据条件查询目标数据(已测试)
|
||||||
List<T> queryClazzListByArgs(LambdaQueryWrapper<T> wrapper);
|
*
|
||||||
|
* @param wrapper 查询条件
|
||||||
<G, Q> List<G> queryFeatureClazzListByArgs(Q args, Class<G> targetClazz);
|
* @param targetClazz 目标类型
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
<G> List<G> queryFeatureClazzListByArgs(LambdaQueryWrapper<T> wrapper, Class<G> targetClazz);
|
<G> List<G> queryFeatureClazzListByArgs(LambdaQueryWrapper<T> wrapper, Class<G> targetClazz);
|
||||||
|
|
||||||
KitPageResult<T> queryClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable);
|
/**
|
||||||
|
* 根据条件分页查询数据(已测试)
|
||||||
|
*
|
||||||
|
* @param wrapper 查询条件
|
||||||
|
* @param pageable 分页参数
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
KitPageResult<T> searchClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable);
|
||||||
|
|
||||||
<G> KitPageResult<G> queryFeatureClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable,
|
/**
|
||||||
Class<G> targetClazz);
|
* 根据条件分页查询目标数据(已测试)
|
||||||
|
*
|
||||||
|
* @param wrapper 查询条件
|
||||||
|
* @param pageable 分页参数
|
||||||
|
* @param targetClazz 目标类型
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
<G> KitPageResult<G> searchFeatureClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable, Class<G> targetClazz);
|
||||||
|
|
||||||
<G, Q> KitPageResult<G> queryFeatureClazzPageByArgs(Q args, IPage<T> pageable, Class<G> targetClazz);
|
/**
|
||||||
|
* 根据条件更新数据(已测试)
|
||||||
<Q> KitPageResult<T> queryClazzPageByArgs(Q args, IPage<T> pageable);
|
*
|
||||||
|
* @param wrapper 查询条件与值
|
||||||
int modifyClazzByArgs(LambdaQueryWrapper<T> wrapper, T entity);
|
* @return 影响的行数
|
||||||
|
*/
|
||||||
int removeClazzByArgs(LambdaQueryWrapper<T> wrapper);
|
int updateClazzByArgs(LambdaQueryWrapper<T> wrapper);
|
||||||
}
|
}
|
||||||
|
|||||||
+37
-166
@@ -20,15 +20,19 @@ import cn.odboy.base.KitPageResult;
|
|||||||
import cn.odboy.framework.exception.BadRequestException;
|
import cn.odboy.framework.exception.BadRequestException;
|
||||||
import cn.odboy.framework.mybatisplus.core.KitMpQUtil;
|
import cn.odboy.framework.mybatisplus.core.KitMpQUtil;
|
||||||
import cn.odboy.util.KitBeanUtil;
|
import cn.odboy.util.KitBeanUtil;
|
||||||
|
import cn.odboy.util.KitValidUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.core.enums.SqlMethod;
|
import com.baomidou.mybatisplus.core.enums.SqlMethod;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.ibatis.binding.MapperMethod;
|
import org.apache.ibatis.binding.MapperMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,147 +43,59 @@ import org.apache.ibatis.binding.MapperMethod;
|
|||||||
*/
|
*/
|
||||||
public class KitMpServiceImpl<M extends KitMpMapper<T>, T> extends ServiceImpl<M, T> {
|
public class KitMpServiceImpl<M extends KitMpMapper<T>, T> extends ServiceImpl<M, T> {
|
||||||
|
|
||||||
/**
|
|
||||||
* 快速插入
|
|
||||||
*
|
|
||||||
* @param resources /
|
|
||||||
*/
|
|
||||||
public <G> int saveFeatureClazz(G resources) {
|
public <G> int saveFeatureClazz(G resources) {
|
||||||
return getBaseMapper().insert(KitBeanUtil.copyToClass(resources, this.getEntityClass()));
|
return getBaseMapper().insert(KitBeanUtil.copyToClass(resources, this.getEntityClass()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public <G> int batchSaveFeatureClazz(List<G> resources) {
|
||||||
* 快速批量插入
|
return getBaseMapper().insert(KitBeanUtil.copyToList(resources, this.getEntityClass())).size();
|
||||||
*
|
|
||||||
* @param resources /
|
|
||||||
*/
|
|
||||||
public <G> int saveFeatureClazzList(List<G> resources) {
|
|
||||||
return getBaseMapper().insertBatchSomeColumn(KitBeanUtil.copyToList(resources, this.getEntityClass()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public <G> int updateFeatureClazzById(G resources) {
|
||||||
* 快速更新
|
|
||||||
*
|
|
||||||
* @param resources /
|
|
||||||
*/
|
|
||||||
public <G> int modifyFeatureClazzById(G resources) {
|
|
||||||
return getBaseMapper().updateById(KitBeanUtil.copyToClass(resources, this.getEntityClass()));
|
return getBaseMapper().updateById(KitBeanUtil.copyToClass(resources, this.getEntityClass()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public <G> int batchUpdateFeatureClazzById(Collection<G> resources, int batchSize) {
|
||||||
* 快速批量更新
|
return getBaseMapper().updateById(KitBeanUtil.copyToList(resources, this.getEntityClass()), batchSize).size();
|
||||||
*
|
// String sqlStatement = this.getSqlStatement(SqlMethod.UPDATE_BY_ID);
|
||||||
* @param resources /
|
// return this.executeBatch(
|
||||||
* @param batchSize 每批数量
|
// resources, batchSize, (sqlSession, entity) -> {
|
||||||
*/
|
// MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
|
||||||
public <G> boolean modifyFeatureClazzListById(Collection<G> resources, int batchSize) {
|
// param.put("et", KitBeanUtil.copyToClass(entity, this.getEntityClass()));
|
||||||
String sqlStatement = this.getSqlStatement(SqlMethod.UPDATE_BY_ID);
|
// sqlSession.update(sqlStatement, param);
|
||||||
return this.executeBatch(resources, batchSize, (sqlSession, entity) -> {
|
// }
|
||||||
MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
|
// );
|
||||||
param.put("et", KitBeanUtil.copyToClass(entity, this.getEntityClass()));
|
|
||||||
sqlSession.update(sqlStatement, param);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public <G> G getFeatureClazzById(Serializable id, Class<G> targetClazz) {
|
||||||
* 通过id查询
|
return KitBeanUtil.copyToClass(this.getById(id), targetClazz);
|
||||||
*
|
|
||||||
* @param id /
|
|
||||||
* @param targetClazz 目标类型
|
|
||||||
*/
|
|
||||||
public <G> G queryFeatureClazzById(Serializable id, Class<G> targetClazz) {
|
|
||||||
T entity = this.getById(id);
|
|
||||||
return entity == null ? null : KitBeanUtil.copyToClass(entity, targetClazz);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public T getClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn) {
|
||||||
* 查一条数据
|
KitValidUtil.isNull(orderColumn, "排序列", "orderColumn");
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
*/
|
|
||||||
public T queryClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn) {
|
|
||||||
if (orderColumn == null) {
|
|
||||||
throw new BadRequestException("参数orderColumn(排序的列)必填");
|
|
||||||
}
|
|
||||||
if (wrapper == null) {
|
if (wrapper == null) {
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
}
|
}
|
||||||
wrapper.last("limit 1");
|
|
||||||
wrapper.orderByDesc(orderColumn);
|
wrapper.orderByDesc(orderColumn);
|
||||||
List<T> ts = baseMapper.selectList(wrapper);
|
wrapper.last("limit 1");
|
||||||
if (ts.isEmpty()) {
|
return baseMapper.selectOne(wrapper);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return ts.stream().findFirst().get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <G> G queryFeatureClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn, Class<G> clazz) {
|
public <G> G getFeatureClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn, Class<G> targetClazz) {
|
||||||
if (orderColumn == null) {
|
KitValidUtil.isNull(orderColumn, "排序列", "orderColumn");
|
||||||
throw new BadRequestException("参数orderColumn(排序的列)必填");
|
|
||||||
}
|
|
||||||
if (wrapper == null) {
|
if (wrapper == null) {
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
}
|
}
|
||||||
wrapper.last("limit 1");
|
|
||||||
wrapper.orderByDesc(orderColumn);
|
wrapper.orderByDesc(orderColumn);
|
||||||
List<T> ts = baseMapper.selectList(wrapper);
|
wrapper.last("limit 1");
|
||||||
if (ts.isEmpty()) {
|
return KitBeanUtil.copyToClass(baseMapper.selectOne(wrapper), targetClazz);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return KitBeanUtil.copyToClass(ts.stream().findFirst().get(), clazz);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 通过id集合查询
|
|
||||||
*
|
|
||||||
* @param ids /
|
|
||||||
* @param targetClazz 目标类型
|
|
||||||
*/
|
|
||||||
public <G> List<G> queryFeatureClazzListByIds(List<Serializable> ids, Class<G> targetClazz) {
|
public <G> List<G> queryFeatureClazzListByIds(List<Serializable> ids, Class<G> targetClazz) {
|
||||||
List<T> entitys = this.listByIds(ids);
|
List<T> entitys = this.listByIds(ids);
|
||||||
return entitys == null || entitys.isEmpty() ? CollUtil.newArrayList() : KitBeanUtil.copyToList(entitys, targetClazz);
|
return KitBeanUtil.copyToList(entitys, targetClazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 查有关的数据
|
|
||||||
*
|
|
||||||
* @param args 动态条件
|
|
||||||
*/
|
|
||||||
public <Q> List<T> queryClazzListByArgs(Q args) {
|
|
||||||
QueryWrapper<T> predicate = KitMpQUtil.build(args);
|
|
||||||
return baseMapper.selectList(predicate);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查有关的数据
|
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
*/
|
|
||||||
public List<T> queryClazzListByArgs(LambdaQueryWrapper<T> wrapper) {
|
|
||||||
if (wrapper == null) {
|
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
|
||||||
}
|
|
||||||
return baseMapper.selectList(wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查有关的数据并转成目标类型的数据
|
|
||||||
*
|
|
||||||
* @param args 动态条件
|
|
||||||
* @param targetClazz 目标类型
|
|
||||||
*/
|
|
||||||
public <G, Q> List<G> queryFeatureClazzListByArgs(Q args, Class<G> targetClazz) {
|
|
||||||
QueryWrapper<T> predicate = KitMpQUtil.build(args);
|
|
||||||
List<T> data = baseMapper.selectList(predicate);
|
|
||||||
return KitBeanUtil.copyToList(data, targetClazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查有关的数据并转换为目标类型
|
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
* @param targetClazz 目标类型
|
|
||||||
*/
|
|
||||||
public <G> List<G> queryFeatureClazzListByArgs(LambdaQueryWrapper<T> wrapper, Class<G> targetClazz) {
|
public <G> List<G> queryFeatureClazzListByArgs(LambdaQueryWrapper<T> wrapper, Class<G> targetClazz) {
|
||||||
if (wrapper == null) {
|
if (wrapper == null) {
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
@@ -187,13 +103,7 @@ public class KitMpServiceImpl<M extends KitMpMapper<T>, T> extends ServiceImpl<M
|
|||||||
return KitBeanUtil.copyToList(baseMapper.selectList(wrapper), targetClazz);
|
return KitBeanUtil.copyToList(baseMapper.selectList(wrapper), targetClazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public KitPageResult<T> searchClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable) {
|
||||||
* 查有关的分页数据
|
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
* @param pageable 分页参数
|
|
||||||
*/
|
|
||||||
public KitPageResult<T> queryClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable) {
|
|
||||||
if (wrapper == null) {
|
if (wrapper == null) {
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
}
|
}
|
||||||
@@ -201,58 +111,19 @@ public class KitMpServiceImpl<M extends KitMpMapper<T>, T> extends ServiceImpl<M
|
|||||||
return new KitPageResult<>(originPageData.getRecords(), originPageData.getTotal());
|
return new KitPageResult<>(originPageData.getRecords(), originPageData.getTotal());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public <G> KitPageResult<G> searchFeatureClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable, Class<G> targetClazz) {
|
||||||
* 查有关的分页数据
|
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
* @param pageable 分页参数
|
|
||||||
* @param targetClazz 目标类型
|
|
||||||
*/
|
|
||||||
public <G> KitPageResult<G> queryFeatureClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable,
|
|
||||||
Class<G> targetClazz) {
|
|
||||||
if (wrapper == null) {
|
if (wrapper == null) {
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
}
|
}
|
||||||
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
||||||
return new KitPageResult<>(KitBeanUtil.copyToList(originPageData.getRecords(), targetClazz),
|
return new KitPageResult<>(
|
||||||
originPageData.getTotal());
|
KitBeanUtil.copyToList(originPageData.getRecords(), targetClazz),
|
||||||
|
originPageData.getTotal()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <G, Q> KitPageResult<G> queryFeatureClazzPageByArgs(Q args, IPage<T> pageable, Class<G> targetClazz) {
|
public int updateClazzByArgs(LambdaUpdateWrapper<T> wrapper) {
|
||||||
QueryWrapper<T> wrapper = KitMpQUtil.build(args);
|
KitValidUtil.isNull(wrapper, "查询条件与值", "wrapper");
|
||||||
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
return baseMapper.update(null, wrapper);
|
||||||
return new KitPageResult<>(KitBeanUtil.copyToList(originPageData.getRecords(), targetClazz),
|
|
||||||
originPageData.getTotal());
|
|
||||||
}
|
|
||||||
|
|
||||||
public <Q> KitPageResult<T> queryClazzPageByArgs(Q args, IPage<T> pageable) {
|
|
||||||
QueryWrapper<T> wrapper = KitMpQUtil.build(args);
|
|
||||||
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
|
||||||
return new KitPageResult<>(originPageData.getRecords(), originPageData.getTotal());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 条件更新
|
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
* @param entity 更新内容
|
|
||||||
*/
|
|
||||||
public int modifyClazzByArgs(LambdaQueryWrapper<T> wrapper, T entity) {
|
|
||||||
if (wrapper == null) {
|
|
||||||
throw new BadRequestException("参数wrapper必填");
|
|
||||||
}
|
|
||||||
return baseMapper.update(entity, wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 条件删除
|
|
||||||
*
|
|
||||||
* @param wrapper 制造条件
|
|
||||||
*/
|
|
||||||
public int removeClazzByArgs(LambdaQueryWrapper<T> wrapper) {
|
|
||||||
if (wrapper == null) {
|
|
||||||
throw new BadRequestException("参数wrapper必填");
|
|
||||||
}
|
|
||||||
return baseMapper.delete(wrapper);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,11 @@ package cn.odboy.util;
|
|||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.bean.copier.CopyOptions;
|
import cn.hutool.core.bean.copier.CopyOptions;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import lombok.experimental.UtilityClass;
|
import lombok.experimental.UtilityClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,18 +34,26 @@ import lombok.experimental.UtilityClass;
|
|||||||
public class KitBeanUtil {
|
public class KitBeanUtil {
|
||||||
|
|
||||||
public static <T> T copyToClass(Object source, Class<T> tClass) {
|
public static <T> T copyToClass(Object source, Class<T> tClass) {
|
||||||
|
if (source == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return BeanUtil.copyProperties(source, tClass);
|
return BeanUtil.copyProperties(source, tClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void copyToTarget(Object source, Object target) {
|
public static void copyToTarget(Object source, Object target) {
|
||||||
|
if (source == null || target == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
BeanUtil.copyProperties(source, target, CopyOptions.create().setIgnoreNullValue(true));
|
BeanUtil.copyProperties(source, target, CopyOptions.create().setIgnoreNullValue(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType) {
|
public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType) {
|
||||||
List<T> ts = BeanUtil.copyToList(collection, targetType);
|
if (collection == null) {
|
||||||
if (ts == null) {
|
|
||||||
return new ArrayList<>(0);
|
return new ArrayList<>(0);
|
||||||
}
|
}
|
||||||
return ts;
|
if (collection.isEmpty()) {
|
||||||
|
return new ArrayList<>(0);
|
||||||
|
}
|
||||||
|
return BeanUtil.copyToList(collection, targetType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user