@ApiModel(value = "订单")
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Order implements Serializable {
private static final long serialVersionUID = -44480262996409913L;
@Id
@Column(name = "id", nullable = false, length = 100)
@GeneratedValue(generator = GeneratorConst.ID_GENERATOR_NAME)
@GenericGenerator(name = GeneratorConst.ID_GENERATOR_NAME, strategy = GeneratorConst.ID_STRATEGY, parameters = {
@org.hibernate.annotations.Parameter(name = GeneratorConst.ID_SEQ_DATE, value = "yyyyMMdd"),
@org.hibernate.annotations.Parameter(name = GeneratorConst.ID_SEQ_FMT_WIDTH, value = "14"),
@org.hibernate.annotations.Parameter(name = org.hibernate.id.enhanced.TableGenerator.SEGMENT_COLUMN_PARAM, value = "ID"),
@org.hibernate.annotations.Parameter(name = org.hibernate.id.enhanced.TableGenerator.SEGMENT_VALUE_PARAM, value = "oderId"),
@org.hibernate.annotations.Parameter(name = org.hibernate.id.enhanced.TableGenerator.VALUE_COLUMN_PARAM, value = "seq"),
@org.hibernate.annotations.Parameter(name = org.hibernate.id.enhanced.TableGenerator.TABLE_PARAM, value = "tseq"),
@org.hibernate.annotations.Parameter(name = TableGenerator.INCREMENT_PARAM, value = "1000") })
private String orderId;
}
public class GeneratorConst {
public final static String ID_STRATEGY = "com.util.AssignedSequenceGenerator";
public final static String ID_GENERATOR_NAME = "orderGenerator";
public final static String ID_SEQ_FMT_WIDTH = "fmtWidth";
public final static String ID_SEQ_DATE = "dateWidth";
}
import com.ruyicai.edu.constant.GeneratorConst;
import org.apache.commons.lang3.time.FastDateFormat;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.id.enhanced.TableGenerator;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.IntegerType;
import org.hibernate.type.Type;
import java.io.Serializable;
import java.util.Date;
import java.util.Properties;
public class AssignedSequenceGenerator extends TableGenerator implements
PersistentIdentifierGenerator, Configurable {
private String prefix;
private Integer fmtWidth;
private FastDateFormat sdf;
private Integer INCREMENT_PARAM = 0;
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry)
throws MappingException {
super.configure(IntegerType.INSTANCE, params, serviceRegistry);
if (params.containsKey(GeneratorConst.ID_SEQ_FMT_WIDTH))
fmtWidth = new Integer(params.getProperty(GeneratorConst.ID_SEQ_FMT_WIDTH));
if (params.containsKey(GeneratorConst.ID_SEQ_DATE))
sdf = FastDateFormat.getInstance(params.getProperty(GeneratorConst.ID_SEQ_DATE));
if (params.containsKey(TableGenerator.INCREMENT_PARAM))
INCREMENT_PARAM = new Integer(params.getProperty(TableGenerator.INCREMENT_PARAM));
}
@Override
public Serializable generate(SharedSessionContractImplementor session, Object obj)
throws HibernateException {
int seq = (Integer) super.generate(session, obj)+1+INCREMENT_PARAM;
StringBuffer sbf = new StringBuffer();
if (prefix != null) {
sbf.append(prefix);
}
if (sdf != null) {
Date now = new Date();
sbf.append(sdf.format(now));
}
if (fmtWidth != null) {
sbf.append(format(seq, fmtWidth));
} else {
sbf.append(seq);
}
return sbf.toString();
}
private String format(int num, int width) {
if (num < 0)
return "";
StringBuffer sb = new StringBuffer();
String s = "" + num;
if (s.length() < width) {
int addNum = width - s.length();
for (int i = 0; i < addNum; i++) {
sb.append("0");
}
sb.append(s);
} else {
return s.substring(s.length() - width, s.length());
}
return sb.toString();
}
}
CREATE TABLE `tseq` (
`ID` varchar(100) NOT NULL,
`SEQ` bigint(20) DEFAULT NULL,
`MEMO` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;