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

  • 相关阅读:
    centos 安装 TortoiseSVN svn 客户端
    linux 定时任务 日志记录
    centos6.5 安装PHP7.0支持nginx
    linux root 用户 定时任务添加
    composer 一些使用说明
    laravel cookie写入
    laravel composer 安装指定版本以及基本的配置
    mysql 删除重复记录语句
    linux php redis 扩展安装
    linux php 安装 memcache 扩展
  • 原文地址:https://www.cnblogs.com/minxiaofei/p/9771002.html
Copyright © 2011-2022 走看看