封装各种生成唯一性ID算法的工具类
来源:互联网 发布:vnc server使用的端口 编辑:程序博客网 时间:2024/09/21 06:36
import java.io.Serializable;
import java.security.SecureRandom;
import java.util.UUID;
import org.activiti.engine.impl.cfg.IdGenerator;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionIdGenerator;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
/**
* 封装各种生成唯一性ID算法的工具类.
*/
@Service
@Lazy(false)
public class IdGen implements IdGenerator, SessionIdGenerator {
private static SecureRandom random = new SecureRandom();
/**
* 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
*/
public static String uuid() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
/**
* 使用SecureRandom随机生成Long.
*/
public static long randomLong() {
return Math.abs(random.nextLong());
}
/**
* 基于Base62编码的SecureRandom随机生成bytes.
*/
public static String randomBase62(int length) {
byte[] randomBytes = new byte[length];
random.nextBytes(randomBytes);
return Encodes.encodeBase62(randomBytes);
}
/**
* Activiti ID 生成
*/
@Override
public String getNextId() {
return IdGen.uuid();
}
@Override
public Serializable generateId(Session session) {
return IdGen.uuid();
}
public static void main(String[] args) {
System.out.println(IdGen.uuid());
System.out.println(IdGen.uuid().length());
System.out.println(new IdGen().getNextId());
for (int i=0; i<1000; i++){
System.out.println(IdGen.randomLong() + " <-----> " + IdGen.randomBase62(5));
}
}
import java.security.SecureRandom;
import java.util.UUID;
import org.activiti.engine.impl.cfg.IdGenerator;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionIdGenerator;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
/**
* 封装各种生成唯一性ID算法的工具类.
*/
@Service
@Lazy(false)
public class IdGen implements IdGenerator, SessionIdGenerator {
private static SecureRandom random = new SecureRandom();
/**
* 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
*/
public static String uuid() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
/**
* 使用SecureRandom随机生成Long.
*/
public static long randomLong() {
return Math.abs(random.nextLong());
}
/**
* 基于Base62编码的SecureRandom随机生成bytes.
*/
public static String randomBase62(int length) {
byte[] randomBytes = new byte[length];
random.nextBytes(randomBytes);
return Encodes.encodeBase62(randomBytes);
}
/**
* Activiti ID 生成
*/
@Override
public String getNextId() {
return IdGen.uuid();
}
@Override
public Serializable generateId(Session session) {
return IdGen.uuid();
}
public static void main(String[] args) {
System.out.println(IdGen.uuid());
System.out.println(IdGen.uuid().length());
System.out.println(new IdGen().getNextId());
for (int i=0; i<1000; i++){
System.out.println(IdGen.randomLong() + " <-----> " + IdGen.randomBase62(5));
}
}
}
thank lzj
0 0
- 封装各种生成唯一性ID算法的工具类
- 封装生成唯一性ID的算法工具类
- 唯一性ID算法的工具类
- [算法] 生成唯一id
- 订单ID的唯一性封装
- 生成唯一的ID
- ID生成工具类,各种id生成策略
- 用MD5生成唯一性的id
- PHP----生成唯一的ID
- 生成一个唯一的id
- 生成一个唯一的Id
- 利用网上的Caesar算法写了一个全局唯一id生成类
- PHP使用SnowFlake算法生成唯一ID
- C语言实现分布式自增有序的唯一ID生成算法-snowflake算法
- C语言实现分布式自增有序的唯一ID生成算法-snowflake算法
- 如何生成一个唯一的ID值
- 数据库中生成唯一的ID
- iOS 如何生成唯一的设备ID
- 抽头延时信道模型(David Tse教材笔记)
- 你对ChemOffice Professional 16.0了解多少
- 51单片机的LED与数码管的驱动
- 用了springmvc为何JS,CSS等文件就是访问不到
- JavaScript趣题:深度比较对象
- 封装各种生成唯一性ID算法的工具类
- caffe添加新层教程
- InstallShield2015打包程序在win7下权限问题
- Android Studio之基本Gradle使用
- CSS长度单位:px和pt的区别
- 机器学习-周志华老师新书
- C语言中可变参数的用法——va_list、va_start、va_arg、va_end参数定义
- Android 记住密码和自动登录界面的实现的几篇参考文章
- iOS开发总结-UITableView 自定义cell和动态计算cell的高度