标签:当前时间 ber session 自定义 uuid for log 生成器 .com
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "custom-id") @GenericGenerator(name = "custom-id", strategy = "com.h5page-mapdiy.utils.CustomIDGenerator")
@Column(name = "id")
public Long getId() {
return id;
}
注意:GeneratedValue中的generator要与GenericGenerator中的name相等 上面代码中是"custom-id"
package com.h5page-mapdiy.utils
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentityGenerator;
import java.io.Serializable;
/**
* 自定义ID生成器
* @author liaoguanwang
* @date 2019-03-16 10:33
*/
public class CustomIDGenerator extends UUIDGenerator {
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws MappingException {
SimpleDateFormat simpleDateFormat;
simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String str = simpleDateFormat.format(date);
Random random = new Random();
int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数
return rannum + str;// 当前时间
}
}
注意:
参考:
Spring boot JPA 用自定义主键策略 生成自定义主键ID
SpringJPA主键生成采用自定义ID,自定义ID采用年月日时间格式
标签:当前时间 ber session 自定义 uuid for log 生成器 .com
原文地址:https://www.cnblogs.com/liaoguanwang/p/10575633.html