From fdbffa2b61d60c290331d80b5168fd97b54e59d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Thu, 11 Dec 2025 10:25:35 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=B7=BB=E5=8A=A0=20ID=20=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=B7=A5=E5=85=B7=E7=B1=BB=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=A4=9A=E7=A7=8D=20ID=20=E7=94=9F=E6=88=90=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/mybatis/utils/IdGeneratorUtil.java | 129 ++++++++++++++++++ .../gen/service/GenTableServiceImpl.java | 7 +- .../service/impl/FlwTaskServiceImpl.java | 5 +- 3 files changed, 134 insertions(+), 7 deletions(-) create mode 100644 ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/utils/IdGeneratorUtil.java diff --git a/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/utils/IdGeneratorUtil.java b/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/utils/IdGeneratorUtil.java new file mode 100644 index 00000000..33b3f1c5 --- /dev/null +++ b/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/utils/IdGeneratorUtil.java @@ -0,0 +1,129 @@ +package org.dromara.common.mybatis.utils; + +import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator; +import com.baomidou.mybatisplus.core.toolkit.IdWorker; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.dromara.common.core.utils.SpringUtils; + +/** + * ID 生成工具类 + * + * @author AprilWind + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class IdGeneratorUtil { + + private static final IdentifierGenerator GENERATOR = SpringUtils.getBean(IdentifierGenerator.class); + + /** + * 生成字符串类型主键 ID + *

+ * 调用 {@link IdentifierGenerator#nextId(Object)},返回 String 格式 ID。 + *

+ * + * @return 字符串格式主键 ID + */ + public static String nextId() { + return GENERATOR.nextId(null).toString(); + } + + /** + * 生成 Long 类型主键 ID + *

+ * 自动将生成的数字型主键转换为 Long 类型 + *

+ * + * @return Long 类型主键 ID + */ + public static Long nextLongId() { + return GENERATOR.nextId(null).longValue(); + } + + /** + * 生成 Number 类型主键 ID + *

+ * 推荐在需要保留原始 Number 类型时使用 + *

+ * + * @return Number 类型主键 ID + */ + public static Number nextNumberId() { + return GENERATOR.nextId(null); + } + + /** + * 根据实体生成数字型主键 ID + *

+ * 若自定义的 {@link IdentifierGenerator} 根据实体内容生成 ID,则可以使用本方法 + *

+ * + * @param entity 实体对象 + * @return Number 类型主键 ID + */ + public static Number nextId(Object entity) { + return GENERATOR.nextId(entity); + } + + /** + * 根据实体生成字符串主键 ID + *

+ * 与 {@link #nextId(Object)} 类似,但返回 String 类型 + *

+ * + * @param entity 实体对象 + * @return 字符串格式主键 ID + */ + public static String nextStringId(Object entity) { + return GENERATOR.nextId(entity).toString(); + } + + /** + * 生成 32 位 UUID + *

+ * 底层使用 {@link IdWorker#get32UUID()} + *

+ * + * @return 32 位 UUID 字符串 + */ + public static String nextUUID() { + return IdWorker.get32UUID(); + } + + /** + * 根据实体生成 32 位 UUID + *

+ * 默认 {@link IdentifierGenerator#nextUUID(Object)} 实现忽略实体,但保留该方法便于扩展。 + *

+ * + * @param entity 实体对象 + * @return 32 位 UUID 字符串 + */ + public static String nextUUID(Object entity) { + return GENERATOR.nextUUID(entity); + } + + /** + * 生成带指定前缀的字符串主键 ID + *

+ * 示例:prefix = "ORD",生成结果形如:{@code ORD20251211000123} + *

+ * + * @param prefix 自定义前缀 + * @return 带前缀的字符串主键 ID + */ + public static String nextIdWithPrefix(String prefix) { + return prefix + nextId(); + } + + /** + * 生成带前缀的 UUID + * + * @param prefix 前缀 + * @return prefix + UUID + */ + public static String nextUUIDWithPrefix(String prefix) { + return prefix + nextUUID(); + } + +} diff --git a/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/service/GenTableServiceImpl.java b/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/service/GenTableServiceImpl.java index 3aa61c2f..accd8e2d 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/service/GenTableServiceImpl.java +++ b/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/service/GenTableServiceImpl.java @@ -8,7 +8,6 @@ import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.dynamic.datasource.annotation.DSTransactional; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.RequiredArgsConstructor; @@ -28,6 +27,7 @@ import org.dromara.common.core.utils.file.FileUtils; import org.dromara.common.json.utils.JsonUtils; import org.dromara.common.mybatis.core.page.PageQuery; import org.dromara.common.mybatis.core.page.TableDataInfo; +import org.dromara.common.mybatis.utils.IdGeneratorUtil; import org.dromara.gen.constant.GenConstants; import org.dromara.gen.domain.GenTable; import org.dromara.gen.domain.GenTableColumn; @@ -60,7 +60,6 @@ public class GenTableServiceImpl implements IGenTableService { private final GenTableMapper baseMapper; private final GenTableColumnMapper genTableColumnMapper; - private final IdentifierGenerator identifierGenerator; private static final String[] TABLE_IGNORE = new String[]{"sj_", "act_", "flw_", "gen_"}; @@ -322,7 +321,7 @@ public class GenTableServiceImpl implements IGenTableService { GenTable table = baseMapper.selectGenTableById(tableId); List menuIds = new ArrayList<>(); for (int i = 0; i < 6; i++) { - menuIds.add(identifierGenerator.nextId(null).longValue()); + menuIds.add(IdGeneratorUtil.nextLongId()); } table.setMenuIds(menuIds); // 设置主键列信息 @@ -468,7 +467,7 @@ public class GenTableServiceImpl implements IGenTableService { GenTable table = baseMapper.selectGenTableById(tableId); List menuIds = new ArrayList<>(); for (int i = 0; i < 6; i++) { - menuIds.add(identifierGenerator.nextId(null).longValue()); + menuIds.add(IdGeneratorUtil.nextLongId()); } table.setMenuIds(menuIds); // 设置主键列信息 diff --git a/ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/FlwTaskServiceImpl.java b/ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/FlwTaskServiceImpl.java index 5a652794..488719e8 100644 --- a/ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/FlwTaskServiceImpl.java +++ b/ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/FlwTaskServiceImpl.java @@ -9,7 +9,6 @@ import cn.hutool.core.util.StrUtil; import com.baomidou.lock.annotation.Lock4j; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.RequiredArgsConstructor; @@ -25,6 +24,7 @@ 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.page.TableDataInfo; +import org.dromara.common.mybatis.utils.IdGeneratorUtil; import org.dromara.common.satoken.utils.LoginHelper; import org.dromara.system.api.RemoteUserService; import org.dromara.system.api.domain.vo.RemoteUserVo; @@ -87,7 +87,6 @@ public class FlwTaskServiceImpl implements IFlwTaskService { private final FlowInstanceMapper flowInstanceMapper; private final FlowTaskMapper flowTaskMapper; private final FlowHisTaskMapper flowHisTaskMapper; - private final IdentifierGenerator identifierGenerator; private final FlwTaskMapper flwTaskMapper; private final FlwCategoryMapper flwCategoryMapper; private final FlowNodeMapper flowNodeMapper; @@ -334,7 +333,7 @@ public class FlwTaskServiceImpl implements IFlwTaskService { flowNode.setNodeCode(flowHisTask.getTargetNodeCode()); flowNode.setNodeName(flowHisTask.getTargetNodeName()); //生成新的任务id - long taskId = identifierGenerator.nextId(null).longValue(); + long taskId = IdGeneratorUtil.nextLongId(); task.setId(taskId); task.setNodeName("【抄送】" + task.getNodeName()); Date updateTime = new Date(flowHisTask.getUpdateTime().getTime() - 1000);