在平时开发时,如果没有定义数据库自动生成ID的机制,可以使用java自带的uuid生成随机唯一主键,在插入数据库时,可以直接给ID主键赋值;这个uuid方法,在java自带的utils包里,就可以直接调用:
示例:
public static String getUUID(){
UUID uuid=UUID.randomUUID();
String uuidStr=uuid.toString();
return uuidStr;
}
=======注意=======
UUID随机主键的长度为36位,在数据库表设计时,注意为UUID存储列,分配足够的长度