Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Template method pattern generation unique number

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report