溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java隨機(jī)生成10位數(shù)的字符串ID

發(fā)布時(shí)間:2020-08-25 14:02:23 來(lái)源:腳本之家 閱讀:1030 作者:Snow、楊 欄目:編程語(yǔ)言

簡(jiǎn)述

項(xiàng)目中,有種業(yè)務(wù)需要當(dāng)前表中存?zhèn)€另外一個(gè)表中的主鍵ID字段,如果用數(shù)據(jù)庫(kù)自增的話,容易出現(xiàn)重復(fù)

比如A表主鍵ID自增1、2、3.。。。。,B表主鍵ID自增1、2、3.。。。。,A表中有個(gè)字段是B_Id,會(huì)出現(xiàn)a_id=3、b_id=3的情況,傻傻分不清楚這邊主要分享的是一個(gè)隨機(jī)生成10位永不重復(fù)的隨機(jī)字符串,不啰嗦了,下面上代碼

隨機(jī)數(shù)生成工具類

/**
 * MathUtils.java
 * com.prereadweb.utils
 * Copyright (c) 2019, 北京鏈天下科技有限公司版權(quán)所有.
 */
package com.prereadweb.utils;
 
/**
 * @Description: 隨機(jī)數(shù)生成工具類
 * @author: Yangxf
 * @date: 2019/4/14 12:38
 */
public class MathUtils {
 
 private static final String DEFAULT_DIGITS = "0";
 private static final String FIRST_DEFAULT_DIGITS = "1";
 
 /**
  * @param target 目標(biāo)數(shù)字
  * @param length 需要補(bǔ)充到的位數(shù), 補(bǔ)充默認(rèn)數(shù)字[0], 第一位默認(rèn)補(bǔ)充[1]
  * @return   補(bǔ)充后的結(jié)果
  */
 public static String makeUpNewData(String target, int length){
  return makeUpNewData(target, length, DEFAULT_DIGITS);
 }
 
 /**
  * @param target 目標(biāo)數(shù)字
  * @param length 需要補(bǔ)充到的位數(shù)
  * @param add  需要補(bǔ)充的數(shù)字, 補(bǔ)充默認(rèn)數(shù)字[0], 第一位默認(rèn)補(bǔ)充[1]
  * @return   補(bǔ)充后的結(jié)果
  */
 public static String makeUpNewData(String target, int length, String add){
  if(target.startsWith("-")) target.replace("-", "");
  if(target.length() >= length) return target.substring(0, length);
  StringBuffer sb = new StringBuffer(FIRST_DEFAULT_DIGITS);
  for (int i = 0; i < length - (1 + target.length()); i++) {
   sb.append(add);
  }
  return sb.append(target).toString();
 }
 
 /**
  * 生產(chǎn)一個(gè)隨機(jī)的指定位數(shù)的字符串?dāng)?shù)字
  * @param length
  * @return
  */
 public static String randomDigitNumber(int length){
  int start = Integer.parseInt(makeUpNewData("", length));//1000+8999=9999
  int end = Integer.parseInt(makeUpNewData("", length + 1)) - start;//9000
  return (int)(Math.random() * end) + start + "";
 }
}

生成ID工具類

/**
 * IdUtils.java
 * com.prereadweb.utils
 * Copyright (c) 2019, 北京鏈天下科技有限公司版權(quán)所有.
 */
package com.prereadweb.utils;
 
/**
 * @Description: 生成ID工具類
 * @author: Yangxf
 * @date: 2019/4/14 12:40
 */
public class IdUtils {
 
 /**
  * 以毫微秒做基礎(chǔ)計(jì)數(shù), 返回唯一有序增長(zhǎng)ID
  * <pre>System.nanoTime()</pre>
  * <pre>
  * 線程數(shù)量: 100
  * 執(zhí)行次數(shù): 1000
  * 平均耗時(shí): 222 ms
  * 數(shù)組長(zhǎng)度: 100000
  * Map Size: 100000
  * </pre>
  * @return ID長(zhǎng)度32位
  */
 public static String getPrimaryKey(){
  return MathUtils.makeUpNewData(Thread.currentThread().hashCode()+"", 3)+ MathUtils.randomDigitNumber(7);           //隨機(jī)7位數(shù)
 }
}

效果

java隨機(jī)生成10位數(shù)的字符串ID

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI