• mysql生成主键


    在mysql中,可以使用uuid 来生成主键,但是用mysql的uuid()函数 ,生成的uuid是36位的,其中包含32个字符以及4个分隔符(-),

    往往这个分隔符对我们来说是没有用的,可以使用mysql自带的replace函数去掉分隔符

    replace(uuid(),'-','')   ---->将uuid()中的‘-’,去掉,即替换成空串;

    此外

    upper(replace(uuid(),'-',''))用于将字符转换为大写

    JAVA文件中也可生成UUID主键:

    package ---;
    
    import java.util.Random;
    import java.util.UUID;
    
    /**
     * 功能简述:主键生成器,调用java util生成32位的 字符串
     * 
     * @author 
     * @version
     * 
     */
    public class IdGenerator {
    
        /**
         * 
         * @return 32位的uuid
         */
        public static String getUUID() {
            return UUID.randomUUID().toString().replaceAll("-", "");
        }
    
        /**
         * 功能简述:根据UUID生成10位订单号
         * 
         * @return
         */
        public static String getOrderIdByUUID() {
            int hashCode = UUID.randomUUID().toString().hashCode();
            if (hashCode < 0) {
                hashCode = -hashCode;
            }
            // 0-前面补充0;10 代表长度为10;d代表参数为正数
            return String.format("%010d", hashCode);
        }
    
        // 生成4位提取码
        public static int getCode() {
            Random random = new Random();
            int num = random.nextInt(900);
            num = num + 100;
            return num;
        }
    
        // 16位订单流水号: 类型  + 时间戳 + 随机3位数
        public static String generateOrderNo(String type) {
            return type + System.currentTimeMillis() + getCode();
        }
    
        public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
                "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0",
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
                "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y",
                "Z" };
    
        // 8位uuid
        public static String generateShortUuid() {
            StringBuffer shortBuffer = new StringBuffer();
            String uuid = UUID.randomUUID().toString().replace("-", "");
            for (int i = 0; i < 8; i++) {
                String str = uuid.substring(i * 4, i * 4 + 4);
                int x = Integer.parseInt(str, 16);
                shortBuffer.append(chars[x % 0x3E]);
            }
            return shortBuffer.toString();
        }
    
    }
  • 相关阅读:
    理解inode
    贝叶斯公式与拼写检查器
    《C程序设计语言》第四章 函数和程序结构
    MIT《计算机科学与编程导论》课堂笔记
    很牛的牛顿迭代法
    开发一个小工具重温C#经典问题
    斯坦福《编程方法学》环境搭建及常见问题
    看Sybase官方手册学索引工作原理
    学习编程的方法、软件和工具
    大师里奇留给了我们什么
  • 原文地址:https://www.cnblogs.com/lizhonghua34/p/6027832.html
Copyright © 2020-2023  润新知