一 需求:
编写一个接口,生成唯一的一个数值;
二 操作步骤:
2.1 建表
DROP TABLE IF EXISTS `sys_idrule`;
CREATE TABLE `sys_idrule` (
`RULE_ID` varchar(32) NOT NULL,
`RULE_EXP` varchar(32) NOT NULL,
`RULE_TYPE` varchar(16) NOT NULL,
`SEQ_START` bigint(20) NOT NULL,
`SEQ_END` bigint(20) NOT NULL,
`CURRENT_VALUE` bigint(20) NOT NULL,
`LAST_UPDATE` datetime NOT NULL,
PRIMARY KEY (`RULE_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.2 ibatis配置
sys_idrule.xml sqlmap配置: <sqlMap resource="ibatis/sys_idrule.xml" />
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="IdRuleDao">
<typeAlias type="com.yht.wxt.system.po.IdRule" alias="IdRule" />
<select id="find" resultClass="IdRule" parameterClass="java.util.Map">
SELECT
RULE_ID,
RULE_EXP,
RULE_TYPE,
SEQ_START,
SEQ_END,
CURRENT_VALUE,
LAST_UPDATE
FROM SYS_IDRULE
WHERE RULE_ID=#rule_id#
</select>
<update id="update" parameterClass="IdRule">
UPDATE SYS_IDRULE SET
CURRENT_VALUE=#current_value#,
LAST_UPDATE=#last_update#
WHERE RULE_ID=#rule_id#
</update>
</sqlMap>
2.3 dao层接口
public interface IdRuleDao {
/**
*更加ID查询编码规则
*
* @param rule_id
* @return
* @author lenic 2013-5-21
*/
IdRule find(String rule_id);
/**
* 更新编码规则
*
* @param idRule
* @author lenic 2013-5-21
*/
void update(IdRule idRule);
}
2.4 dao层接口实现类
@Repository("idRuleDao")
public class IdRuleDaoImpl extends WxtBaseDao implements IdRuleDao {
@Override
public IdRule find(String rule_id) {
return (IdRule) find("IdRuleDao.find", Collections.singletonMap("rule_id", rule_id));
}
@Override
public void update(IdRule idRule) {
update("IdRuleDao.update", idRule);
}
}
2.5 service层接口
public interface IdRuleService {
/**
* 获取UUID
*
* @return
* @throws SysException
* @author lenic 2013-5-31
*/
String getUUID() throws SysException;
/**
* 获取项目ID
*
* @return
* @throws SysException
* @author lenic 2013-5-21
*/
String getProjectId() throws SysException;
/**
* 获取提现ID
*
* @return
* @throws SysException
* @author lenic 2013-5-21
*/
String getCashId() throws SysException;
/**
* 获取充值ID
*
* @return
* @throws SysException
* @author lenic 2013-5-21
*/
String getRechargeId() throws SysException;
/**
* 获取计划单编码
*
* @return
* @throws SysException
* @author lenic 2013-5-31
*/
String getPlanId() throws SysException;
}
2.6 service层接口实现类
@Transactional
@Service("idRuleService")
public class IdRuleServiceImpl implements IdRuleService {
private Logger logger = Logger.getLogger(getClass());
@Autowired
private IdRuleDao idRuleDao;
public String getUUID() throws SysException {
return UUID.randomUUID().toString().replaceAll("-", "");
}
private static final String PROJECT_ID_CODE = "PROJECT_ID_CODE";
private static final Object PROJECT_ID_LOCK = new Object();
@Override
public String getProjectId() throws SysException {
synchronized (PROJECT_ID_LOCK) {
String id = replaceParams(findIdRule(PROJECT_ID_CODE));
logger.info("获取项目编码[" + id + "]");
return id;
}
}
private static final String CASH_ID_CODE = "CASH_ID_CODE";
private static final Object CASH_ID_LOCK = new Object();
@Override
public String getCashId() throws SysException {
synchronized (CASH_ID_LOCK) {
String id = replaceParams(findIdRule(CASH_ID_CODE));
logger.info("获取提现编码[" + id + "]");
return id;
}
}
private static final String RECHARGE_ID_CODE = "RECHARGE_ID_CODE";
private static final Object RECHARGE_ID_LOCK = new Object();
@Override
public String getRechargeId() throws SysException {
synchronized (RECHARGE_ID_LOCK) {
String id = replaceParams(findIdRule(RECHARGE_ID_CODE));
logger.info("获取充值编码[" + id + "]");
return id;
}
}
private static final String PLAN_ID_CODE = "PLAN_ID_CODE";
private static final Object PLAN_ID_LOCK = new Object();
@Override
public String getPlanId() throws SysException {
synchronized (PLAN_ID_LOCK) {
String id = replaceParams(findIdRule(PLAN_ID_CODE));
logger.info("获取计划单编码[" + id + "]");
return id;
}
}
private IdRule findIdRule(String rule_id) {
IdRule idRule = idRuleDao.find(rule_id);
Assert.notNull(idRule, "编码规则[" + rule_id + "]不存在");
if (RuleType.AUTO.name().equals(idRule.getRule_type())) {
idRule.setLast_update(DateUtil.getCurrentTime());
Assert.notNull(idRule.getCurrent_value(), "编码规则[" + rule_id + "]沒有當前值");
idRule.setCurrent_value(idRule.getCurrent_value() + 1);
idRuleDao.update(idRule);
} else {
throw new SysException("非法的编码类型");
}
return idRule;
}
private String replaceParams(IdRule idRule) {
String exp = idRule.getRule_exp();
exp = exp.replaceAll("#DATETIME#", DateUtil.formatCurrentTime("yyyyMMddHHmmss"));
int i = 10;
while (i-- > 0) {
exp = exp.replaceAll("#SEQ" + i + "#", StringUtils.leftPad(String.valueOf(idRule.getCurrent_value()), i, '0'));
}
return exp;
}
}
三 测试:
main方法里:
IdRuleService idRuleService= (idRuleService)getBean("numGeneraterServ");
String uuid = idRuleService.getUUID();
String project_id= idRuleService.getProjectId();
String cash_id= idRuleService.getCashId();
String recharge_id = idRuleService.getRechargeId();
String plan_id = idRuleService.getPlanId();
表数据查看: