diff --git a/src/main/java/com/lanyuanxiaoyao/service/template/controller/Query.java b/src/main/java/com/lanyuanxiaoyao/service/template/controller/Query.java index 2e5a3d9..152a540 100644 --- a/src/main/java/com/lanyuanxiaoyao/service/template/controller/Query.java +++ b/src/main/java/com/lanyuanxiaoyao/service/template/controller/Query.java @@ -59,131 +59,131 @@ public class Query { private Map between; private Map notBetween; - public java.util.List getNullEqual() { + public List getNullEqual() { return nullEqual; } - public void setNullEqual(java.util.List nullEqual) { + public void setNullEqual(List nullEqual) { this.nullEqual = nullEqual; } - public java.util.List getNotNullEqual() { + public List getNotNullEqual() { return notNullEqual; } - public void setNotNullEqual(java.util.List notNullEqual) { + public void setNotNullEqual(List notNullEqual) { this.notNullEqual = notNullEqual; } - public java.util.List getEmpty() { + public List getEmpty() { return empty; } - public void setEmpty(java.util.List empty) { + public void setEmpty(List empty) { this.empty = empty; } - public java.util.List getNotEmpty() { + public List getNotEmpty() { return notEmpty; } - public void setNotEmpty(java.util.List notEmpty) { + public void setNotEmpty(List notEmpty) { this.notEmpty = notEmpty; } - public java.util.Map getEqual() { + public Map getEqual() { return equal; } - public void setEqual(java.util.Map equal) { + public void setEqual(Map equal) { this.equal = equal; } - public java.util.Map getNotEqual() { + public Map getNotEqual() { return notEqual; } - public void setNotEqual(java.util.Map notEqual) { + public void setNotEqual(Map notEqual) { this.notEqual = notEqual; } - public java.util.Map getLike() { + public Map getLike() { return like; } - public void setLike(java.util.Map like) { + public void setLike(Map like) { this.like = like; } - public java.util.Map getNotLike() { + public Map getNotLike() { return notLike; } - public void setNotLike(java.util.Map notLike) { + public void setNotLike(Map notLike) { this.notLike = notLike; } - public java.util.Map> getGreat() { + public Map> getGreat() { return great; } - public void setGreat(java.util.Map> great) { + public void setGreat(Map> great) { this.great = great; } - public java.util.Map> getLess() { + public Map> getLess() { return less; } - public void setLess(java.util.Map> less) { + public void setLess(Map> less) { this.less = less; } - public java.util.Map> getGreatEqual() { + public Map> getGreatEqual() { return greatEqual; } - public void setGreatEqual(java.util.Map> greatEqual) { + public void setGreatEqual(Map> greatEqual) { this.greatEqual = greatEqual; } - public java.util.Map> getLessEqual() { + public Map> getLessEqual() { return lessEqual; } - public void setLessEqual(java.util.Map> lessEqual) { + public void setLessEqual(Map> lessEqual) { this.lessEqual = lessEqual; } - public java.util.Map> getIn() { + public Map> getIn() { return in; } - public void setIn(java.util.Map> in) { + public void setIn(Map> in) { this.in = in; } - public java.util.Map> getNotIn() { + public Map> getNotIn() { return notIn; } - public void setNotIn(java.util.Map> notIn) { + public void setNotIn(Map> notIn) { this.notIn = notIn; } - public java.util.Map getBetween() { + public Map getBetween() { return between; } - public void setBetween(java.util.Map between) { + public void setBetween(Map between) { this.between = between; } - public java.util.Map getNotBetween() { + public Map getNotBetween() { return notBetween; } - public void setNotBetween(java.util.Map notBetween) { + public void setNotBetween(Map notBetween) { this.notBetween = notBetween; } diff --git a/src/main/java/com/lanyuanxiaoyao/service/template/entity/IdOnlyEntity.java b/src/main/java/com/lanyuanxiaoyao/service/template/entity/IdOnlyEntity.java index ad0cafb..1eced6a 100644 --- a/src/main/java/com/lanyuanxiaoyao/service/template/entity/IdOnlyEntity.java +++ b/src/main/java/com/lanyuanxiaoyao/service/template/entity/IdOnlyEntity.java @@ -14,7 +14,7 @@ public class IdOnlyEntity { @Comment("记录唯一标记") @Id @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; public Long getId() { diff --git a/src/main/java/com/lanyuanxiaoyao/service/template/helper/SnowflakeIdGenerator.java b/src/main/java/com/lanyuanxiaoyao/service/template/helper/SnowflakeIdGenerator.java new file mode 100644 index 0000000..2e071d9 --- /dev/null +++ b/src/main/java/com/lanyuanxiaoyao/service/template/helper/SnowflakeIdGenerator.java @@ -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(); + } + } +}