#需求是数据库生成一个唯一码,格式是规定字符串+当天日期+3位顺序码,自己想了下,写了下面这个工具类
说一下思路: 1.首先要增加一个表,这个表是用来放每个表最新的编码的(表名 business_code_generate) 2、从数据库中查询到这个表中的最新的编码 然后截取后面的顺序码 3、截取到顺序码的数字,给其加1,加1操作在工具类中体现 4、最后拼接上日期和规定字符串生成编码 5.然后将最新的编码更新到这个表中(business_code_generate)
package com.cpeam.controller; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.founder.fix.dbcore.DataTable; import com.founder.fix.dbcore.DataTable.DataRow; import com.founder.fix.webcore.interfaceLayer.adapter.SqlAdapter; import com.google.gson.Gson; @Controller @RequestMapping("/businessCodeGenerateAction") public class SerialNumController { /** * 自动生成编号格式:编码-公司编码-日期序号 如:YJ-1-20191021001 */ @RequestMapping(value = "/createCode") @ResponseBody public String createCode(HttpServletRequest request, String tableName,String typeCode) { Map<String, Object> res = new HashMap<String, Object>(); String sql; if(typeCode.equals("YJ")||typeCode.equals("DT")){ sql = "select new_value from business_code_generate where table_name = '" + tableName + "' AND TABLE_TYPE = '"+typeCode+"'"; }else{ sql = "select new_value from business_code_generate where table_name = '" + tableName + "'"; } DataTable dt; try { dt = (DataTable)SqlAdapter.executeForGetSqlData(sql, "DB_FIX_BIZ_BASE", null); Map<String, Object> map = new HashMap<String, Object>(); if(dt.Rows != null && dt.Rows.length>0){ for(DataRow row:dt.Rows){ map.put("maxRecord", row.ItemValue("new_value")); } } String maxRecord = (String) map.get("maxRecord"); // System.out.println("最大编号:"+list.get(0).get("max_comment_code")); String comment_code = ""; SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); // 时间字符串产生方式 String uid_pfix = format.format(new Date()); // 组合流水号前一部分,时间字符串,如:1601 System.out.println("time=" + format.format(new Date())); if (maxRecord != null && maxRecord.contains(typeCode+"-1-"+uid_pfix)) { String[] array = maxRecord.split("-"); String reoord = array[2]; String uid_end = reoord.substring(8, 11); // 截取字符串最后三位,结果:001 // System.out.println("uid_end=" + uid_end); int endNum = Integer.parseInt(uid_end); // 把String类型的0001转化为int类型的1 // System.out.println("endNum=" + endNum); int tmpNum = 1000 + endNum + 1; // 结果1002 // System.out.println("tmpNum=" + tmpNum); comment_code = typeCode + "-1-" + uid_pfix + subStr("" + tmpNum, 1);// 把10002首位的1去掉,再拼成1601260002字符串 } else { comment_code = typeCode + "-1-" + uid_pfix + "001"; } res.put("businessCode", comment_code); String insertSql; if(typeCode.equals("YJ")||typeCode.equals("DT")){ insertSql = "update BUSINESS_CODE_GENERATE set new_value = '"+comment_code+"' where table_name = '"+tableName + "'" + " AND TABLE_TYPE = '"+typeCode+"'"; }else{ insertSql = "update BUSINESS_CODE_GENERATE set new_value = '"+comment_code+"' where table_name = '"+tableName + "'"; } Object obj = SqlAdapter.executeForExecSql(insertSql, "DB_FIX_BIZ_BASE", null); System.out.println("生成" +tableName+"表的最新值"+ obj); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return new Gson().toJson(res); } /* * 把10002首位的1去掉的实现方法: * @param str * @param start * @return */ public static String subStr(String str, int start) { if (str == null || str.equals("") || str.length() == 0) return ""; if (start < str.length()) { return str.substring(start); } else { return ""; } } }