提交 2a1b40f9 编写于 作者: M MaxKey

SnowFlakeIdGenerator(InetAddress inetAddress)

上级 4088deb5
......@@ -31,7 +31,7 @@ public class IdentifierGeneratorFactory {
register(IdStrategy.UUIDHEX , new UUIDHexGenerator());
register(IdStrategy.SERIAL , new SerialGenerator());
register(IdStrategy.SNOWFLAKEID , new SnowFlakeIdGenerator());
register(IdStrategy.DEFAULT , new SnowFlakeIdGenerator());
register(IdStrategy.DEFAULT , new SnowFlakeIdGenerator(null));
}
public IdentifierGeneratorFactory(long datacenterId, long machineId) {
......
......@@ -17,6 +17,8 @@
package org.dromara.mybatis.jpa.id;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Calendar;
import java.util.Date;
import org.joda.time.DateTime;
......@@ -64,6 +66,27 @@ public class SnowFlakeIdGenerator implements IdentifierGenerator{
public SnowFlakeIdGenerator() {}
public SnowFlakeIdGenerator(InetAddress inetAddress) {
long id = 0L;
try {
if (null == inetAddress) {
inetAddress = InetAddress.getLocalHost();
}
NetworkInterface network = NetworkInterface.getByInetAddress(inetAddress);
if (null == network) {
id = 1L;
} else {
byte[] mac = network.getHardwareAddress();
if (null != mac) {
id = ((0x000000FF & (long) mac[mac.length - 2]) | (0x0000FF00 & (((long) mac[mac.length - 1]) << 8))) >> 6;
id = id % (datacenterId + 1);
}
}
} catch (Exception e) {
System.err.println(" getDatacenterId: " + e.getMessage());
}
}
public SnowFlakeIdGenerator(long datacenterId, long machineId) {
if (datacenterId > MAX_DATACENTER_NUM || datacenterId < 0) {
throw new IllegalArgumentException("datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0");
......
......@@ -60,18 +60,18 @@ public class InsertProvider <T extends JpaBaseEntity>{
||fieldColumnMapper.getFieldType().startsWith("byte")
)
&& BeanUtil.getValue(entity, fieldColumnMapper.getFieldName())==null
&& fieldColumnMapper.getGeneratedValue()==null) {
&& fieldColumnMapper.getGeneratedValue() == null) {
//skip null field value
_logger.trace("skip field value is null ");
}else {
//have GeneratedValue and (value is null or eq "")
if(fieldColumnMapper.getGeneratedValue()!=null
if(fieldColumnMapper.getGeneratedValue() != null
&& (
BeanUtil.get(entity, fieldColumnMapper.getFieldName()) == null
|| BeanUtil.get(entity, fieldColumnMapper.getFieldName()) == ""
)) {
GeneratedValue generatedValue=listFields.get(i).getGeneratedValue();
if(generatedValue.strategy()==GenerationType.AUTO) {
GeneratedValue generatedValue = listFields.get(i).getGeneratedValue();
if(generatedValue.strategy() == GenerationType.AUTO) {
if(MapperMetadata.identifierGeneratorFactory.getGeneratorStrategyMap()
.containsKey(generatedValue.generator().toLowerCase())) {
BeanUtil.set(entity,
......
......@@ -23,7 +23,6 @@ import org.dromara.mybatis.jpa.persistence.JpaBaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册