fix: 雪花算法id

V0.5.x
jay 2023-06-12 19:12:52 +08:00
parent ce99a78d39
commit 4e6a354847
3 changed files with 23 additions and 2 deletions

View File

@ -31,6 +31,10 @@
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.github.yitter</groupId>
<artifactId>yitter-idgenerator</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>

View File

@ -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();
}
}

View File

@ -46,6 +46,7 @@
<knife4j.version>2.0.9</knife4j.version>
<validateion.version>1.1.0.Final</validateion.version>
<lombok.version>1.18.26</lombok.version>
<yitter.version>1.0.6</yitter.version>
</properties>
<dependencyManagement>
@ -74,7 +75,11 @@
<artifactId>commons-beanutils</artifactId>
<version>${beanutils.version}</version>
</dependency>
<dependency>
<groupId>com.github.yitter</groupId>
<artifactId>yitter-idgenerator</artifactId>
<version>${yitter.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>