日期:2014-05-16 浏览次数:20443 次
package com.wesley.framework.dao; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.dbcp.BasicDataSource; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import com.wesley.framework.dao.dbutils.DBHelper; public class PrimaryGenerater { private static PrimaryGenerater primaryGenerater = null; private DBHelper<PrimaryBean> dbHelper; private Map<String, String> sqls = new HashMap<String, String>(); private Map<String, PrimaryBean> primarBeans = new HashMap<String, PrimaryBean>(); private PrimaryGenerater() { super(); } public PrimaryGenerater(BasicDataSource database, Map<String, String> sqls) { super(); this.dbHelper = new DBHelper<PrimaryBean>(database, PrimaryBean.class); this.sqls = sqls; for (String key : sqls.keySet()) { this.primarBeans.put(key, this.getPrimaryBeanByDatabase(key)); } } public static PrimaryGenerater newInstance(BasicDataSource database, Map<String, String> sqls) { synchronized (PrimaryGenerater.class) { primaryGenerater = new PrimaryGenerater(database, sqls); } return primaryGenerater; } /** * * 取得PrimaryGenerater的单例实现 * * @return */ public static PrimaryGenerater getInstance() { if (primaryGenerater == null) { synchronized (PrimaryGenerater.class) { if (primaryGenerater == null) { primaryGenerater = new PrimaryGenerater(); } } } return primaryGenerater; } /** * 通过 数据库查询键获得封装类 * * @param key * @return */ public synchronized PrimaryBean getPrimaryBeanByDatabase(String key) { if (!this.sqls.containsKey(key)) return null; PrimaryBean primaryBean = this.primarBeans.get(key); if (primaryBean != null && !primaryBean.getIsDataBase()) return primaryBean; primaryBean = dbHelper.query(this.sqls.get(key)); return primaryBean; } /** * 通过数据库查询键位生成流水号 * * @param key * @return */ public synchronized String geneterNextNumberByKey(String key) { PrimaryBean primaryBean = this.getPrimaryBeanByDatabase(key); return this.geneterNextNumber(primaryBean); } /** * 通过封装类生成流水号 * * @param primaryBean * @return */ public synchronized String geneterNextNumber(PrimaryBean primaryBean) { String nextNumber = this .geneterNextNumber(primaryBean.getStartNumber()); primaryBean.setStartNumber(nextNumber); String dataString = this.geneterDataString(primaryBean.getDataString()); String serialNumber = primaryBean.getPrefix() + dataString + nextNumber; return serialNumber; } /** * 通过开始数字字符串生成下一个流水号 * * @param startNumber * @return */ public synchronized String geneterNextNumber(String startNumber) { Long temp = Long.valueOf(startNumber) + 1; StringBuilder sb = new StringBuilder(); for (int i = 0; i < startNumber.length(); i++) sb.append("0"); DecimalFormat df = new DecimalFormat(sb.toString()); return df.format(temp); } /** * 通过 格式生成日期格式 * * @param dataformat * @return */ private synchronized String geneterDataString(String dataformat) { SimpleDateFormat formatter = new SimpleDateFormat(dataformat); return formatter.format(new Date()); } public static void main(String[] args) throws Exception { XmlBeanFactory context = new XmlBeanFactory(new ClassPathResource( "applicationContext.xml")); BasicDataSource dataSource = (BasicDataSource) context .getBean("dataSource"); Map<String, String> map = new HashMap<String, String>(); map.put("new", "select 'SP-' as prefix,'yyyyMMdd' as dataString,'000000' as startNumber from business union select 'SP-' as prefix,'yyyyMMdd' as dataString,'000000' as startNumber"); PrimaryGenerater.newInstance(dataSource, map); for (int i = 0; i < 100; i++) { System.out.print