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 1eced6a..328bf26 100644 --- a/src/main/java/com/lanyuanxiaoyao/service/template/entity/IdOnlyEntity.java +++ b/src/main/java/com/lanyuanxiaoyao/service/template/entity/IdOnlyEntity.java @@ -1,20 +1,18 @@ package com.lanyuanxiaoyao.service.template.entity; +import com.blinkfox.fenix.id.SnowflakeId; import jakarta.persistence.EntityListeners; -import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.MappedSuperclass; import org.hibernate.annotations.Comment; -import org.hibernate.annotations.GenericGenerator; import org.springframework.data.jpa.domain.support.AuditingEntityListener; @MappedSuperclass @EntityListeners(AuditingEntityListener.class) public class IdOnlyEntity { - @Comment("记录唯一标记") @Id - @GeneratedValue(generator = "snowflake") - @GenericGenerator(name = "snowflake", strategy = "com.lanyuanxiaoyao.service.template.helper.SnowflakeIdGenerator") + @SnowflakeId + @Comment("记录唯一标记") private Long id; public Long getId() { diff --git a/src/main/java/com/lanyuanxiaoyao/service/template/entity/SimpleEntity.java b/src/main/java/com/lanyuanxiaoyao/service/template/entity/SimpleEntity.java index b839a2c..a12e1e3 100644 --- a/src/main/java/com/lanyuanxiaoyao/service/template/entity/SimpleEntity.java +++ b/src/main/java/com/lanyuanxiaoyao/service/template/entity/SimpleEntity.java @@ -11,11 +11,11 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener; @MappedSuperclass @EntityListeners(AuditingEntityListener.class) public class SimpleEntity extends IdOnlyEntity { - @Comment("记录创建时间") @CreatedDate + @Comment("记录创建时间") private LocalDateTime createdTime; - @Comment("记录更新时间") @LastModifiedDate + @Comment("记录更新时间") private LocalDateTime modifiedTime; public LocalDateTime getCreatedTime() { diff --git a/src/main/java/com/lanyuanxiaoyao/service/template/helper/SnowflakeIdGenerator.java b/src/main/java/com/lanyuanxiaoyao/service/template/helper/SnowflakeIdGenerator.java deleted file mode 100644 index 2e071d9..0000000 --- a/src/main/java/com/lanyuanxiaoyao/service/template/helper/SnowflakeIdGenerator.java +++ /dev/null @@ -1,82 +0,0 @@ -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(); - } - } -} diff --git a/src/test/java/com/lanyuanxiaoyao/service/template/TestApplication.java b/src/test/java/com/lanyuanxiaoyao/service/template/TestApplication.java index 5ed1d7b..5595537 100644 --- a/src/test/java/com/lanyuanxiaoyao/service/template/TestApplication.java +++ b/src/test/java/com/lanyuanxiaoyao/service/template/TestApplication.java @@ -109,6 +109,8 @@ public class TestApplication { Assert.isTrue(listItems("company").at("/data/items").size() == 2, "数量错误"); Assert.isTrue(listItems("company").at("/data/total").asLong() == 2, "返回数量错误"); + log.info(listItems("company").toPrettyString()); + System.exit(0); } diff --git a/src/test/java/com/lanyuanxiaoyao/service/template/entity/Company.java b/src/test/java/com/lanyuanxiaoyao/service/template/entity/Company.java index b0ed490..c1a0a82 100644 --- a/src/test/java/com/lanyuanxiaoyao/service/template/entity/Company.java +++ b/src/test/java/com/lanyuanxiaoyao/service/template/entity/Company.java @@ -11,7 +11,9 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener; @EntityListeners(AuditingEntityListener.class) @Comment("企业") public class Company extends SimpleEntity { + @Comment("名称") private String name; + @Comment("成员数") private Integer members; public String getName() { diff --git a/src/test/java/com/lanyuanxiaoyao/service/template/service/CompanyService.java b/src/test/java/com/lanyuanxiaoyao/service/template/service/CompanyService.java index 08b92ee..a63f585 100644 --- a/src/test/java/com/lanyuanxiaoyao/service/template/service/CompanyService.java +++ b/src/test/java/com/lanyuanxiaoyao/service/template/service/CompanyService.java @@ -13,4 +13,8 @@ public class CompanyService extends SimpleServiceSupport { public CompanyService(CompanyRepository repository) { super(repository); } + + public void test() { + + } }