java数据库唯一id生成工具类
本文实例为大家分享了java数据库唯一id生成工具类的具体代码,供大家参考,具体内容如下
importjava.io.File; importjava.text.SimpleDateFormat; importjava.util.Date; importjava.util.Map; importjava.util.Random; importjava.util.UUID; importorg.springframework.dao.EmptyResultDataAccessException; importorg.springframework.jdbc.core.JdbcTemplate; publicclassUniqueIdUtil { privatestaticlongadjust=1L; privatestaticlongnextId=0L; privatestaticlonglastId=-1L; privatestaticlongrandomLeng=6L; privatestaticJdbcTemplatejdbcTemplate; privatestaticSimpleDateFormatdateFormat=newSimpleDateFormat("yyyyMMddHHmmss"); privatestaticvoidinit() { try { jdbcTemplate=(JdbcTemplate)AppUtil.getBean("jdbcTemplateSN"); Stringstr2=AppConfigUtil.get("genId.adjust"); if(str2!=null){ adjust=Integer.parseInt(str2); } } catch(ExceptionlocalException) { adjust=1L; Stringstr3=AppConfigUtil.get("genId.adjust"); if(str3!=null){ adjust=Integer.parseInt(str3); } } } privatestaticvoidgetNextIdBlock() { if(jdbcTemplate==null){ init(); } LonglocalLong=Long.valueOf(-1L); IntegerlocalInteger=Integer.valueOf(-1); Stringstr1="SELECTbound,incrementalFROMSYS_DB_IDTWHERET.ID=?"; Stringstr2="UPDATESYS_DB_IDSETBOUND=?WHEREID=?"; try { MaplocalMap=jdbcTemplate.queryForMap(str1,newObject[]{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,newObject[]{Long.valueOf(lastId),Long.valueOf(adjust)}); } catch(EmptyResultDataAccessExceptionlocalEmptyResultDataAccessException) { insertNewComputer(); } } privatestaticvoidinsertNewComputer() { try { lastId=10000L; Stringstr="INSERTINTOSYS_DB_ID(id,incremental,bound)VALUES("+adjust+",10000,"+lastId+")"; jdbcTemplate.update(str); } catch(ExceptionlocalException) { localException.printStackTrace(); } } publicstaticsynchronizedlonggenId() { if(lastId<=nextId){ getNextIdBlock(); } longl=nextId++; returnl+adjust*10000000000000L; } publicstaticfinalStringgetGuid() { UUIDlocalUUID=UUID.randomUUID(); returnlocalUUID.toString(); } publicstaticfinalStringgetRandom() { RandomlocalRandom=newRandom(); Stringstr1=""; for(inti=0;i以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。