怎样生成唯一订单号
比如格式为:
yyyy-MM-dd + 00000001,
yyyy-MM-dd + 00000002,
yyyy-MM-dd + 00000003,
...
yyyy-MM-dd是订单产生日日期,后跟8位数字,每天都是从00000001开始。
大家都有什么好方法,能保证唯一性和速度。
[解决办法]
从年月日到时间 ,,取到毫秒
[解决办法]
上面说用时间来取唯一值不行吧,如果访问量很大,同一毫秒上来的订单那不是会重复了呢?
比较严谨的做法用一个自动增长的表来产生一个唯一值。
[解决办法]
办法有,有点小麻烦。
首先定义一个工厂类,生成你的订单号序列。
package bean;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.FactoryBean;
public class SequenceFactoryBean implements FactoryBean<String> {
private static long counter = 0;
public synchronized String getObject() throws Exception {
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " + ";
String sequ = new DecimalFormat("00000000").format(counter ++);
return date + sequ;
}
public Class<String> getObjectType() {
return String.class;
}
public boolean isSingleton() {
return false;
}
public static void reset() {
SequenceFactoryBean.counter = 0;
}
}
package job;
import bean.SequenceFactoryBean;
public class SequenceResetJob {
public void execute() {
SequenceFactoryBean.reset();
}
}
<bean id="sequence" class="bean.SequenceFactoryBean" />
<bean id="job" class="job.SequenceResetJob" />
<bean id="jobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="job" />
<property name="targetMethod" value="execute" />
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetail" />
<property name="cronExpression" value="0 0 0 * * ?" />
</bean>
<bean name="quartzScheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
package com.anxin.utils;
import java.io.Serializable;
import java.net.InetAddress;
/**
* 生成类似hibernate中uuid 32位主键序列
*
* @version: V1.0
*/
public class UUIDGenerator {
private static final int IP;
public static int IptoInt(byte[] bytes) {
int result = 0;
for (int i = 0; i < 4; i++) {
result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
}
return result;
}
static {
int ipadd;
try {
ipadd = IptoInt(InetAddress.getLocalHost().getAddress());
} catch (Exception e) {
ipadd = 0;
}
IP = ipadd;
}
private static short counter = (short) 0;
private static final int JVM = (int) (System.currentTimeMillis() >>> 8);
public UUIDGenerator() {
}
public static int getJVM() {
return JVM;
}
public static short getCount() {
synchronized (UUIDGenerator.class) {
if (counter < 0)
counter = 0;
return counter++;
}
}
public static int getIP() {
return IP;
}
public static short getHiTime() {
return (short) (System.currentTimeMillis() >>> 32);
}
public static int getLoTime() {
return (int) System.currentTimeMillis();
}
private final static String sep = "";
public static String format(int intval) {
String formatted = Integer.toHexString(intval);
StringBuffer buf = new StringBuffer("00000000");
buf.replace(8 - formatted.length(), 8, formatted);
return buf.toString();
}
public static String format(short shortval) {
String formatted = Integer.toHexString(shortval);
StringBuffer buf = new StringBuffer("0000");
buf.replace(4 - formatted.length(), 4, formatted);
return buf.toString();
}
public static String generate() {
return String.valueOf(new StringBuffer(36).append(format(getIP())).append(sep)
.append(format(getJVM())).append(sep)
.append(format(getHiTime())).append(sep)
.append(format(getLoTime())).append(sep)
.append(format(getCount())).toString());
}
public static void main(String args[]){
System.out.println(UUIDGenerator.generate());
}
}
UUID.randomUUID().toString().replaceAll("-", "");