// SKU码:系列前3位+6位年月日+3位序号(自动生产,取数据库中当天最大的,没有就赋值位001) // 订单编号:BRD+6位年月日+5位序号 // // 退单号:BRT+6位年月日+3位序号 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:/spring/applicationContext-dao.xml") public class CodeGenerateServiceImplTest { private final Logger logger = Logger.getLogger(this.getClass()); @Autowired private CodeGenerateMapper codeGenerateMapper; @Test public void insertOne() throws Exception { BMUtil bmUtil = new BMUtil(); //参数s String firstCode = "BRG"; String generateType = bmUtil.BRG; // String generateType = "BRG"; int indexSize = 3; //参数e String zero = ""; for (int i = 0; i < indexSize; i++) { zero += "0"; } Date dt = new Date(); SimpleDateFormat matter1 = new SimpleDateFormat("yyyyMMdd"); String secondCode = matter1.format(dt); // 当天的日期六位格式化 CodeGenerate codeGenerate = new CodeGenerate(); codeGenerate.setFirstCode(firstCode); codeGenerate.setSecondCode(secondCode); codeGenerate.setGenerateType(generateType); String maxLastCode = codeGenerateMapper.getMaxLastCode(codeGenerate); String newLastCode = ""; if (maxLastCode != "" && maxLastCode != null) { int maxLastCodeNum = Integer.valueOf(maxLastCode); maxLastCodeNum++; Format f = new DecimalFormat(zero); newLastCode = f.format(maxLastCodeNum); System.out.println(newLastCode); } else { int num = 1; Format ff = new DecimalFormat(zero); newLastCode = ff.format(num); System.out.println(newLastCode); } codeGenerate.setLastCode(newLastCode); // 插入数据库 int t = codeGenerateMapper.insertOne(codeGenerate); //通过插入后返回的主见,查询编码信息 System.out.println(codeGenerate.getId()); List<CodeGenerate> codeGenerateList = codeGenerateMapper.getCodeGenerateById(codeGenerate.getId()); String bm = codeGenerateList.get(0).getFirstCode() + codeGenerateList.get(0).getSecondCode() + codeGenerateList.get(0).getLastCode(); logger.info(JSON.toJSONStringWithDateFormat(codeGenerateList, "yyyy-MM-dd HH:mm:ss")); System.out.println(bm); }