资源描述:
《Hibernae基于注解方式的主键生成策略》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、MySQL主键生成方式小结一、使用native(自然)生成方式♦配置如F:@Id©GeneratedValue(generator=H_nativeu)@GenericGenerator(name=T,_nativenrstrategy="native11)SColumn(name=T,uuidn)publicLonggetld(){returnid;♦效果如下:hysql>select*fromtab_users;iuser_id!user_nane!1icareers!2icareers02!2rowsinset<0.00sec>二、使用uuid生成方式♦配置如下:
2、@Id©GeneratedValue(generator=rf_uuid,1)©GenericGenerator(name=n_uuidnrstrategy="uuid")©Column(name=f,user_idf,)publicStringgetld(){returnid;♦效果如下:mysql>select*fromtab_users;iuser_id!user_nane!!8a8a0ffb27flbb540127flbb56bc0001!careers!!8a8a0ffb27flbb540127flbb56cc0002!careers02!2rowsinset
3、<0.01sec>三、使用hilo(高低值)生成方式♦配置如下:@Id©GeneratedValue(generator=n_hilof,)QGenericGenerator(name=n_hilonfstrategy=nhilofl)©Column(name=nuser_idf,)publicLonggetld(){returnid;}♦效果如下:
4、ni;sql>select*fromtab_users;iuser_idiuser_namei1icareersi2icareers02i12rowsinset<0.00sec>四、使用assigned(手工指定)生成方式
5、♦配置如下:@Id@GeneratedValue(generator=n_assignedf,)@GenericGenerator(name=If_assigneduzstrategy="assigned")@Colunm(name="useridTI)publicLonggetld(){returnid;}♦效果如下nysql>select*trontah_users;++—+iuser_id!user_nanei■++—+■10011careersi■■10021careers02i■++—+2rowsinset<0.00sec>
6、五、使用identity牛:成方式
7、♦注解如F:@Id@GeneratedValue(generator=fl_identityff)SGenericGenerator(name=If_identityT,fstrategy="identity")QColunm(name="userid11)publicLonggetId(){returnid;}♦效果如下:mysql>select*fromtab_users;+++■user_id■user_nane■+++!1!careers!■2Icareers02!+++2rowsinset<0.00sec>六、使用sequence(序列)牛.成方式♦注解如F:
8、@Id@GeneratedValue(generator=,f_sequencen)@GenericGenerator(name=fl_sequenceTI,strategy=T,sequenceT,)@Column(name=f,user_id,f)publicLonggetld(){returnid;}♦效果如下:注:MySQL不支持序列主键七、使用increment(自增长方式)♦注解如下:@Id@GeneratedValue(generator=n_increment,f)@GenericGenerator(name=r,_incrementf,rstrateg
9、y=nincrementn)©Column(name=ffuser_idf,)publicLonggetld(){returnid;♦效杲如下:八、使用guid生成方式♦注解如下:@Id@GeneratedValue(generator=n_guidn)@GenericGenerator(name=f,_guidr,,strategy="guid")@Colunm(name=f,user_idf,)publicStringgetld(){returnid;}hysql>select*fromtab_users;iuser_idiuser_