1.随机生成编号(雪花算法)
int month = Calendar.getInstance().get(Calendar.MONTH);
Snowflake snowflake = IdUtil.createSnowflake(month, month);
Long.toString(snowflake.nextId())
2.按规则生成编号(redis)
拼接key放入redis中,作为生成新编号的依据
@Autowired
private RedisTemplate redisTemplate;
int year=DateUtil.year(DateUtil.date());
Long increment = redisTemplate.opsForValue().increment(key, 1);
String projectNo;
if(increment<10){
projectNo=key+"00"+increment;
}else if(increment>=10 && increment<100){
projectNo=key+"0"+increment;
}else{
projectNo=key+increment;
}