idgenerator.java
来自「使用WEBWORK,SPRING,HIBERNATE编写的简单的添加」· Java 代码 · 共 70 行
JAVA
70 行
/**
*文件功能:
*/
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-21
*/
public class IdGenerator {
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 static String getId(){
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 String get
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<40;i++){
System.out.println(IdGenerator.getId());
System.out.println(IdGenerator.getId());
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?