首页 > 代码库 > 编号的生成(日期+序列号)
编号的生成(日期+序列号)
经常会用到编号的生成,这边整理一下:
先看controller方法:
package com.simpleframework.bjno.controller; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/serial") public class SerialController { @Autowired private SerialService serialService; /** * 获取编号 * * @param model * @param request * @return */ @ResponseBody @RequestMapping("/getSipSdNo") public InvokeResult getSipSdNo( ) { try { Map returnResult = new HashMap(); String dateString=serialService.getDateString(); int sdNo = serialService.getInfSipSdNo(); String serialNum = dateString + String.format("%04d", sdNo); returnResult.put("serialNo", serialNum); serialService.save(String.valueOf(sdNo),""); return InvokeResult.success(returnResult); } catch (Exception e) { return InvokeResult.failure(e.getMessage()); } } }
再看service,sql语句写在service中:
package com.bjno.service.serviceImpl; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import com.simpleframework.bjno.model.InfApplyNo; import com.simpleframework.bjno.service.SerialService; import com.simpleframework.core.repo.RepositoryFactory; import com.simpleframework.core.repo.ServiceSupport; import com.simpleframework.core.repo.jpa.spi.SingleEntityManager; @Service @Transactional public class SerialServiceImpl extends ServiceSupport implements SerialService { private SingleEntityManager<InfApplyNo> internal = RepositoryFactory.get().build(InfApplyNo.class); @Override public void save(String applyNo,String sdNo) { InfApplyNo infApplyNo = new InfApplyNo(); infApplyNo.setSdNo(StringUtils.isEmpty(sdNo)?"":sdNo); infApplyNo.setCreateDate(new Date()); infApplyNo.setAppSdNo(StringUtils.isEmpty(applyNo)?"":applyNo); // infApplyNo.setId(null); internal.save(infApplyNo); } @Override public int getInfSipSdNo() { String sql = "select max(to_number(APP_SD_NO)) " + "from INF_APPLY_NO t " + "where to_char(CREATE_DATE,‘YYYY-MM-DD‘)= to_char(sysdate,‘YYYY-MM-DD‘) " + "and APP_SD_NO is not null "; Map<String, Object> param = new HashMap<>(); List<Object[]> list = nativeQuery().setSql(sql).list(); int sdNo; if (list != null && list.size() > 0 && list.get(0) != null) { sdNo = Integer.parseInt(String.valueOf(list.get(0))) + 1; } else { sdNo = 1; } return sdNo; } @Override public String getYwNo(String deptQlId, String deptYwNum) { String sql = "select max(DEPT_YW_REG_NO) " + "from DEPT_YW_INF t " + "where DEPT_QL_ID=:deptQlId and DEPT_YW_NUM=:deptYwNum "; String serialNo = ""; Map<String, Object> param = new HashMap<>(); param.put("deptQlId", deptQlId); param.put("deptYwNum", deptYwNum); List<Object[]> list = nativeQuery().setSql(sql).setParam(param).list(); if (list != null && list.size() > 0 && list.get(0) != null) { serialNo = String.valueOf(list.get(0)); } return serialNo; } @Override public String getSipYwNo(String deptQlId, String deptYwNum) { String sql = "select max(YW_NAME_DESC) " + "from DEPT_YW_INF t " + "where DEPT_QL_ID=:deptQlId and DEPT_YW_NUM=:deptYwNum "; String serialNo = ""; Map<String, Object> param = new HashMap<>(); param.put("deptQlId", deptQlId); param.put("deptYwNum", deptYwNum); List<Object[]> list = nativeQuery().setSql(sql).setParam(param).list(); if (list != null && list.size() > 0 && list.get(0) != null) { serialNo = String.valueOf(list.get(0)); } return serialNo; } @Override public String getDateString() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); String dateString = formatter.format(currentTime); return dateString; } }
编号的生成(日期+序列号)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。