fix: 增加雪花ID生成的实现
This commit is contained in:
@@ -59,131 +59,131 @@ public class Query {
|
|||||||
private Map<String, Between> between;
|
private Map<String, Between> between;
|
||||||
private Map<String, Between> notBetween;
|
private Map<String, Between> notBetween;
|
||||||
|
|
||||||
public java.util.List<String> getNullEqual() {
|
public List<String> getNullEqual() {
|
||||||
return nullEqual;
|
return nullEqual;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNullEqual(java.util.List<String> nullEqual) {
|
public void setNullEqual(List<String> nullEqual) {
|
||||||
this.nullEqual = nullEqual;
|
this.nullEqual = nullEqual;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.List<String> getNotNullEqual() {
|
public List<String> getNotNullEqual() {
|
||||||
return notNullEqual;
|
return notNullEqual;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNotNullEqual(java.util.List<String> notNullEqual) {
|
public void setNotNullEqual(List<String> notNullEqual) {
|
||||||
this.notNullEqual = notNullEqual;
|
this.notNullEqual = notNullEqual;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.List<String> getEmpty() {
|
public List<String> getEmpty() {
|
||||||
return empty;
|
return empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEmpty(java.util.List<String> empty) {
|
public void setEmpty(List<String> empty) {
|
||||||
this.empty = empty;
|
this.empty = empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.List<String> getNotEmpty() {
|
public List<String> getNotEmpty() {
|
||||||
return notEmpty;
|
return notEmpty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNotEmpty(java.util.List<String> notEmpty) {
|
public void setNotEmpty(List<String> notEmpty) {
|
||||||
this.notEmpty = notEmpty;
|
this.notEmpty = notEmpty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.Map<String, Object> getEqual() {
|
public Map<String, Object> getEqual() {
|
||||||
return equal;
|
return equal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEqual(java.util.Map<String, Object> equal) {
|
public void setEqual(Map<String, Object> equal) {
|
||||||
this.equal = equal;
|
this.equal = equal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.Map<String, Object> getNotEqual() {
|
public Map<String, Object> getNotEqual() {
|
||||||
return notEqual;
|
return notEqual;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNotEqual(java.util.Map<String, Object> notEqual) {
|
public void setNotEqual(Map<String, Object> notEqual) {
|
||||||
this.notEqual = notEqual;
|
this.notEqual = notEqual;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.Map<String, String> getLike() {
|
public Map<String, String> getLike() {
|
||||||
return like;
|
return like;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLike(java.util.Map<String, String> like) {
|
public void setLike(Map<String, String> like) {
|
||||||
this.like = like;
|
this.like = like;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.Map<String, String> getNotLike() {
|
public Map<String, String> getNotLike() {
|
||||||
return notLike;
|
return notLike;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNotLike(java.util.Map<String, String> notLike) {
|
public void setNotLike(Map<String, String> notLike) {
|
||||||
this.notLike = notLike;
|
this.notLike = notLike;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.Map<String, Comparable<Object>> getGreat() {
|
public Map<String, Comparable<Object>> getGreat() {
|
||||||
return great;
|
return great;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGreat(java.util.Map<String, Comparable<Object>> great) {
|
public void setGreat(Map<String, Comparable<Object>> great) {
|
||||||
this.great = great;
|
this.great = great;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.Map<String, Comparable<Object>> getLess() {
|
public Map<String, Comparable<Object>> getLess() {
|
||||||
return less;
|
return less;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLess(java.util.Map<String, Comparable<Object>> less) {
|
public void setLess(Map<String, Comparable<Object>> less) {
|
||||||
this.less = less;
|
this.less = less;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.Map<String, Comparable<Object>> getGreatEqual() {
|
public Map<String, Comparable<Object>> getGreatEqual() {
|
||||||
return greatEqual;
|
return greatEqual;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGreatEqual(java.util.Map<String, Comparable<Object>> greatEqual) {
|
public void setGreatEqual(Map<String, Comparable<Object>> greatEqual) {
|
||||||
this.greatEqual = greatEqual;
|
this.greatEqual = greatEqual;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.Map<String, Comparable<Object>> getLessEqual() {
|
public Map<String, Comparable<Object>> getLessEqual() {
|
||||||
return lessEqual;
|
return lessEqual;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLessEqual(java.util.Map<String, Comparable<Object>> lessEqual) {
|
public void setLessEqual(Map<String, Comparable<Object>> lessEqual) {
|
||||||
this.lessEqual = lessEqual;
|
this.lessEqual = lessEqual;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.Map<String, java.util.List<Object>> getIn() {
|
public Map<String, List<Object>> getIn() {
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setIn(java.util.Map<String, java.util.List<Object>> in) {
|
public void setIn(Map<String, List<Object>> in) {
|
||||||
this.in = in;
|
this.in = in;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.Map<String, java.util.List<Object>> getNotIn() {
|
public Map<String, List<Object>> getNotIn() {
|
||||||
return notIn;
|
return notIn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNotIn(java.util.Map<String, java.util.List<Object>> notIn) {
|
public void setNotIn(Map<String, List<Object>> notIn) {
|
||||||
this.notIn = notIn;
|
this.notIn = notIn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.Map<String, com.lanyuanxiaoyao.service.template.controller.Query.Queryable.Between> getBetween() {
|
public Map<String, Between> getBetween() {
|
||||||
return between;
|
return between;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBetween(java.util.Map<String, com.lanyuanxiaoyao.service.template.controller.Query.Queryable.Between> between) {
|
public void setBetween(Map<String, Between> between) {
|
||||||
this.between = between;
|
this.between = between;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.Map<String, com.lanyuanxiaoyao.service.template.controller.Query.Queryable.Between> getNotBetween() {
|
public Map<String, Between> getNotBetween() {
|
||||||
return notBetween;
|
return notBetween;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNotBetween(java.util.Map<String, com.lanyuanxiaoyao.service.template.controller.Query.Queryable.Between> notBetween) {
|
public void setNotBetween(Map<String, Between> notBetween) {
|
||||||
this.notBetween = notBetween;
|
this.notBetween = notBetween;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ public class IdOnlyEntity {
|
|||||||
@Comment("记录唯一标记")
|
@Comment("记录唯一标记")
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(generator = "snowflake")
|
@GeneratedValue(generator = "snowflake")
|
||||||
@GenericGenerator(name = "snowflake", strategy = "com.lanyuanxiaoyao.service.ai.web.configuration.SnowflakeIdGenerator")
|
@GenericGenerator(name = "snowflake", strategy = "com.lanyuanxiaoyao.service.template.helper.SnowflakeIdGenerator")
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
package com.lanyuanxiaoyao.service.template.helper;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.Instant;
|
||||||
|
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||||
|
import org.hibernate.id.IdentifierGenerator;
|
||||||
|
|
||||||
|
public class SnowflakeIdGenerator implements IdentifierGenerator {
|
||||||
|
@Override
|
||||||
|
public Serializable generate(SharedSessionContractImplementor session, Object object) {
|
||||||
|
try {
|
||||||
|
return Snowflake.next();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Snowflake {
|
||||||
|
/**
|
||||||
|
* 起始的时间戳
|
||||||
|
*/
|
||||||
|
private final static long START_TIMESTAMP = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 序列号占用的位数
|
||||||
|
*/
|
||||||
|
private final static long SEQUENCE_BIT = 11;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 序列号最大值
|
||||||
|
*/
|
||||||
|
private final static long MAX_SEQUENCE_BIT = ~(-1 << SEQUENCE_BIT);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 时间戳值向左位移
|
||||||
|
*/
|
||||||
|
private final static long TIMESTAMP_OFFSET = SEQUENCE_BIT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 序列号
|
||||||
|
*/
|
||||||
|
private static long sequence = 0;
|
||||||
|
/**
|
||||||
|
* 上一次时间戳
|
||||||
|
*/
|
||||||
|
private static long lastTimestamp = -1;
|
||||||
|
|
||||||
|
public static synchronized long next() {
|
||||||
|
long currentTimestamp = nowTimestamp();
|
||||||
|
if (currentTimestamp < lastTimestamp) {
|
||||||
|
throw new RuntimeException("Clock have moved backwards.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentTimestamp == lastTimestamp) {
|
||||||
|
// 相同毫秒内, 序列号自增
|
||||||
|
sequence = (sequence + 1) & MAX_SEQUENCE_BIT;
|
||||||
|
// 同一毫秒的序列数已经达到最大
|
||||||
|
if (sequence == 0) {
|
||||||
|
currentTimestamp = nextTimestamp();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 不同毫秒内, 序列号置为0
|
||||||
|
sequence = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastTimestamp = currentTimestamp;
|
||||||
|
return (currentTimestamp - START_TIMESTAMP) << TIMESTAMP_OFFSET | sequence;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long nextTimestamp() {
|
||||||
|
long milli = nowTimestamp();
|
||||||
|
while (milli <= lastTimestamp) {
|
||||||
|
milli = nowTimestamp();
|
||||||
|
}
|
||||||
|
return milli;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long nowTimestamp() {
|
||||||
|
return Instant.now().toEpochMilli();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user