温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-10-09 21:20:44 来源:网络 阅读:452 作者:乾坤刀 栏目:软件技术

模板抽象类

/**
 * 编码生成器
 */
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("生成客户编码失败");
    }

}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI