oracle?指定類型和指定位數(shù)創(chuàng)建序列號的代碼詳解
一、腳本部分
1. 表結(jié)構(gòu)
有注釋
-- Create table create table LDMAXNO ( NOTYPE VARCHAR2(17) not null, NOLIMIT VARCHAR2(12) not null, MAXNO INTEGER not null ); -- Add comments to the table comment on table LDMAXNO is '產(chǎn)生最大的流水號,所有的號碼從1開始'; -- Add comments to the columns comment on column LDMAXNO.NOTYPE is '含義描述:1、號碼類型'; comment on column LDMAXNO.NOLIMIT is '含義描述:1、號碼限制條件'; comment on column LDMAXNO.MAXNO is '含義描述:1、當前最大值'; -- Create/Recreate primary, unique and foreign key constraints alter table LDMAXNO add constraint PK_LDMAXNO primary key (NOTYPE, NOLIMIT);
2. 函數(shù)
create or replace function CreateMaxNos(cNoType in ldmaxno.notype%type,
cNoLimit in ldmaxno.nolimit%type)
return integer is
pragma autonomous_transaction;
tMaxNo integer := 0; --初始化賦值等于0,定義返回變量
begin
--最大數(shù)加1
update LDMaxNo
set MaxNo = MaxNo + 1
where NoType = cNoType
and NoLimit = cNoLimit
Returning MaxNo Into tMaxNo; --取出最大數(shù)
If (Sql%Notfound) then
--第一次向數(shù)據(jù)庫中插入最大數(shù)為 1 的記錄
Insert Into LDMaxNo
(NOTYPE, NOLIMIT, MAXNO)
values
(cNoType, cNoLimit, 1);
tMaxNo := 1;
End If;
commit;
return(tMaxNo); --返回結(jié)果
end CreateMaxNos;
/二、代碼部分
2.1. xml
DullMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gblfy.business.mapper.DullMapper">
<select id="getMaxNo" resultType="java.lang.String">
select createmaxno(#{cNoType},#{cNoLength}) from dual
</select>
</mapper>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 {
/**
* 功能:產(chǎn)生指定長度的流水號,一個號碼類型一個流水
* @param cNoType 流水號的類型
* @param cNoLength 流水號的長度
* @return 返回產(chǎn)生的流水號碼
*/
String getMaxNo(@Param("cNoType") String cNoType, @Param("cNoLength") int cNoLength);
}
2.3. api接口
package com.gblfy.business.service;
public interface SysMaxNoService {
/**
* 功能:產(chǎn)生指定長度的流水號,一個號碼類型一個流水
*
* @param cNoType 流水號的類型
* @param cNoLength 流水號的長度
* @return 返回產(chǎn)生的流水號碼
*/
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;
/**
* 功能:產(chǎn)生指定長度的流水號,一個號碼類型一個流水
*
* @param cNoType 流水號的類型
* @param cNoLength 流水號的長度
* @return 返回產(chǎn)生的流水號碼
*/
@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("生成流水號出現(xiàn)異常,請核實!");
}
String tStr = tMaxNo.toString();
//將生成的流水號進行加工處理
tStr = LCh(tStr, "0", cNoLength);
tReturn = tStr.trim();
return tReturn;
}
/**
* 將生成的流水號進行加工處理
* <p>
* 1.判斷是否滿足指定長度,如果不滿足前面用0來補位
* 2.將生成的流水號進行去空格處理
* 3.將最終的流水號進行字符串拼接
* </P>
*
* @param sourString
* @param cChar
* @param cLen
* @return
*/
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;
/**
* 生成指定類型+位數(shù)的流水號
*
* @Author gblfy
* @Date 2022-05-16 20:13
**/
@RestController
public class SysMaxNoController {
@Autowired
private SysMaxNoService maxNoService;
/**
* 生成指定類型+位數(shù)的流水號
*
* @param cNoType
* @param cNoLength
* @return
*/
@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. 效果圖


到此這篇關(guān)于oracle 指定類型和指定位數(shù)創(chuàng)建序列號的文章就介紹到這了,更多相關(guān)oracle創(chuàng)建序列號內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
oracle數(shù)據(jù)庫的基本使用教程(建表,操作表等)
這篇文章主要給大家介紹了關(guān)于oracle數(shù)據(jù)庫的基本使用(建表,操作表等)的相關(guān)資料,包含了Oracle創(chuàng)建表(create table as)使用方法、操作技巧、實例演示和注意事項,需要的朋友可以參考下2024-01-01
ORACLE 11g從 11.2.0.1升級到11.2.0.4 詳細實戰(zhàn)教程
這篇文章主要介紹了ORACLE 11g從 11.2.0.1升級到11.2.0.4 詳細實戰(zhàn)教程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03
Oracle插入日期數(shù)據(jù)常見的2個問題和解決方法
這篇文章主要介紹了Oracle插入日期數(shù)據(jù)常見的2個問題和解決方法,一個是提示無效的月份問題,一個是日期插入格式問題 ,需要的朋友可以參考下2014-07-07
查找oracle數(shù)據(jù)庫表中是否存在系統(tǒng)關(guān)鍵字的方法
遇到列說明無效的報錯情況,這是由于數(shù)據(jù)庫列名起的不好引起的,名字用到了數(shù)據(jù)庫的關(guān)鍵字2014-07-07
解決ORA-01747:user.table.column,table.column或列說明無效
這篇文章主要介紹了解決ORA-01747:user.table.column,table.column或列說明無效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

