第一篇文章[url]http://numen06.iteye.com/blog/1420694[/url]介绍了,流水号的生成,可惜忘了加入循环操作,比如日循环,月循环,年循环,这次来补上。
注入方法已经在一写过了,就不写了。主要是代码部分。
直接上代码
package com.wesley.framework.dao;
import java.text.ParseException;
import java.util.GregorianCalendar;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
/**
* 流水号参数封装类
*
* @author it.zl
*
*/
public class PrimaryBean {
// select 'SP-' as prefix,'yyyyMMdd' as dataString,'000000' startNumber
public final static String YEAR = "year";
public final static String MONTH = "month";
public final static String DAY = "day";
/**
* 表示流水号中的前缀,比如测试操作可以加'TETS-'那么流水号的前缀就会加上'TEST-201207250000001'
*/
private String prefix;
/**
* 表示日期的格式如'yyyyMMdd',那么会生产流水号中的日期为'20120725','yyyy-MM-dd'则会是'2012-07-25'
*/
private String dataString;
/**
* 开始第一个数是多少,定义长度和其实数值,如000000
*/
private String startNumber;
/**
* 表示流水号中的日期,如20120725
*/
private String dateTimeString;
/**
* 可以设置循环类型,如每日生成新的流水号从0开始,默认为日,循环 数值为year,month,day
*/
private String repeatCycle = DAY;
/**
* 是否每次都从数据库验证
*/
private Boolean isDataBase = false;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getDataString() {
return dataString;
}
public void setDataString(String dataString) {
this.dataString = dataString;
}
public String getStartNumber() {
return startNumber;
}
public void setStartNumber(String startNumber) {
this.startNumber = startNumber;
}
public Boolean getIsDataBase() {
return isDataBase;
}
public void setIsDataBase(Boolean isDataBase) {
this.isDataBase = isDataBase;
}
public String getRepeatCycle() {
return repeatCycle;
}
public void setRepeatCycle(String repeatCycle) {
this.repeatCycle = repeatCycle;
}
public String getDateTimeString() {
return dateTimeString;
}
public void setDateTimeString(String dateTimeString) {
this.dateTimeString = dateTimeString;
}
/**
* 把dateTimeString转换正GregorianCalendar
*
* @return DateTime
*/
public GregorianCalendar getDateTime() {
if (StringUtils.isEmpty(dateTimeString))
return new GregorianCalendar();
GregorianCalendar date = new GregorianCalendar();
try {
date.setTime(DateUtils.parseDate(this.getDateTimeString(),
new String[] { this.getDataString() }));
} catch (ParseException e) {
return new GregorianCalendar();
}
return date;
}
}
package com.wesley.framework.dao;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import com.wesley.framework.dao.dbutils.DBHelper;
/**
* 流水号生成类
*
* @author it.zl
*
*/
public class PrimaryGenerater {
/**
* 单例
*/
private static PrimaryGenerater primaryGenerater = null;
/**
* 数据库访问类
*/
private DBHelper dbHelper;
/**
* 生成流水号的SQL,支持多种格式,如业务BIZ,非业务NOBIZ
*/
private Map sqls = new HashMap();
/**
* 对应不用的SQL生产的不同参数类
*/
private Map primarBeans = new HashMap();
private PrimaryGenerater() {
super();
}
public PrimaryGenerater(BasicDataSource database, Map sqls) {
super();
this.dbHelper = new DBHelper(database);
this.sqls = sqls;
for (String key : sqls.keySet()) {
this.primarBeans.put(key, this.getPrimaryBeanByDatabase(key));
}
}
public static PrimaryGenerater newInstance(BasicDataSource database,
Map 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.findFirst(PrimaryBean.class, 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(isRestart(primaryBean),
primaryBean.getStartNumber());
primaryBean.setStartNumber(nextNumber);
String dataString = this.geneterDataString(primaryBean.getDataString());
primaryBean.setDateTimeString(dataString);
String serialNumber = primaryBean.getPrefix() + dataString + nextNumber;
return serialNumber;
}
private synchronized Boolean isRestart(PrimaryBean primaryBean) {
GregorianCalendar gcNow = new GregorianCalendar();
GregorianCalendar date = primaryBean.getDateTime();
if (StringUtils.equalsIgnoreCase(PrimaryBean.YEAR,
primaryBean.getRepeatCycle())) {
if (gcNow.get(GregorianCalendar.YEAR) == date
.get(GregorianCalendar.YEAR))
return false;
}
if (StringUtils.equalsIgnoreCase(PrimaryBean.MONTH,
primaryBean.getRepeatCycle())) {
if (gcNow.get(GregorianCalendar.YEAR) == date
.get(GregorianCalendar.YEAR)
&& gcNow.get(GregorianCalendar.MONTH) == date
.get(GregorianCalendar.MONTH))
return false;
}
if (StringUtils.equalsIgnoreCase(PrimaryBean.DAY,
primaryBean.getRepeatCycle())) {
if (DateUtils.isSameDay(gcNow, date))
return false;
}
return true;
}
/**
* 通过开始数字字符串生成下一个流水号
*
* @param startNumber
* @return
*/
public synchronized String geneterNextNumber(Boolean isRestart,
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 isRestart ? sb.toString() : df.format(temp);
}
/**
* 通过 格式生成日期格式
*
* @param dataformat
* @return
*/
private synchronized String geneterDataString(String dataformat) {
SimpleDateFormat formatter = new SimpleDateFormat(dataformat);
return formatter.format(new Date());
}
}
package com.wesley.framework.dao.dbutils;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.wesley.framework.commen.StringFormat;
public class DBHelper {
private static final Log logger = LogFactory.getLog(DBHelper.class);
private BasicDataSource dataSource;
private QueryRunner queryRunner;
public DBHelper(BasicDataSource dataSource) {
super();
this.dataSource = dataSource;
}
public void setDataSource(BasicDataSource dataSource) {
this.dataSource = dataSource;
}
/**
* 执行sql语句
*
* @param sql
* sql语句
* @return 受影响的行数
*/
public int update(String sql) {
return update(sql, null);
}
/**
* 执行sql语句
* executeUpdate("update user set username = 'kitty' where username = ?", "hello kitty");
*
*
* @param sql
* sql语句
* @param param
* 参数
* @return 受影响的行数
*/
public int update(String sql, Object param) {
return update(sql, new Object[] { param });
}
/**
* 执行sql语句
*
* @param sql
* sql语句
* @param params
* 参数数组
* @return 受影响的行数
*/
public int update(String sql, Object[] params) {
queryRunner = new QueryRunner(dataSource);
int affectedRows = 0;
try {
if (params == null) {
affectedRows = queryRunner.update(sql);
} else {
affectedRows = queryRunner.update(sql, params);
}
} catch (SQLException e) {
logger.error("Error occured while attempting to update data", e);
}
return affectedRows;
}
/**
* 执行批量sql语句
*
* @param sql
* sql语句
* @param params
* 二维参数数组
* @return 受影响的行数的数组
*/
public int[] batchUpdate(String sql, Object[][] params) {
queryRunner = new QueryRunner(dataSource);
int[] affectedRows = new int[0];
try {
affectedRows = queryRunner.batch(sql, params);
} catch (SQLException e) {
logger.error("Error occured while attempting to batch update data",
e);
}
return affectedRows;
}
/**
* 执行查询,将每行的结果保存到一个Map对象中,然后将所有Map对象保存到List中
*
* @param sql
* sql语句
* @return 查询结果
*/
public List