From 4cfff40cb71ca81e6b7ece23dd487b597146b01b Mon Sep 17 00:00:00 2001 From: zch Date: Mon, 24 Mar 2025 18:58:05 +0800 Subject: [PATCH] =?UTF-8?q?add(common/common-mybatis):=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=BC=96=E7=A0=81=E5=94=AF=E4=B8=80=E6=80=A7=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E5=B7=A5=E5=85=B7=E7=B1=BB,=E6=96=B0=E5=A2=9E=20Uniqu?= =?UTF-8?q?eCodeUtils=20=E5=B7=A5=E5=85=B7=E7=B1=BB=EF=BC=8C=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E9=80=9A=E7=94=A8=E7=9A=84=E7=BC=96=E7=A0=81=E5=94=AF?= =?UTF-8?q?=E4=B8=80=E6=80=A7=E6=A0=A1=E9=AA=8C=E6=96=B9=E6=B3=95=E3=80=82?= =?UTF-8?q?=E8=AF=A5=E6=96=B9=E6=B3=95=E5=8F=AF=E7=94=A8=E4=BA=8E=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E6=95=B0=E6=8D=AE=E5=BA=93=E4=B8=AD=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E9=87=8D=E5=A4=8D=E7=9A=84=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E5=80=BC=EF=BC=8C=E6=94=AF=E6=8C=81=E6=96=B0=E5=A2=9E=E5=92=8C?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=93=8D=E4=BD=9C=E6=97=B6=E7=9A=84=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E6=A0=A1=E9=AA=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 validateCodeUnique 方法,用于编码唯一性校验 - 方法参数包括:实体对应的 Mapper 实例、需校验的编码值、实体类中编码字段和主键字段的 Lambda 表达式、当前记录的 ID值、重复时的错误提示信息 - 方法内部实现编码校验逻辑,如果编码重复则抛出 ServiceException 异常 - 提供了新增和更新操作时的使用示例 --- .../common/mybatis/Utils/UniqueCodeUtils.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/Utils/UniqueCodeUtils.java diff --git a/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/Utils/UniqueCodeUtils.java b/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/Utils/UniqueCodeUtils.java new file mode 100644 index 00000000..b3704a3f --- /dev/null +++ b/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/Utils/UniqueCodeUtils.java @@ -0,0 +1,81 @@ +package org.dromara.common.mybatis.Utils; + + +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; +import com.github.yulichang.base.MPJBaseMapper; +import com.github.yulichang.toolkit.JoinWrappers; +import com.github.yulichang.wrapper.MPJLambdaWrapper; +import org.dromara.common.core.exception.ServiceException; +import org.dromara.common.core.utils.StringUtils; +import org.springframework.stereotype.Component; + + +@Component +public class UniqueCodeUtils { + + /** + * 通用的编码唯一性校验方法 + * 用于校验数据库中是否存在重复的编码值 + * + * @param baseMapper 实体对应的Mapper实例,例如:baseMapper + * @param code 需要校验的编码值,例如:entity.getDeviceCode() + * @param codeField 实体类中编码字段的Lambda表达式,例如:DmsBaseDeviceBom::getDeviceCode + * @param idField 实体类中主键字段的Lambda表达式,例如:DmsBaseDeviceBom::getDeviceBomId + * @param idValue 当前记录的ID值(更新时需要传入,新增时传null),例如:entity.getDeviceBomId() + * @param errorMessage 重复时的错误提示信息,例如:"设备编码已存在" + * @param 实体类型 + * @param Mapper类型 + * @throws ServiceException 当编码重复时抛出异常,异常信息为errorMessage + * + * 使用示例1 - 新增时校验: + * uniqueCodeService.validateCodeUnique( + * baseMapper, // Mapper实例 + * entity.getDeviceCode(), // 待校验的编码 + * DmsBaseDeviceBom::getDeviceCode, // 编码字段 + * DmsBaseDeviceBom::getDeviceBomId,// ID字段 + * null, // 新增时ID为null + * "设备编码已存在" // 错误提示 + * ); + * + * 使用示例2 - 更新时校验: + * uniqueCodeService.validateCodeUnique( + * baseMapper, // Mapper实例 + * entity.getDeviceCode(), // 待校验的编码 + * DmsBaseDeviceBom::getDeviceCode, // 编码字段 + * DmsBaseDeviceBom::getDeviceBomId,// ID字段 + * entity.getDeviceBomId(), // 更新时传入当前记录ID + * "设备编码已存在" // 错误提示 + * ); + */ + public static > void validateCodeUnique( + M baseMapper, + String code, + SFunction codeField, + SFunction idField, + Object idValue, + String errorMessage + ) { + // 1. 首先判断编码是否为空,为空则不需要校验 + if (StringUtils.isNotBlank(code)) { + // 2. 构建查询条件包装器 + MPJLambdaWrapper wrapper = JoinWrappers.lambda((Class) codeField.getClass()) + // 3. 添加编码相等的条件:WHERE code = 'xxx' + .eq(codeField, code); + + // 4. 如果是更新操作(idValue不为空),需要排除自身记录 + if (idValue != null) { + // 5. 添加ID不等于当前记录ID的条件:AND id != xxx + // 这样可以排除当前正在更新的记录,避免误报重复 + wrapper.ne(idField, idValue); + } + + // 6. 执行查询,获取符合条件的记录数 + Long count = baseMapper.selectCount(wrapper); + // 7. 如果存在符合条件的记录(count > 0),说明编码重复 + if (count > 0) { + // 8. 抛出业务异常,中断当前操作 + throw new ServiceException(errorMessage); + } + } + } +}