public static void main( String[] args ) throws Exception {
//创建配置文件并且向配置文件中写入字符串的分隔符
Properties props = new Properties();props.setProperty("separator", "/");
//实现分隔符号的设置,默认为“”,下面不写当面设置无效
IdentifierGenerator gen = new UUIDHexGenerator();( (Configurable) gen ).configure(Hibernate.STRING, props, null);
IdentifierGenerator gen2 = new UUIDHexGenerator();
( (Configurable) gen2 ).configure(Hibernate.STRING, props, null);
//遍历打印获得的id 主键生成策略 getIP() + getJVM() + getHiTime() +getLoTime() + getCount()
/*
单机情况下只有getJVM() + getLoTime()这两项发生变化
getCount() 每次都是从1开始计数
*/
for ( int i=0; i<10; i++) {
String id = (String) gen.generate(null, null);System.out.println(id);
String id2 = (String) gen2.generate(null, null);
System.out.println(id2);
}
}
本文探讨了Java中使用UUIDHexGenerator作为主键生成策略的实现方式,并通过实例展示了如何设置分隔符及生成ID的过程。重点介绍了单机环境下ID生成的变化特性。
916

被折叠的 条评论
为什么被折叠?



