模板方法模式生成唯一编号

模板抽象类

/**
 * 编码生成器
 */
public abstract class AbstractNumberGenerator {

    private int leastSerialNoLength = 6;

    /**
     * 生成编码
     * @return
     */
    public String generateNumber() throws Exception {
        return new StringBuilder().append(getPrefix()).append(getBusinessCode()).append(getWrapperSerialNo()).toString();
    }

    /**
     * 前缀
     * @return
     */
    public abstract String getPrefix();

    /**
     * 业务码
     * @return
     */
    public abstract String getBusinessCode();

    /**
     * 顺序号
     * @return
     */
    public abstract int getSerialNo() throws Exception ;

    /**
     * 顺序号最少位数
     * @param length
     */
    protected void setLeastSerialNo(int length){
        this.leastSerialNoLength = length;
    }

    /**
     * 顺序号不足补位
     * @return
     */
    protected String getWrapperSerialNo() throws Exception {
        String format = "%0"+ leastSerialNoLength +"d";
        return String.format(format, getSerialNo());
    }
}

数据库实现顺序号

/**
 * 通过数据库的索引机制,建表生成相关数据
 */
public class CustomerNumberCounterGenerator extends AbstractNumberGenerator {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerNumberCounterGenerator.class);

    @Override
    public String getPrefix() {
        return "K";
    }

    @Override
    public String getBusinessCode() {
        return DateUtils.format(new Date(), "yyMMdd");
    }

    /**
     * 获取顺序号。注:数据库建立的唯一索引机制,获取多并发的情况会报错,因此尝试第二次获取。
     * @return
     * @throws Exception
     */
    @Override
    public int getSerialNo() throws Exception {
        int serialNo = Integer.MIN_VALUE;
        try {
            serialNo = getCustomSerialNo();
        } catch (Exception e) {
            try {
                serialNo = getCustomSerialNo();
            } catch (Exception e1) {
                throw new Exception("getSerialNo is failed.", e1);
            }
        }
        return serialNo;
    }

    public int getCustomSerialNo() throws Exception {
        String prefix = getPrefix();
        String businessCode = getBusinessCode();
        CustomerNumberCounter numberCounter = CustomerNumberCounter.findFirst(CustomerNumberCounter.class, "prefix=? and timeCode=?", new Object[]{prefix, businessCode});
        if (null == numberCounter){
            numberCounter = new CustomerNumberCounter();
            numberCounter.setPrefix(prefix);
            numberCounter.setTimeCode(businessCode);
            numberCounter.setSerialNo(1);
            numberCounter.create();
            return 1;
        }
        try {
            BaseDomain.beginTransaction(CustomerNumberCounter.class);
            int execute = BaseDomain.execute(BaseDomain.class, "update t_customer_number_counter set serialNo = serialNo+1 where id = ?", new Object[]{numberCounter.getId()});
            if (execute != 1)
                throw new IllegalAccessException("更新计数器报错");
            return CustomerNumberCounter.find(CustomerNumberCounter.class, numberCounter.getId()).getSerialNo();
        }catch (Exception e){
            BaseDomain.rollback(CustomerNumberCounter.class);
            if (LOGGER.isErrorEnabled())
                LOGGER.error("to generate customer number occur error.", e);
        }finally {
            if (BaseDomain.getCurrentTransaction(CustomerNumberCounter.class) != null)
                BaseDomain.commit(CustomerNumberCounter.class);
        }
        throw new IllegalAccessException("生成客户编码失败");
    }

}

网页标题:模板方法模式生成唯一编号
链接URL:http://azwzsj.com/article/gdpchs.html