MyBatis-Plus主键策略(雪花算法16位长度的整型id,解决默认雪花算法生成19位长度id导致JS精度丢失问题)
js表达的最大整数2的53次方减1,精度丢失后面几位全是0!
主键策略
如果内置支持不满足你的需求,可实现 IKeyGenerator 接口来进行扩展.
举个栗子
@KeySequence(value = "SEQ_ORACLE_STRING_KEY", clazz = String.class)
public class YourEntity {
@TableId(value = "ID_STR", type = IdType.INPUT)
private String idStr;
}
#Spring-Boot
#方式一:使用配置类
@Bean
public IKeyGenerator keyGenerator() {
return new H2KeyGenerator();
}
#方式二:通过 MybatisPlusPropertiesCustomizer 自定义
@Bean
public MybatisPlusPropertiesCustomizer plusPropertiesCustomizer() {
return plusProperties -> plusProperties.getGlobalConfig().getDbConfig().setKeyGenerator(new H2KeyGenerator());
}
#Spring
#方式一: XML 配置
<bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
<property name="dbConfig" ref="dbConfig"/>
</bean>
<bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
<property name="keyGenerator" ref="keyGenerator"/>
</bean>
<bean id="keyGenerator" class="com.baomidou.mybatisplus.extension.incrementer.H2KeyGenerator"/>
#方式二:注解配置
@Bean
public GlobalConfig globalConfig() {
GlobalConfig conf = new GlobalConfig();
conf.setDbConfig(new GlobalConfig.DbConfig().setKeyGenerator(new H2KeyGenerator()));
return conf;
}
官方示例文章来源:https://www.toymoban.com/news/detail-400306.html
官方id generator示例https://gitee.com/baomidou/mybatis-plus-samples/tree/master/mybatis-plus-sample-id-generator文章来源地址https://www.toymoban.com/news/detail-400306.html
工程中16位长度的long整型id示例
@Component
public class CustomerIdGenerator implements IdentifierGenerator {
@Override
public Long nextId(Object entity) {
// 填充自己的Id生成器,
return IdGenerator.generateId();
}
}
import java.util.Date;
import java.util.UUID;
/**
* compressed id generator, result id not great than 53bits before 2318-06-04.
*/
public class IdGenerator {
private static IdGenerator instance = new IdGenerator(0);
public static IdGenerator initDefaultInstance(int machineId) {
instance = new IdGenerator(machineId);
return instance;
}
public static IdGenerator getInstance() {
return instance;
}
public static long generateId() {
return instance.nextId();
}
// total bits=53(max 2^53-1:9007199254740992-1)
// private final static long TIME_BIT = 40; // max: 2318-06-04
private final static long MACHINE_BIT = 5; // max 31
private final static long SEQUENCE_BIT = 8; // 256/10ms
/**
* mask/max value
*/
private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);
private final static long MACHINE_LEFT = SEQUENCE_BIT;
private final static long TIMESTMP_LEFT = MACHINE_BIT + SEQUENCE_BIT;
private long machineId;
private long sequence = 0L;
private long lastStmp = -1L;
private IdGenerator(long machineId) {
if (machineId > MAX_MACHINE_NUM || machineId < 0) {
throw new IllegalArgumentException(
"machineId can't be greater than " + MAX_MACHINE_NUM + " or less than 0");
}
this.machineId = machineId;
}
/**
* generate new ID
*
* @return
*/
public synchronized long nextId() {
long currStmp = getTimestamp();
if (currStmp < lastStmp) {
throw new RuntimeException("Clock moved backwards. Refusing to generate id");
}
if (currStmp == lastStmp) {
sequence = (sequence + 1) & MAX_SEQUENCE;
if (sequence == 0L) {
currStmp = getNextTimestamp();
}
} else {
sequence = 0L;
}
lastStmp = currStmp;
return currStmp << TIMESTMP_LEFT //
| machineId << MACHINE_LEFT //
| sequence;
}
private long getNextTimestamp() {
long mill = getTimestamp();
while (mill <= lastStmp) {
mill = getTimestamp();
}
return mill;
}
private long getTimestamp() {
// per 10ms
return System.currentTimeMillis() / 10;// 10ms
}
public static Date parseIdTimestamp(long id) {
return new Date((id >>> TIMESTMP_LEFT) * 10);
}
public static String uuid() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
}
实体类还是直接使用
@TableId(value = "id", type = IdType.ASSIGN_ID)
@TableId(value = "id", type = IdType.ASSIGN_ID)
private Long id;
到了这里,关于MyBatis-Plus主键策略(雪花算法16位长度的整型id,解决默认雪花算法生成19位长度id导致JS精度丢失问题)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!