From 9e695545477f9cde8920252bb4a7351970ec205c Mon Sep 17 00:00:00 2001 From: AprilWind <2100166581@qq.com> Date: Thu, 11 Dec 2025 10:12:23 +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 ++++++++++++++++++ 1 file changed, 129 insertions(+) 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 000000000..33b3f1c56 --- /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(); + } + +}