前言
在工作中经常会遇到使用32位的uuid作为数据库中的主键id情况,这里记录一下分别使用python和java来生成uuid的方法!
python生成uuid
# coding:utf-8
import uuid
def get_uuid():
"""获取uuid"""
return str(uuid.uuid1()).replace("-", "").upper()
if __name__ == "__main__":
uid = get_uuid()
print("uuid>>>:{}".format(uid))
java生成uuid
import java.util.UUID;
/*
获取uuid
*/
public class CommonUtils {
public static void main(String[] args) {
String uid = getUUID();
System.out.println("uid>>> " + uid);
}
public static String getUUID() {
// 链式调用
return UUID.randomUUID().toString().replace("-", "").toUpperCase();
}
}