生成自增id

  • 时间:
  • 来源:互联网
  • 文章标签:

 1.批量生成自增类型流水号id

package com.test.demo.config;

import java.util.ArrayList;
import java.util.List;

/**
 * 导入导出工具类ExcelUtil
 *
 * @author zhuochao
 * @date 2020/10/22
 */
public class ExcelUtil {
    //a 表示你想生成多少个主键的数量
    public static List<String> getnum(int a) {
        List<String> list = new ArrayList<String>();
        String str = "";
        for (int i = 1; i <= a; i++) {
            str = String.format("%04d", i);
            list.add(str);
        }
        return list;
    }


    public static void main(String[] args) {

        List<String> list = new ArrayList<String>();
        String m = "ER-";
        for (int i = 0; i < 2; i++) {
            String format = String.format("%05d", i);
            list.add(format);
            System.out.println(m + format);
        }
        System.out.println(getnum(5));
    }
}

2.生成单条的自增型流水号id

 /**
 *
 */
package com.mw.framework.util;
 
import java.util.concurrent.atomic.AtomicInteger;
 

public class SerialNumberUtils {
	
	public static String[] sn(int len,Integer i,String driver,String prefix,int limited){
		String[] result = new String[4];
		AtomicInteger z = new AtomicInteger(i);
		AtomicInteger x = new AtomicInteger(i);
		driver = driver==null?"":driver;
		prefix = prefix==null||prefix.equals("null")?"":prefix;
		//1.判断流水号长度是否大于约定长度
		if(z.toString().length()<=(len-driver.length())){
			if(Integer.valueOf(x.incrementAndGet()).toString().length()>(len-driver.length())||(driver.length()>limited && limited!=-1)){
				System.out.println("默认索引超出范围,需从字母A开始重新一轮的流水号");
				//2.如果流水号+字母长度超过了最大限度修改新的长度
				//len=String.valueOf(x.get()).length();
				int temp  = String.valueOf(x.get()).length();
				
				AtomicInteger atomicInteger = new AtomicInteger(0);
				char[] charArray = driver.toCharArray();
				
				for (char c : charArray) {
					if(c=='Z'){
						atomicInteger.getAndIncrement();
					}
				}
				
				if((temp+driver.length()>len && atomicInteger.intValue()==driver.length() && driver.length()>0) ||(driver.length()>limited && limited!=-1)){
					System.out.println("如果流水号+字母长度超过了最大限度修改新的长度,并重新开始一轮+1的流水号");
					len++;
					driver="";
				}else{
					driver = driverCheck(driver,len,limited);
					if(driver.equals(".N")){
						driver = "";
					}
				}
				z.set(0);
			}
			
			
			
			String current = String.format("%0"+(len-driver.length())+"d",z.incrementAndGet());
			result[0]=prefix+driver+current;
			result[1]=driver;
			result[2]=String.valueOf(len);
			result[3]=String.valueOf(z.get());
			System.out.println(prefix+driver+current);
		}else{
			System.out.println("输入不合法!");
		}
		
		return result;
	}
	
	/**
	 * 字母有效检查
	 * 1.检查字母是否都为Z
	 * 2.检查字母长度
	 * @param driver
	 * @param len
	 * @return
	 */
	public static String driverCheck(String driver,int len,int limited){
		if(driver!=null && driver.length()>0){
			AtomicInteger z = new AtomicInteger(0);
			char[] charArray = driver.toCharArray();
			
			for (char c : charArray) {
				if(c=='Z'){
					z.getAndIncrement();
				}
			}
			
			if(z.intValue()==driver.length()){
				String result = "";
				if(z.intValue()==len && z.intValue()>limited){//如所有字母都为Z,并且长度达到限定长度,返回.N
					System.out.println("如所有字母都为Z,并且长度达到限定长度,返回.N");
					result = ".N";
				}else if(z.intValue()<len-1 && z.intValue()<=limited){
					System.out.println("如果所有字母都为Z,但长度未达到限定长度,直接递增");
					result = driver(driver+"@");
				}
				return result;
			}else{
				System.out.println("以上两个条件都不满足,则直接递增");
				return driver(driver);
			}
			/*if(z.intValue()==driver.length() && z.intValue()==len){//如所有字母都为Z,并且长度达到限定长度,返回.N
				return ".N";
			}else if(z.intValue()==driver.length() && z.intValue()<len){//如果所有字母都为Z,但长度未达到限定长度,则在调用字母递增方法之前加入@用以递增A
				System.out.println(z.intValue());
				System.out.println(driver.length());
				return driver2(driver);
				
			}else{//以上两个条件都不满足,则直接递增
				return driver2(driver);
			}*/
		}else{
			return "A";
		}
	}
	
	/**
	 * 字母递增
	 * @param driver
	 * @return
	 */
	public static String driver(String driver){
		
		if(driver!=null && driver.length()>0){
			char[] charArray = driver.toCharArray();
			boolean bool = false;
			for(int i = charArray.length-1;i>-1;i--){
				if(charArray[i]=='Z'){
					if(i==charArray.length-1){
						charArray[i]='A';
						bool = true;
					}else{
						if(bool==true){
							AtomicInteger atomic = new AtomicInteger(charArray[i]);
							int val = atomic.incrementAndGet();
							if(90>=val){
								charArray[i]=(char) val;
								bool = false;
							}else{
								charArray[i]='A';
								bool = true;
							}
						}
					}
				}else{
					if(bool==true || (i==charArray.length-1)){
						AtomicInteger atomic = new AtomicInteger(charArray[i]);
						int val = atomic.incrementAndGet();
						if(90>=val){
							charArray[i]=(char) val;
							bool = false;
						}else{
							charArray[i]='A';
							bool = true;
						}
					}
				}
				
			}
			return String.valueOf(charArray);
		}else{
			return "A";
		}
	}
	
 
	public static void main(String[] args) {
		String[] s_= sn(5,9, "ZYAS","",-1);
		for (String string : s_) {
			System.out.println(string);
		}
		
	}
}

 

本文链接http://www.taodudu.cc/news/show-1782130.html