栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mysql 指定类型和指定位数创建序列号

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mysql 指定类型和指定位数创建序列号


文章目录
          • 一、脚本部分
            • 1. 表结构
            • 2. 函数
          • 二、代码部分
            • 2.1. xml
            • 2.2. 接口
            • 2.3. api接口
            • 2.4. api实例
            • 2.5. 控制层
          • 三、测试
            • 3.1. 效果图

一、脚本部分 1. 表结构

有注释

CREATE TABLE `ldmaxno` (
  `notype` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '号码类型',
  `nolimit` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '号码限制条件',
  `maxno` int NOT NULL COMMENT '当前最大值',
  PRIMARY KEY (`notype`,`nolimit`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 ROW_FORMAT=COMPACT COMMENT='流水号编码表';

无注释

CREATE TABLE `ldmaxno` (
  `notype` varchar(60) NOT NULL,
  `nolimit` varchar(60) NOT NULL,
  `maxno` int(11) NOT NULL,
  PRIMARY KEY (`notype`,`nolimit`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
2. 函数
CREATE DEFINER=`root`@`localhost` FUNCTION `CreateMaxNo`(
  cNoType VARCHAr ( 60 ),
  cNoLimit VARCHAr ( 60 )) RETURNS int(11)
BEGIN
  DECLARE
    tMaxNo INTEGER DEFAULT 0; -- 初始化赋值等于0,
  
  SET tMaxNo = ( SELECt MaxNo FROM LDMaxNo WHERe NoType = cNoType AND NoLimit = cNoLimit FOR UPDATe );
  IF
    isnull( tMaxNo ) = 1 THEN
      INSERT INTO LDMaxNo ( NOTYPE, NOLIMIT, MAXNO )
    VALUES
      ( cNoType, cNoLimit, 1 );
    
    SET tMaxNo = 1;
    ELSE 
      SET tMaxNo = tMaxNo + 1;
    UPDATE LDMaxNo 
    SET MaxNo = tMaxNo 
    WHERe
      NoType = cNoType 
      AND NoLimit = cNoLimit;
    
  END IF;
  RETURN tMaxNo;

END
二、代码部分 2.1. xml

DullMapper.xml





    


2.2. 接口

DullMapper.java

package com.gblfy.business.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;

public interface DullMapper extends BaseMapper {

    
    String getMaxNo(@Param("cNoType") String cNoType, @Param("cNoLength") int cNoLength);
}

2.3. api接口
package com.gblfy.business.service;

public interface SysMaxNoService {

    
    String createMaxNo(String cNoType, int cNoLength);
}


2.4. api实例
package com.gblfy.business.service.impl;

import com.gblfy.business.mapper.DullMapper;
import com.gblfy.business.service.SysMaxNoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;

@Service
public class SysMaxNoServiceImpl implements SysMaxNoService {

    private final static Logger logger = LoggerFactory.getLogger(SysMaxNoServiceImpl.class);

    @Resource
    private DullMapper dullMapper;


    
    @Override
    public String createMaxNo(String cNoType, int cNoLength) {
        if ((cNoType == null) || (cNoType.trim().length() <= 0) ||
                (cNoLength <= 0)) {
            logger.info("NoType长度错误 {} NoLength错误", cNoType, cNoLength);
            return null;
        }
        cNoType = cNoType.toUpperCase();
        String tReturn = "";
        String cNoLimit = "SN";
        BigInteger tMaxNo = new BigInteger("0");
        tReturn = cNoLimit;

        try {
            String result = dullMapper.getMaxNo(cNoType, cNoLength);
            tMaxNo = new BigInteger(result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("生成流水号出现异常,请核实!");
        }

        String tStr = tMaxNo.toString();

        //将生成的流水号进行加工处理
        tStr = LCh(tStr, "0", cNoLength);
        tReturn = tStr.trim();

        return tReturn;
    }

    
    private String LCh(String sourString, String cChar, int cLen) {
        int tLen = sourString.length();
        int i, iMax;
        String tReturn = "";
        if (tLen >= cLen) {
            return sourString;
        }
        //1.判断是否满足指定长度,如果不满足前面用0来补位
        iMax = cLen - tLen;
        for (i = 0; i < iMax; i++) {
            tReturn += cChar;
        }
        //2.将生成的流水号进行去空格处理
        //3.将最终的流水号进行字符串拼接
        tReturn = tReturn.trim() + sourString.trim();
        return tReturn;
    }
}


2.5. 控制层
package com.gblfy.business.controller;

import com.gblfy.business.service.SysMaxNoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class SysMaxNoController {

    @Autowired
    private SysMaxNoService maxNoService;

    
    @GetMapping("/generate/serial/number")
    public String generateSerialNumber(@RequestParam(name = "cNoType", required = false, defaultValue = "cNoType") String cNoType,
                                       @RequestParam int cNoLength) {
        return maxNoService.createMaxNo(cNoType, cNoLength);
    }
}

三、测试 3.1. 效果图


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/891858.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号