java
优点:可以根据要求在键前添加指定字符,例如键要添加以公司缩写的字符
缺点:严重依赖时间,如果时间回拨,并且数据量大会在那一秒钟凉凉
推荐用stringBuffer进行字符的增加(这里就不写了)
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
@Component
public class IdVoluntarilyConfig {
public static String voluntarilyId(){
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmssSSS");
String dateString = format.format(new Date());
Long time = Long.parseLong(dateString);
Long IdTime = time - 6315786780674848L;//随便填
String Id = IdTime.toString();
Random random = new Random();
int a = random.nextInt(10);
for (int i = 0; i < 4; i++ ) {
Id = Id + random.nextInt(10);
}
System.out.println(Id);
return Id;
}
}
vue
//uuid型
uuid() {
var s = [];
var hexDigits = '0123456789qwertyuiopasdfghjklzxcvbnm';
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x24), 1);
}
s[14] = '4';
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
s[8] = s[13] = s[18] = s[23] = '-';
let uuid =''
for(var i = 0; i < 36; i++){
uuid+=s[i]
}
let uuidA = s.join('');
console.log(uuid);
return uuidA;
},
//时间戳型
timeId(){
let date = formatDate(new Date(),"yyyyMMddhhmmss")
let tm= Math.floor(date);
tm = tm -6315786780674
let date2= tm+''
let date1 =new Date().getMilliseconds();
date2 = date2+date1;
let flo = Math.floor(Math.random()*10);
let flr = Math.floor(Math.random()*10);
date = date2+flo+flr
return date
},
//formatDate
export default function formatDate(date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
};
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + '';
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
}
}
return fmt;
}
前端一般都是做临时键、展示键值等使用,不要当主键用