📄 dategenerator.java
字号:
/**
*文件功能:
*/
package com.common.hibernate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.Configurable;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.type.Type;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Properties;
import com.common.util.StringUtil;
/**
* @作者 徐建协
* @日期 2008-1-19
*/
public class DateGenerator implements IdentifierGenerator {
private Log log = LogFactory.getLog(DateGenerator.class);
public static String preDateTime = "";
public static String currentDateTime = "";
public static int seq = 0;
public static byte[] lock = new byte[0];
public DateGenerator() {
}
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
currentDateTime = StringUtil.getCurDate("yyyyMMddHHmmss");
String res = "";
synchronized (lock) {
if (currentDateTime.equals(preDateTime)) {
seq++;
if (seq < 10) {
res = currentDateTime + "000" + seq;
} else if (seq >= 10 && seq < 100) {
res = currentDateTime + "00" + seq;
} else if (seq >= 100 && seq < 1000) {
res = currentDateTime + "0" + seq;
} else if (seq >= 1000 && seq < 10000) {
res = currentDateTime + seq;
}else{
//throw new Exception("generante error");
}
} else {
res = currentDateTime + "0001";
seq = 1;
}
preDateTime=currentDateTime;
}
return res;
}
public void configure(Type type, Properties params, Dialect dialect)
throws MappingException {
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -