Skip to content

Commit

Permalink
修改默认Id生成器类名,过时GlobalConfig中workerId,datacenterId属性.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Nov 6, 2019
1 parent f0d9ac8 commit 606c5f0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdGenerator;
import com.baomidou.mybatisplus.core.incrementer.SnowflakeIdGenerator;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdGenerator;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
Expand Down Expand Up @@ -257,9 +257,9 @@ public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory
public IdGenerator idGenerator() {
GlobalConfig globalConfig = this.properties.getGlobalConfig();
if (globalConfig.getWorkerId() != null && globalConfig.getDatacenterId() != null) {
return new DefaultIdGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
return new SnowflakeIdGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
}
return new DefaultIdGenerator();
return new SnowflakeIdGenerator();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.baomidou.mybatisplus.core;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdGenerator;
import com.baomidou.mybatisplus.core.incrementer.SnowflakeIdGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdGenerator;
import com.baomidou.mybatisplus.core.injector.SqlRunnerInjector;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
Expand Down Expand Up @@ -84,9 +84,9 @@ public SqlSessionFactory build(Configuration config) {
IdGenerator idGenerator = globalConfig.getIdGenerator();
if (globalConfig.getIdGenerator() == null) {
if (null != globalConfig.getWorkerId() && null != globalConfig.getDatacenterId()) {
idGenerator = new DefaultIdGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
idGenerator = new SnowflakeIdGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
} else {
idGenerator = new DefaultIdGenerator();
idGenerator = new SnowflakeIdGenerator();
}
globalConfig.setIdGenerator(idGenerator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ public class GlobalConfig implements Serializable {
private boolean banner = true;
/**
* 机器 ID 部分
*
* @see #setIdGenerator(IdGenerator)
* @deprecated 3.2.1 建议手动初始化,Spring应用直接@bean注入SnowflakeIdGenerator即可.
*/
@Deprecated
private Long workerId;
/**
* 数据标识 ID 部分
*
* @see #setIdGenerator(IdGenerator)
* @deprecated 3.2.1 建议手动初始化,Spring应用直接@bean注入SnowflakeIdGenerator即可.
*/
@Deprecated
private Long datacenterId;
/**
* 是否初始化 SqlRunner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
import com.baomidou.mybatisplus.core.toolkit.Sequence;

/**
* 默认 主键 生成器
* 雪花算法ID生成器
*
* @author [email protected]
* @date 2019/10/15
*/
public class DefaultIdGenerator implements IdGenerator {
public class SnowflakeIdGenerator implements IdGenerator {

private Sequence sequence;
private final Sequence sequence;

public DefaultIdGenerator() {
sequence = new Sequence();
public SnowflakeIdGenerator() {
this.sequence = new Sequence();
}

public DefaultIdGenerator(long workerId, long dataCenterId) {
sequence = new Sequence(workerId, dataCenterId);
public SnowflakeIdGenerator(long workerId, long dataCenterId) {
this.sequence = new Sequence(workerId, dataCenterId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.baomidou.mybatisplus.core.toolkit;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdGenerator;
import com.baomidou.mybatisplus.core.incrementer.SnowflakeIdGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdGenerator;

import java.time.LocalDateTime;
Expand All @@ -37,7 +37,7 @@ public class IdWorker {
* 主机和进程的机器码
*/
@Deprecated
private static IdGenerator ID_GENERATOR = new DefaultIdGenerator();
private static IdGenerator ID_GENERATOR = new SnowflakeIdGenerator();

/**
* 毫秒格式化时间
Expand Down Expand Up @@ -93,7 +93,7 @@ public static String getTimeId() {
*/
@Deprecated
public static void initSequence(long workerId, long dataCenterId) {
ID_GENERATOR = new DefaultIdGenerator(workerId, dataCenterId);
ID_GENERATOR = new SnowflakeIdGenerator(workerId, dataCenterId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.baomidou.mybatisplus.core;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdGenerator;
import com.baomidou.mybatisplus.core.incrementer.SnowflakeIdGenerator;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -45,7 +45,7 @@ void test() {
MappedStatement mappedStatement;
Configuration configuration = new MybatisConfiguration();
StaticSqlSource staticSqlSource = new StaticSqlSource(configuration, " ***********");
GlobalConfigUtils.getGlobalConfig(configuration).setIdGenerator(new DefaultIdGenerator()).setMetaObjectHandler(new MetaObjectHandler() {
GlobalConfigUtils.getGlobalConfig(configuration).setIdGenerator(new SnowflakeIdGenerator()).setMetaObjectHandler(new MetaObjectHandler() {
@Override
public void insertFill(MetaObject metaObject) {
setFieldValByName("insertOperator", "咩咩", metaObject);
Expand Down

0 comments on commit 606c5f0

Please sign in to comment.