自动生成业务编号 备份

     String sqlCode = "SELECT MAX(code) as code from gongzuo where   year(create_date) = year(NOW()) and month(create_date)=month(NOW())  ";
                String delegateCode = jdbcTemplate.queryForObject(sqlCode, String.class);

                StringB     String sqlCode = "SELECT MAX(code) as code from gongzuo where   year(create_date) = year(NOW()) and month(create_date)=month(NOW())  ";
                String delegateCode = jdbcTemplate.queryForObject(sqlCode, String.class);

                StringBuffer code = new StringBuffer("ZY");
                code.append(DateUtils.getDataString(DateUtils.yyyyMMdd).substring(2, 6));
                if (StringUtils.isEmpty(delegateCode)) {
                    code.append("0001");
                } else {
                    code.append(getCode(delegateCode));
                }
                gongzuo.setCode(code.toString());uffer code = new StringBuffer("ZY");
                code.append(DateUtils.getDataString(DateUtils.yyyyMMdd).substring(2, 6));
                if (StringUtils.isEmpty(delegateCode)) {
                    code.append("0001");
                } else {
                    code.append(getCode(delegateCode));
                }
                gongzuo.setCode(code.toString());
 // 后4位自定义编码 +1
    public String getCode(String code) {
        String result = "";
        code = code.substring(code.length() - 4);
        int a = Integer.parseInt(code);
        a++;
        int len = (a + "").length();
        if (len == 1) {
            result = "000" + a;
        } else if (len == 2) {
            result = "00" + a;
        } else if (len == 3) {
            result = "0" + a;
        } else {
            result = "" + a;
        }
        return result;
    }
原文地址:https://www.cnblogs.com/xueblvip/p/14380593.html