zoukankan      html  css  js  c++  java
  • 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();
        }
    
    }
  • 相关阅读:
    C#_数据访问类查询 和 属性扩展
    C#_0913 完整修改删除、实体类和数据访问类
    webform基础
    TreeView, 数据导出
    无边框窗体,tiner控件
    容器,打印设置
    多窗体,菜单工具栏
    公共控件
    winform
    数据访问类查询
  • 原文地址:https://www.cnblogs.com/lizhonghua34/p/6027832.html
Copyright © 2011-2022 走看看