[重大更新] 增加mybatis-plus扩展工具 大幅简化整体业务代码写法 优雅舒适

This commit is contained in:
疯狂的狮子Li
2026-05-15 11:39:20 +08:00
parent fdf9e4ae39
commit 7a5d6b4723
44 changed files with 1535 additions and 483 deletions
@@ -1,8 +1,6 @@
package org.dromara.workflow.mapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
import org.dromara.common.mybatis.helper.DataBaseHelper;
import org.dromara.workflow.domain.FlowCategory;
import org.dromara.workflow.domain.vo.FlowCategoryVo;
@@ -25,9 +23,10 @@ public interface FlwCategoryMapper extends BaseMapperPlus<FlowCategory, FlowCate
* @return 包含子流程分类的列表
*/
default List<FlowCategory> selectListByParentId(Long parentId) {
return this.selectList(new LambdaQueryWrapper<FlowCategory>()
return this.lambda()
.select(FlowCategory::getCategoryId)
.apply(DataBaseHelper.findInSet(parentId, "ancestors")));
.findInSet(parentId, FlowCategory::getAncestors)
.list();
}
/**
@@ -1,7 +1,6 @@
package org.dromara.workflow.mapper;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
import org.dromara.workflow.domain.FlowInstanceBizExt;
@@ -23,8 +22,9 @@ public interface FlwInstanceBizExtMapper extends BaseMapperPlus<FlowInstanceBizE
*/
default int saveOrUpdateByInstanceId(FlowInstanceBizExt entity) {
// 查询是否存在
FlowInstanceBizExt exist = this.selectOne(new LambdaQueryWrapper<FlowInstanceBizExt>()
.eq(FlowInstanceBizExt::getInstanceId, entity.getInstanceId()));
FlowInstanceBizExt exist = this.lambda()
.eq(FlowInstanceBizExt::getInstanceId, entity.getInstanceId())
.one();
if (ObjectUtil.isNotNull(exist)) {
// 存在就带上主键更新
@@ -43,8 +43,7 @@ public interface FlwInstanceBizExtMapper extends BaseMapperPlus<FlowInstanceBizE
* @return 删除的行数
*/
default int deleteByInstId(Long instanceId) {
return this.delete(new LambdaQueryWrapper<FlowInstanceBizExt>()
.eq(FlowInstanceBizExt::getInstanceId, instanceId));
return this.lambda().eq(FlowInstanceBizExt::getInstanceId, instanceId).deleteCount();
}
/**
@@ -54,8 +53,7 @@ public interface FlwInstanceBizExtMapper extends BaseMapperPlus<FlowInstanceBizE
* @return 删除的行数
*/
default int deleteByInstIds(Collection<Long> instanceIds) {
return this.delete(new LambdaQueryWrapper<FlowInstanceBizExt>()
.in(FlowInstanceBizExt::getInstanceId, instanceIds));
return this.lambda().in(FlowInstanceBizExt::getInstanceId, instanceIds).deleteCount();
}
}
@@ -10,7 +10,6 @@ import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.SystemConstants;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.*;
import org.dromara.common.mybatis.helper.DataBaseHelper;
import org.dromara.warm.flow.core.service.DefService;
import org.dromara.warm.flow.orm.entity.FlowDefinition;
import org.dromara.warm.flow.ui.service.CategoryService;
@@ -64,8 +63,10 @@ public class FlwCategoryServiceImpl implements IFlwCategoryService, CategoryServ
if (ObjectUtil.isNull(categoryId)) {
return null;
}
FlowCategory category = baseMapper.selectOne(new LambdaQueryWrapper<FlowCategory>()
.select(FlowCategory::getCategoryName).eq(FlowCategory::getCategoryId, categoryId));
FlowCategory category = baseMapper.lambda()
.select(FlowCategory::getCategoryName)
.eq(FlowCategory::getCategoryId, categoryId)
.one();
return ObjectUtils.notNullGetter(category, FlowCategory::getCategoryName);
}
@@ -80,9 +81,10 @@ public class FlwCategoryServiceImpl implements IFlwCategoryService, CategoryServ
if (CollUtil.isEmpty(categoryIds)) {
return Collections.emptyMap();
}
List<FlowCategory> list = baseMapper.selectList(new LambdaQueryWrapper<FlowCategory>()
List<FlowCategory> list = baseMapper.lambda()
.select(FlowCategory::getCategoryId, FlowCategory::getCategoryName)
.in(FlowCategory::getCategoryId, categoryIds));
.in(FlowCategory::getCategoryId, categoryIds)
.list();
return StreamUtils.toMap(list, FlowCategory::getCategoryId, FlowCategory::getCategoryName);
}
@@ -145,10 +147,11 @@ public class FlwCategoryServiceImpl implements IFlwCategoryService, CategoryServ
*/
@Override
public boolean checkCategoryNameUnique(FlowCategoryBo category) {
boolean exist = baseMapper.exists(new LambdaQueryWrapper<FlowCategory>()
boolean exist = baseMapper.lambda()
.eq(FlowCategory::getCategoryName, category.getCategoryName())
.eq(FlowCategory::getParentId, category.getParentId())
.ne(ObjectUtil.isNotNull(category.getCategoryId()), FlowCategory::getCategoryId, category.getCategoryId()));
.neIfPresent(FlowCategory::getCategoryId, category.getCategoryId())
.exists();
return !exist;
}
@@ -173,8 +176,7 @@ public class FlwCategoryServiceImpl implements IFlwCategoryService, CategoryServ
*/
@Override
public boolean hasChildByCategoryId(Long categoryId) {
return baseMapper.exists(new LambdaQueryWrapper<FlowCategory>()
.eq(FlowCategory::getParentId, categoryId));
return baseMapper.lambda().eq(FlowCategory::getParentId, categoryId).exists();
}
/**
@@ -255,8 +257,9 @@ public class FlwCategoryServiceImpl implements IFlwCategoryService, CategoryServ
* @param oldAncestors 旧的父ID集合
*/
private void updateCategoryChildren(Long categoryId, String newAncestors, String oldAncestors) {
List<FlowCategory> children = baseMapper.selectList(new LambdaQueryWrapper<FlowCategory>()
.apply(DataBaseHelper.findInSet(categoryId, "ancestors")));
List<FlowCategory> children = baseMapper.lambda()
.findInSet(categoryId, FlowCategory::getAncestors)
.list();
List<FlowCategory> list = new ArrayList<>();
for (FlowCategory child : children) {
FlowCategory category = new FlowCategory();
@@ -16,6 +16,7 @@ import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.query.QueryBuilder;
import org.dromara.warm.flow.core.dto.DefJson;
import org.dromara.warm.flow.core.enums.NodeType;
import org.dromara.warm.flow.core.enums.PublishStatus;
@@ -121,7 +122,8 @@ public class FlwDefinitionServiceImpl implements IFlwDefinitionService {
@Override
@Transactional(rollbackFor = Exception.class)
public boolean publish(Long id) {
List<FlowNode> flowNodes = flowNodeMapper.selectList(new LambdaQueryWrapper<FlowNode>().eq(FlowNode::getDefinitionId, id));
List<FlowNode> flowNodes = flowNodeMapper.selectList(
QueryBuilder.lambda(FlowNode.class).eq(FlowNode::getDefinitionId, id).build());
List<String> errorMsg = new ArrayList<>();
if (CollUtil.isNotEmpty(flowNodes)) {
String applyNodeCode = flwCommonService.applyNodeCode(id);
@@ -4,8 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
@@ -17,6 +16,7 @@ import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.query.QueryBuilder;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.warm.flow.core.FlowEngine;
import org.dromara.warm.flow.core.constant.ExceptionCons;
@@ -185,7 +185,10 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
*/
@Override
public FlowInstance selectInstByBusinessId(String businessId) {
return flowInstanceMapper.selectOne(new LambdaQueryWrapper<FlowInstance>().eq(FlowInstance::getBusinessId, businessId));
return flowInstanceMapper.selectOne(
QueryBuilder.lambda(FlowInstance.class)
.eq(FlowInstance::getBusinessId, businessId)
.build());
}
/**
@@ -219,7 +222,10 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
@Override
@Transactional(rollbackFor = Exception.class)
public boolean deleteByBusinessIds(List<String> businessIds) {
List<FlowInstance> flowInstances = flowInstanceMapper.selectList(new LambdaQueryWrapper<FlowInstance>().in(FlowInstance::getBusinessId, businessIds));
List<FlowInstance> flowInstances = flowInstanceMapper.selectList(
QueryBuilder.lambda(FlowInstance.class)
.in(FlowInstance::getBusinessId, businessIds)
.build());
if (CollUtil.isEmpty(flowInstances)) {
log.warn("未找到对应的流程实例信息,无法执行删除操作。");
return false;
@@ -393,10 +399,11 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
// 再组装历史任务(已处理任务)
List<FlowHisTaskVo> hisTaskVos = new ArrayList<>();
List<FlowHisTask> hisTasks = flowHisTaskMapper.selectList(
new LambdaQueryWrapper<FlowHisTask>()
QueryBuilder.lambda(FlowHisTask.class)
.eq(FlowHisTask::getInstanceId, instanceId)
.eq(FlowHisTask::getNodeType, NodeType.BETWEEN.getKey())
.orderByDesc(FlowHisTask::getUpdateTime)
.build()
);
if (CollUtil.isNotEmpty(hisTasks)) {
hisTaskVos = BeanUtil.copyToList(hisTasks, FlowHisTaskVo.class);
@@ -418,10 +425,9 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
*/
@Override
public void updateStatus(Long instanceId, String status) {
LambdaUpdateWrapper<FlowInstance> wrapper = new LambdaUpdateWrapper<>();
wrapper.set(FlowInstance::getFlowStatus, status);
wrapper.eq(FlowInstance::getId, instanceId);
flowInstanceMapper.update(wrapper);
flowInstanceMapper.update(Wrappers.lambdaUpdate(FlowInstance.class)
.set(FlowInstance::getFlowStatus, status)
.eq(FlowInstance::getId, instanceId));
}
/**
@@ -1,7 +1,6 @@
package org.dromara.workflow.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -135,9 +134,10 @@ public class FlwSpelServiceImpl implements IFlwSpelService {
*/
private void validEntityBeforeSave(FlowSpel entity){
if (StringUtils.isNotBlank(entity.getViewSpel())) {
boolean exists = baseMapper.exists(new LambdaQueryWrapper<FlowSpel>()
boolean exists = baseMapper.lambda()
.eq(FlowSpel::getViewSpel, entity.getViewSpel())
.ne(ObjectUtil.isNotNull(entity.getId()), FlowSpel::getId, entity.getId()));
.neIfPresent(FlowSpel::getId, entity.getId())
.exists();
if (exists) {
throw new ServiceException("SpEL表达式已存在,请勿重复添加");
}
@@ -193,11 +193,10 @@ public class FlwSpelServiceImpl implements IFlwSpelService {
if (CollUtil.isEmpty(viewSpels)) {
return Collections.emptyMap();
}
List<FlowSpel> list = baseMapper.selectList(
new LambdaQueryWrapper<FlowSpel>()
.select(FlowSpel::getViewSpel, FlowSpel::getRemark)
.in(FlowSpel::getViewSpel, viewSpels)
);
List<FlowSpel> list = baseMapper.lambda()
.select(FlowSpel::getViewSpel, FlowSpel::getRemark)
.in(FlowSpel::getViewSpel, viewSpels)
.list();
return StreamUtils.toMap(list, FlowSpel::getViewSpel, x ->
StringUtils.isEmpty(x.getRemark()) ? "" : x.getRemark()
);
@@ -7,7 +7,6 @@ import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.lock.annotation.Lock4j;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -21,6 +20,7 @@ import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.query.QueryBuilder;
import org.dromara.common.mybatis.utils.IdGeneratorUtil;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.api.domain.UserDTO;
@@ -121,8 +121,9 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
FlowInstanceBizExt bizExt = startProcessBo.getBizExt();
// 获取已有流程实例
FlowInstance flowInstance = flowInstanceMapper.selectOne(new LambdaQueryWrapper<>(FlowInstance.class)
.eq(FlowInstance::getBusinessId, businessId));
FlowInstance flowInstance = flowInstanceMapper.selectOne(QueryBuilder.lambda(FlowInstance.class)
.eq(FlowInstance::getBusinessId, businessId)
.build());
if (ObjectUtil.isNotNull(flowInstance)) {
// 已存在流程
@@ -339,8 +340,9 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
}
// 添加抄送人记录
FlowHisTask flowHisTask = flowHisTaskMapper.selectList(
new LambdaQueryWrapper<>(FlowHisTask.class)
.eq(FlowHisTask::getTaskId, task.getId())).get(0);
QueryBuilder.lambda(FlowHisTask.class)
.eq(FlowHisTask::getTaskId, task.getId())
.build()).get(0);
FlowNode flowNode = new FlowNode();
flowNode.setNodeCode(flowHisTask.getTargetNodeCode());
flowNode.setNodeName(flowHisTask.getTargetNodeName());
@@ -599,7 +601,9 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
*/
@Override
public List<FlowTask> selectByIdList(Collection<Long> taskIdList) {
return flowTaskMapper.selectList(new LambdaQueryWrapper<>(FlowTask.class).in(FlowTask::getId, taskIdList));
return flowTaskMapper.selectList(QueryBuilder.lambda(FlowTask.class)
.in(FlowTask::getId, taskIdList)
.build());
}
/**
@@ -713,7 +717,9 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
*/
@Override
public FlowHisTask selectHisTaskById(Long taskId) {
return flowHisTaskMapper.selectOne(new LambdaQueryWrapper<>(FlowHisTask.class).eq(FlowHisTask::getId, taskId));
return flowHisTaskMapper.selectOne(QueryBuilder.lambda(FlowHisTask.class)
.eq(FlowHisTask::getId, taskId)
.build());
}
/**
@@ -724,7 +730,9 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
*/
@Override
public List<FlowTask> selectByInstId(Long instanceId) {
return flowTaskMapper.selectList(new LambdaQueryWrapper<>(FlowTask.class).eq(FlowTask::getInstanceId, instanceId));
return flowTaskMapper.selectList(QueryBuilder.lambda(FlowTask.class)
.eq(FlowTask::getInstanceId, instanceId)
.build());
}
/**
@@ -735,7 +743,9 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
*/
@Override
public List<FlowTask> selectByInstIds(Collection<Long> instanceIds) {
return flowTaskMapper.selectList(new LambdaQueryWrapper<>(FlowTask.class).in(FlowTask::getInstanceId, instanceIds));
return flowTaskMapper.selectList(QueryBuilder.lambda(FlowTask.class)
.in(FlowTask::getInstanceId, instanceIds)
.build());
}
/**
@@ -746,7 +756,9 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
*/
@Override
public boolean isTaskEnd(Long instanceId) {
boolean exists = flowTaskMapper.exists(new LambdaQueryWrapper<FlowTask>().eq(FlowTask::getInstanceId, instanceId));
boolean exists = flowTaskMapper.exists(QueryBuilder.lambda(FlowTask.class)
.eq(FlowTask::getInstanceId, instanceId)
.build());
return !exists;
}
@@ -897,9 +909,10 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
*/
@Override
public FlowNode getByNodeCode(String nodeCode, Long definitionId) {
return flowNodeMapper.selectOne(new LambdaQueryWrapper<FlowNode>()
return flowNodeMapper.selectOne(QueryBuilder.lambda(FlowNode.class)
.eq(FlowNode::getNodeCode, nodeCode)
.eq(FlowNode::getDefinitionId, definitionId));
.eq(FlowNode::getDefinitionId, definitionId)
.build());
}
/**