时间日期加自增序列ID生成工具类

如题,

其一,最简单的通过使用SimpleDateFormat生成每日日期,并使用一个原子整数AtomicInteger来生成递增的ID,每天的第一个ID将从1开始,此处给出的是单例模式情况下,无需注入直接使用

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

public class DailyIdUtil {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
    private final AtomicInteger sequence = new AtomicInteger(1);
    private String lastDate = getCurrentDate();

    // 单例实例
    private static DailyIdUtil instance;

    // 私有化构造方法,防止外部直接创建实例
    private DailyIdUtil() {}

    // 提供一个全局访问点获取唯一的实例
    public static synchronized DailyIdUtil getInstance() {
        if (instance == null) {
            instance = new DailyIdUtil();
        }
        return instance;
    }

    // 生成ID的方法
    public synchronized String generateId() {
        String currentDate = getCurrentDate();
        if (!lastDate.equals(currentDate)) {
            sequence.set(1);
            lastDate = currentDate;
        }
        return currentDate + String.format("%05d", sequence.getAndIncrement());
    }

    private String getCurrentDate() {
        return DATE_FORMAT.format(new Date());
    }
    
}

使用方法为

String id = DailyIdUtil.getInstance().generateId();

其二,考虑分布式或者其他原因导致的ID生成重复问题的时间+自增序列ID工具类如下

首先需要一张简单表,包含这些字段,当前日期"ID_DATE",当前自增序列编号"CURRENT_ID",自定义ID前缀"ID_PREFIX"

这里DAO通过MybatisPlus实现,如下

import com.baomidou.mybatisplus.annotation.TableName;
import com.haiyisoft.common.mybatis.core.domain.PipelineBaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@TableName("util_date_id")
@EqualsAndHashCode(callSuper = true)
public class DBDateId extends PipelineBaseEntity {
    private String idPrefix;
    private String idDate;
    private Integer currentId;
}



import com.baomidou.mybatisplus.extension.service.IService;
import com.haiyisoft.web.entity.DBDateId;

public interface DBDateIdService extends IService<DBDateId> {
}



import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haiyisoft.web.entity.DBDateId;
import com.haiyisoft.web.mapper.DateIdMapper;
import com.haiyisoft.web.service.DBDateIdService;
import org.springframework.stereotype.Service;

@Service("dBDateIdService")
public class DBDateIdServiceImpl extends ServiceImpl<DateIdMapper, DBDateId> implements DBDateIdService {
}



import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.haiyisoft.web.entity.DBDateId;

public interface DateIdMapper extends BaseMapper<DBDateId> {
}

工具类通过实现感知类,无需注入直接使用

package com.haiyisoft.web.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haiyisoft.web.entity.DBDateId;
import com.haiyisoft.web.service.DBDateIdService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class DBDateIdUtil implements ApplicationContextAware {
    private static DBDateIdService dbDateIdService;
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        dbDateIdService = applicationContext.getBean(DBDateIdService.class);
    }

    public static String generateId(String prefix) {
        String currentDate = sdf.format(new Date());

        QueryWrapper<DBDateId> wrapper = new QueryWrapper<DBDateId>().eq("PREFIX", prefix).eq("ID_DATE", currentDate);
        DBDateId dBDateId = dbDateIdService.getOne(wrapper);
        if (dBDateId == null) {
            dBDateId = new DBDateId();
            dBDateId.setIdPrefix(prefix);
            dBDateId.setIdDate(currentDate);
            dBDateId.setCurrentId(1);
            dbDateIdService.save(dBDateId);
            return currentDate + "0001";
        } else {
            if (!currentDate.equals(dBDateId.getIdDate())) {
                dBDateId.setIdDate(currentDate);
                dBDateId.setCurrentId(1);
            } else {
                dBDateId.setCurrentId(dBDateId.getCurrentId() + 1);
            }
            dbDateIdService.update(dBDateId,new QueryWrapper<DBDateId>().eq("PREFIX",prefix).eq("CURRENT_ID",dBDateId.getCurrentId()-1).eq("ID_DATE",currentDate));
            return currentDate + String.format("%04d", dBDateId.getCurrentId());
        }
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值