编者在开发过程中用postman测试接口,发现要求id为必填且不能含有英文字母,问了对面开发人员才知道需要自己生成20位Long型uuid,写法大概如下,在需要生成的部分调用这个类即可。
- package nc.bs.task.util;
-
- import java.text.SimpleDateFormat;
-
- public class UUID{
-
- private static volatile int Guid = 100;
- public static String getGuid()
- {
- UUID.Guid += 1;
- long now = System.currentTimeMillis();
- //获取4位年份数字
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
- //获取时间戳
- String time = dateFormat.format(now);
- String info = now + "";
- //获取三位随机数
- //int ran=(int) ((Math.random()*9+1)*100);
- //要是一段时间内的数据量过大会有重复的情况,所以做以下修改
- int ran = 0;
- if (UUID.Guid > 999)
- {
- UUID.Guid = 100;
- }
- ran = UUID.Guid;
- return time + info.substring(2, info.length()) + ran;
- }
- }
调用场景:
- //20位UUID,如果有值走更新接口,否则走新增,将生成的uuid传给自定义5
- //先判断id为空,添加进数据,
- String getId = UUID.getGuid().toString();
- if(!"".equals(getId) && getId != null) {
- userData.put("id", getId);
- }
最终回写到数据库,效果大概如下: