From 4e6a354847ee713abd7b81ff188875756503f0f4 Mon Sep 17 00:00:00 2001
From: jay <75509151@qq.com>
Date: Mon, 12 Jun 2023 19:12:52 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20=E9=9B=AA=E8=8A=B1=E7=AE=97=E6=B3=95id?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../iot-data-serviceImpl-rdb/pom.xml | 4 ++++
.../data/config/id/SnowflakeIdGenerator.java | 14 +++++++++++++-
pom.xml | 7 ++++++-
3 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/pom.xml b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/pom.xml
index 3c815101..123dcf65 100644
--- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/pom.xml
+++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/pom.xml
@@ -31,6 +31,10 @@
com.querydsl
querydsl-jpa
+
+ com.github.yitter
+ yitter-idgenerator
+
com.h2database
diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/config/id/SnowflakeIdGenerator.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/config/id/SnowflakeIdGenerator.java
index e9bfcb3d..5d47fbde 100644
--- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/config/id/SnowflakeIdGenerator.java
+++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/config/id/SnowflakeIdGenerator.java
@@ -5,11 +5,16 @@ import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Objects;
+import com.github.yitter.contract.IdGeneratorOptions;
+import com.github.yitter.idgen.YitIdHelper;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
+import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
+import javax.annotation.PostConstruct;
+
/**
* @author: Longjun.Tu
* @description:
@@ -19,6 +24,13 @@ import org.springframework.stereotype.Component;
@Component
public class SnowflakeIdGenerator implements IdentifierGenerator {
+ @PostConstruct
+ public void snowflakeIdGenerator() {
+ // TODO: 2023/6/12 从配置文件中读取
+ IdGeneratorOptions options = new IdGeneratorOptions((short) 1);
+ YitIdHelper.setIdGenerator(options);
+ }
+
@Override
public Serializable generate(SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException {
Field id = null;
@@ -32,6 +44,6 @@ public class SnowflakeIdGenerator implements IdentifierGenerator {
} catch (NoSuchFieldException | IllegalAccessException e) {
}
- return SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId();
+ return YitIdHelper.nextId();
}
}
diff --git a/pom.xml b/pom.xml
index 49eb11f8..21db180f 100755
--- a/pom.xml
+++ b/pom.xml
@@ -46,6 +46,7 @@
2.0.9
1.1.0.Final
1.18.26
+ 1.0.6
@@ -74,7 +75,11 @@
commons-beanutils
${beanutils.version}
-
+
+ com.github.yitter
+ yitter-idgenerator
+ ${yitter.version}
+
com.fasterxml.jackson.core