温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java数据库唯一id生成工具类

发布时间:2020-10-11 20:53:51 来源:脚本之家 阅读:186 作者:Tlimited 栏目:编程语言

本文实例为大家分享了java数据库唯一id生成工具类的具体代码,供大家参考,具体内容如下

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

public class UniqueIdUtil
{
 private static long adjust = 1L;
 private static long nextId = 0L;
 private static long lastId = -1L;
 private static long randomLeng = 6L;
 private static JdbcTemplate jdbcTemplate;
 private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
 
 private static void init()
 {
  try
  {
   jdbcTemplate = (JdbcTemplate)AppUtil.getBean("jdbcTemplateSN");
   String str2 = AppConfigUtil.get("genId.adjust");
   if (str2 != null) {
    adjust = Integer.parseInt(str2);
   }
  }
  catch (Exception localException)
  {
   adjust = 1L;
   String str3 = AppConfigUtil.get("genId.adjust");
   if (str3 != null) {
    adjust = Integer.parseInt(str3);
   }
  }
 }
 
 private static void getNextIdBlock()
 {
  if (jdbcTemplate == null) {
   init();
  }
  Long localLong = Long.valueOf(-1L);
  Integer localInteger = Integer.valueOf(-1);
  String str1 = "SELECT bound,incremental FROM SYS_DB_ID T WHERE T.ID=?";
  String str2 = "UPDATE SYS_DB_ID SET BOUND=? WHERE ID=?";
  try
  {
   Map localMap = jdbcTemplate.queryForMap(str1, new Object[] { Long.valueOf(adjust) });
   localLong = Long.valueOf(Long.parseLong(localMap.get("bound").toString()));
   localInteger = Integer.valueOf(Integer.parseInt(localMap.get("incremental").toString()));
   nextId = localLong.longValue();
   lastId = localLong.longValue() + localInteger.intValue();
   jdbcTemplate.update(str2, new Object[] { Long.valueOf(lastId), Long.valueOf(adjust) });
  }
  catch (EmptyResultDataAccessException localEmptyResultDataAccessException)
  {
   insertNewComputer();
  }
 }
 
 private static void insertNewComputer()
 {
  try
  {
   lastId = 10000L;
   String str = "INSERT INTO SYS_DB_ID (id,incremental,bound) VALUES(" + adjust + ",10000," + lastId + ")";
   jdbcTemplate.update(str);
  }
  catch (Exception localException)
  {
   localException.printStackTrace();
  }
 }
 
 public static synchronized long genId()
 {
  if (lastId <= nextId) {
   getNextIdBlock();
  }
  long l = nextId++;
  return l + adjust * 10000000000000L;
 }
 
 public static final String getGuid()
 {
  UUID localUUID = UUID.randomUUID();
  return localUUID.toString();
 }
 
 public static final String getRandom()
 {
  Random localRandom = new Random();
  String str1 = "";
  for (int i = 0; i < randomLeng; i++)
  {
   String str2 = String.valueOf(localRandom.nextInt(10));
   str1 = str1 + str2;
  }
  return str1;
 }
 
 public static String genOrderNo()
 {
  long l = genId();
  String str = dateFormat.format(new Date());
  return str + l;
 }
 
 public static void main(String[] paramArrayOfString)
  throws Exception
 {}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI