zoukankan      html  css  js  c++  java
  • uuid算法

    package com.java.activiti.page;

    import java.util.UUID;

    /**
    * <p>Title: Chief Designer</p>
    * <p>Description: neration ( www.neration.com)</p>
    * <p>Copyright: Copyright (c) 2002</p>
    * <p>Company: neration soft</p>
    *
    * @author zhiguo chen (zhiguochen@hotmail.com)
    * @version 1.0
    */

    public final class UuidUtils {
    private static final int LOMASK = 15;
    private static final int HIMASK = 240;
    private static final int LO8BITMASK = 255;
    private static final int BITS8 = 8;
    private static final int BYTELEN = 16;

    private static final long MAX_LONG = 2147483647;

    public static String generateUUID() {
    return bytesToString(generateByteArray());
    }

    private static byte[] generateByteArray() {
    byte bytes[] = new byte[16];
    int idx = 15;
    long val = (long) (Math.random() * MAX_LONG) + (long) (Math.random() * MAX_LONG) * 1000000000;
    for (int i = 0; i < 8; i++) {
    bytes[idx--] = (byte) (int) (val & (long) 255);
    val >>= 8;
    }

    val = (long) System.currentTimeMillis();
    for (int i = 0; i < 8; i++) {
    bytes[idx--] = (byte) (int) (val & (long) 255);
    val >>= 8;
    }

    return bytes;
    }

    private static String bytesToString(byte bytes[]) {
    // if(16 != bytes.length)
    // return "** Bad UuidUtils Format/Value **";
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < 16; i++) {
    buf.append(Integer.toHexString(hiNibble(bytes[i])));
    buf.append(Integer.toHexString(loNibble(bytes[i])));
    }
    return buf.toString();
    }

    private static final byte loNibble(byte b) {
    return (byte) (b & 0xf);
    }

    private static final byte hiNibble(byte b) {
    return (byte) (b >> 4 & 0xf);
    }

    public static void main(String[] args) {
    for (int i = 0; i < 20; i++) {
    System.out.println("" + UuidUtils.generateUUID());
    }
    }
    }

    实体类里要加入

    private static final long serialVersionUID = 1L;

    在接口里设置

    例如:

    user.setId(UuidUtils.generateUUID());
    int userResult=userService.addUser(user);

  • 相关阅读:
    Java 使用Calendar类输出指定年份和月份的日历
    ioc aop
    多线程下单例模式:懒加载(延迟加载)和即时加载
    Java生产环境下性能监控与调优详解
    springboot + zipkin + mysql
    springboot + zipkin(brave-okhttp实现)
    springboot启动方式
    OpenResty实现限流的几种方式
    RocketMQ核心技术精讲与高并发抗压实战
    codis 使用
  • 原文地址:https://www.cnblogs.com/minxiaofei/p/9771002.html
Copyright © 2011-2022 走看看