mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-12-26 18:15:59 +08:00
!802 update 添加 ID 生成工具类,支持多种 ID 生成方式
* update 使用 IdGeneratorUtil 替代主键生成 * update 添加 ID 生成工具类,支持多种 ID 生成方式
This commit is contained in:
@@ -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
|
||||||
|
* <p>
|
||||||
|
* 调用 {@link IdentifierGenerator#nextId(Object)},返回 String 格式 ID。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return 字符串格式主键 ID
|
||||||
|
*/
|
||||||
|
public static String nextId() {
|
||||||
|
return GENERATOR.nextId(null).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成 Long 类型主键 ID
|
||||||
|
* <p>
|
||||||
|
* 自动将生成的数字型主键转换为 Long 类型
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return Long 类型主键 ID
|
||||||
|
*/
|
||||||
|
public static Long nextLongId() {
|
||||||
|
return GENERATOR.nextId(null).longValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成 Number 类型主键 ID
|
||||||
|
* <p>
|
||||||
|
* 推荐在需要保留原始 Number 类型时使用
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return Number 类型主键 ID
|
||||||
|
*/
|
||||||
|
public static Number nextNumberId() {
|
||||||
|
return GENERATOR.nextId(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据实体生成数字型主键 ID
|
||||||
|
* <p>
|
||||||
|
* 若自定义的 {@link IdentifierGenerator} 根据实体内容生成 ID,则可以使用本方法
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param entity 实体对象
|
||||||
|
* @return Number 类型主键 ID
|
||||||
|
*/
|
||||||
|
public static Number nextId(Object entity) {
|
||||||
|
return GENERATOR.nextId(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据实体生成字符串主键 ID
|
||||||
|
* <p>
|
||||||
|
* 与 {@link #nextId(Object)} 类似,但返回 String 类型
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param entity 实体对象
|
||||||
|
* @return 字符串格式主键 ID
|
||||||
|
*/
|
||||||
|
public static String nextStringId(Object entity) {
|
||||||
|
return GENERATOR.nextId(entity).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成 32 位 UUID
|
||||||
|
* <p>
|
||||||
|
* 底层使用 {@link IdWorker#get32UUID()}
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return 32 位 UUID 字符串
|
||||||
|
*/
|
||||||
|
public static String nextUUID() {
|
||||||
|
return IdWorker.get32UUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据实体生成 32 位 UUID
|
||||||
|
* <p>
|
||||||
|
* 默认 {@link IdentifierGenerator#nextUUID(Object)} 实现忽略实体,但保留该方法便于扩展。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param entity 实体对象
|
||||||
|
* @return 32 位 UUID 字符串
|
||||||
|
*/
|
||||||
|
public static String nextUUID(Object entity) {
|
||||||
|
return GENERATOR.nextUUID(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成带指定前缀的字符串主键 ID
|
||||||
|
* <p>
|
||||||
|
* 示例:prefix = "ORD",生成结果形如:{@code ORD20251211000123}
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,7 +8,6 @@ import com.baomidou.dynamic.datasource.annotation.DS;
|
|||||||
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
||||||
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.incrementer.IdentifierGenerator;
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import lombok.RequiredArgsConstructor;
|
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.json.utils.JsonUtils;
|
||||||
import org.dromara.common.mybatis.core.page.PageQuery;
|
import org.dromara.common.mybatis.core.page.PageQuery;
|
||||||
import org.dromara.common.mybatis.core.page.TableDataInfo;
|
import org.dromara.common.mybatis.core.page.TableDataInfo;
|
||||||
|
import org.dromara.common.mybatis.utils.IdGeneratorUtil;
|
||||||
import org.dromara.generator.constant.GenConstants;
|
import org.dromara.generator.constant.GenConstants;
|
||||||
import org.dromara.generator.domain.GenTable;
|
import org.dromara.generator.domain.GenTable;
|
||||||
import org.dromara.generator.domain.GenTableColumn;
|
import org.dromara.generator.domain.GenTableColumn;
|
||||||
@@ -60,7 +60,6 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
|
|
||||||
private final GenTableMapper baseMapper;
|
private final GenTableMapper baseMapper;
|
||||||
private final GenTableColumnMapper genTableColumnMapper;
|
private final GenTableColumnMapper genTableColumnMapper;
|
||||||
private final IdentifierGenerator identifierGenerator;
|
|
||||||
|
|
||||||
private static final String[] TABLE_IGNORE = new String[]{"sj_", "flow_", "gen_"};
|
private static final String[] TABLE_IGNORE = new String[]{"sj_", "flow_", "gen_"};
|
||||||
|
|
||||||
@@ -322,7 +321,7 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
GenTable table = baseMapper.selectGenTableById(tableId);
|
GenTable table = baseMapper.selectGenTableById(tableId);
|
||||||
List<Long> menuIds = new ArrayList<>();
|
List<Long> menuIds = new ArrayList<>();
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
menuIds.add(identifierGenerator.nextId(null).longValue());
|
menuIds.add(IdGeneratorUtil.nextLongId());
|
||||||
}
|
}
|
||||||
table.setMenuIds(menuIds);
|
table.setMenuIds(menuIds);
|
||||||
// 设置主键列信息
|
// 设置主键列信息
|
||||||
@@ -468,7 +467,7 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
GenTable table = baseMapper.selectGenTableById(tableId);
|
GenTable table = baseMapper.selectGenTableById(tableId);
|
||||||
List<Long> menuIds = new ArrayList<>();
|
List<Long> menuIds = new ArrayList<>();
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
menuIds.add(identifierGenerator.nextId(null).longValue());
|
menuIds.add(IdGeneratorUtil.nextLongId());
|
||||||
}
|
}
|
||||||
table.setMenuIds(menuIds);
|
table.setMenuIds(menuIds);
|
||||||
// 设置主键列信息
|
// 设置主键列信息
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import com.baomidou.lock.annotation.Lock4j;
|
import com.baomidou.lock.annotation.Lock4j;
|
||||||
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.incrementer.IdentifierGenerator;
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@@ -27,6 +26,7 @@ import org.dromara.common.core.validate.EditGroup;
|
|||||||
import org.dromara.common.json.utils.JsonUtils;
|
import org.dromara.common.json.utils.JsonUtils;
|
||||||
import org.dromara.common.mybatis.core.page.PageQuery;
|
import org.dromara.common.mybatis.core.page.PageQuery;
|
||||||
import org.dromara.common.mybatis.core.page.TableDataInfo;
|
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.common.satoken.utils.LoginHelper;
|
||||||
import org.dromara.warm.flow.core.FlowEngine;
|
import org.dromara.warm.flow.core.FlowEngine;
|
||||||
import org.dromara.warm.flow.core.dto.FlowParams;
|
import org.dromara.warm.flow.core.dto.FlowParams;
|
||||||
@@ -86,7 +86,6 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
|
|||||||
private final FlowInstanceMapper flowInstanceMapper;
|
private final FlowInstanceMapper flowInstanceMapper;
|
||||||
private final FlowTaskMapper flowTaskMapper;
|
private final FlowTaskMapper flowTaskMapper;
|
||||||
private final FlowHisTaskMapper flowHisTaskMapper;
|
private final FlowHisTaskMapper flowHisTaskMapper;
|
||||||
private final IdentifierGenerator identifierGenerator;
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private final FlwTaskMapper flwTaskMapper;
|
private final FlwTaskMapper flwTaskMapper;
|
||||||
private final FlwCategoryMapper flwCategoryMapper;
|
private final FlwCategoryMapper flwCategoryMapper;
|
||||||
@@ -332,7 +331,7 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
|
|||||||
flowNode.setNodeCode(flowHisTask.getTargetNodeCode());
|
flowNode.setNodeCode(flowHisTask.getTargetNodeCode());
|
||||||
flowNode.setNodeName(flowHisTask.getTargetNodeName());
|
flowNode.setNodeName(flowHisTask.getTargetNodeName());
|
||||||
//生成新的任务id
|
//生成新的任务id
|
||||||
long taskId = identifierGenerator.nextId(null).longValue();
|
long taskId = IdGeneratorUtil.nextLongId();
|
||||||
task.setId(taskId);
|
task.setId(taskId);
|
||||||
task.setNodeName("【抄送】" + task.getNodeName());
|
task.setNodeName("【抄送】" + task.getNodeName());
|
||||||
Date updateTime = new Date(flowHisTask.getUpdateTime().getTime() - 1000);
|
Date updateTime = new Date(flowHisTask.getUpdateTime().getTime() - 1000);
|
||||||
|
|||||||
Reference in New Issue
Block a user