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);

  • 相关阅读:
    velocity模板引擎学习(2)-velocity tools 2.0
    silverlight: http请求的GET及POST示例
    职责链(Chain of Responsibility)模式在航空货运中的运用实例
    H2 Database入门
    velocity模板引擎学习(1)
    Struts2、Spring MVC4 框架下的ajax统一异常处理
    企业应用通用架构图
    nginx学习(2):启动gzip、虚拟主机、请求转发、负载均衡
    nginx学习(1):编译、安装、启动
    eclipse/intellij Idea集成jetty
  • 原文地址:https://www.cnblogs.com/minxiaofei/p/9771002.html
Copyright © 2011-2022 走看看