胖胖的枫叶
主页
博客
知识图谱
产品设计
数据分析
企业架构
项目管理
效率工具
全栈开发
后端
前端
测试
运维
数据
面试
  • openJdk-docs
  • spring-projects-docs
  • mysql-docs
  • redis-commands
  • redis-projects
  • apache-rocketmq
  • docker-docs
  • mybatis-docs
  • netty-docs
  • journaldev
  • geeksforgeeks
  • 浮生若梦
  • 后端进阶
  • 并发编程网
  • 英语肌肉记忆锻炼软件
  • 墨菲安全
  • Redisson-docs
  • jmh-Visual
  • 美团技术
  • MavenSearch
主页
博客
知识图谱
产品设计
数据分析
企业架构
项目管理
效率工具
全栈开发
后端
前端
测试
运维
数据
面试
  • openJdk-docs
  • spring-projects-docs
  • mysql-docs
  • redis-commands
  • redis-projects
  • apache-rocketmq
  • docker-docs
  • mybatis-docs
  • netty-docs
  • journaldev
  • geeksforgeeks
  • 浮生若梦
  • 后端进阶
  • 并发编程网
  • 英语肌肉记忆锻炼软件
  • 墨菲安全
  • Redisson-docs
  • jmh-Visual
  • 美团技术
  • MavenSearch
  • 博客

    • 博客迁移说明
    • 2024年
    • 2023年
    • 2022年
    • 2021年
    • 2020年
    • 2019年
    • 2018年

Mybatis使用Snowflake生成主键

  • 在某些业务中为了安全已经扩展性需要弃用mysql自增id。采用Snowflake生成方式。
  • 全局唯一性,不能出现重复的id。
  • 趋势递增,MysqlInnoDB引擎使用的是是聚集索引,使用B-tree的数据结构来存储索引数据。尽量使用有序的主键保证写入性能。
  • 单调递增:保证下一个ID一定大于上一个ID,例如事务版本号、排序等特殊需求。
  • id是无序的,连续的id容易被社会工程。

开发思路

  • 创建BaseEntity,将基础字段存放此处。实体基类。
  • Snowflake工具类,生成SnowflakeId。
  • MybatisInterceptor,用于拦截sql执行,根据Insert、update语句拦截。并修改BaseEntity参数。

效果

  • http://localhost:9002/mybatis/ 每次刷新这个地址都会新增一条记录

源码地址

Java实现代码

  • 目录
├── Dockerfile
├── docker-compose.yml # docker-compose 文件 测试环境使用
├── docker-config 
│   ├── mysql
│   │   ├── init
│   │   │   └── 1_init.sql # 数据库初始化文件
│   │   └── my.cnf 
│   └── pwd.txt
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── cn
    │   │       └── z201
    │   │           └── mybatis
    │   │               ├── AccountToolService.java
    │   │               ├── AppApplication.java
    │   │               ├── AppApplicationController.java
    │   │               ├── dao
    │   │               │   └── AccountDao.java
    │   │               ├── entity
    │   │               │   └── Account.java
    │   │               └── mybatis
    │   │                   ├── BaseEntity.java # 实体基类
    │   │                   ├── MybatisConfig.java # mybatis配置
    │   │                   ├── MybatisInterceptor.java # mybatis拦截器
    │   │                   └── SnowflakeTool.java # 雪花算法生成器
    │   └── resources
    │       ├── application-dev.yml
    │       ├── application-test.yml
    │       ├── application.yml
    │       ├── logback.xml
    │       └── mapper
    └── test
        └── java
            └── cn
                └── z201
                    └── mybatis
                        ├── AppApplicationTest.java
                        └── CodeGeneratorTest.java

pom.xml依赖

  • Spring boot 2.4.*
  • Mybatis plus 3.*

BaseEntity基础类

package cn.z201.quick.dev.app.config.mybatis;

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;

/**
 * @author z201.coding@gmail.com
 **/
@Data
public class BaseEntity {

    /**
     * Entity基类字段,这里是数据库的字段
     */
    public static final String[] BASE_ENTITY = {"id", "is_enable", "create_time", "update_time"};

    /**
     * 主键
     */
    private Long id;

    /**
     * 数据是否有效 1 有效 0 无效
     */
    @TableField("is_enable")
    private Boolean isEnable;

    /**
     * 创建时间
     */
    @TableField("create_time")
    private Long createTime;

    /**
     * 更新时间
     */
    @TableField("update_time")
    private Long updateTime;


}

SnowflakeTool工具类



import java.time.Clock;

/**
 * @author z201.coding@gmail.com
 **/
public class SnowflakeTool {

    /**
     * 开始时间截,这里用系统的时间戳
     */
    private final long startTime = 1420041600000L;

    /**
     * 机器id所占的位数 5 位
     */
    private final static long WORKER_ID_BITS = 5L;
    /**
     * 序列在id中占的位数
     */
    private final static long SEQUENCE_BITS = 12L;
    /**
     * 数据标识id所占的位数
     */
    private final long DATA_CENTER_ID_BITS = 5L;

    /**
     * 数据中心最大数量。支持的最大机器id,结果是31 (这个移位算法可以很快计算出几位二进制数所能表示的最大十进制数)
     */
    private final long maxWorkerId = -1L ^ (-1L << WORKER_ID_BITS); //

    /**
     * 支持的最大数据标识id,结果是31
     */
    private final long maxDatacenterId = -1L ^ (-1L << DATA_CENTER_ID_BITS);

    /**
     * 机器ID向左移12位
     */
    private final long workerIdShift = SEQUENCE_BITS;

    /**
     * 数据标识id向左移17位(12+5)
     */
    private final long datacenterIdShift = SEQUENCE_BITS + WORKER_ID_BITS;

    /**
     * 时间截向左移22位(5+5+12)
     */
    private final long timestampLeftShift = SEQUENCE_BITS + WORKER_ID_BITS + DATA_CENTER_ID_BITS;

    /**
     * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095)
     */
    private final long sequenceMask = -1L ^ (-1L << SEQUENCE_BITS);

    /**
     * 工作机器ID(0~31)
     */
    private long workerId;

    /**
     * 数据中心ID(0~31)
     */
    private long datacenterId;

    /**
     * 毫秒内序列(0~4095)
     */
    private long sequence = 0L;

    /**
     * 上次生成ID的时间截
     */
    private long lastTimestamp = -1L;

    /**
     * 禁止参的构造,规避创建对象时候使用默认的机器码
     */
    private SnowflakeTool() {
    }

    private static class SingletonHolder {
        private static final SnowflakeTool INSTANCE = new SnowflakeTool(0, 0);
    }

    public static final SnowflakeTool getInstance() {
        return SingletonHolder.INSTANCE;
    }

    /**
     * 构造函数
     *
     * @param workerId     工作ID (0~31)
     * @param datacenterId 数据中心ID (0~31)
     */
    private SnowflakeTool(long workerId, long datacenterId) {
        if (workerId > maxWorkerId || workerId < 0) {
            throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
        }
        if (datacenterId > maxDatacenterId || datacenterId < 0) {
            throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
        }
        this.workerId = workerId;
        this.datacenterId = datacenterId;
    }

    /**
     * 获得下一个ID (该方法是线程安全的)
     *
     * @return SnowflakeId
     */
    public synchronized long nextId() {
        long timestamp = Clock.systemDefaultZone().millis();
        //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
        if (timestamp < lastTimestamp) {
            throw new RuntimeException(
                    String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
        }
        //如果是同一时间生成的,则进行毫秒内序列
        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) & sequenceMask;
            //毫秒内序列溢出,序列数已经达到最大
            if (sequence == 0) {
                //阻塞到下一个毫秒,获得新的时间戳
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {
            //时间戳改变,毫秒内序列重置
            sequence = 0L;
        }
        //上次生成ID的时间截
        lastTimestamp = timestamp;

        //移位并通过或运算拼到一起组成64位的ID
        return ((timestamp - startTime) << timestampLeftShift) // 时间戳部分
                | (datacenterId << datacenterIdShift) // 数据中心部分
                | (workerId << workerIdShift) // 机器标识符部分
                | sequence; // 序列号部分
    }

    /**
     * 阻塞到下一个毫秒,直到获得新的时间戳
     *
     * @param lastTimestamp 上次生成ID的时间截
     * @return 当前时间戳
     */
    protected long tilNextMillis(long lastTimestamp) {
        long timestamp = Clock.systemDefaultZone().millis();
        while (timestamp <= lastTimestamp) {
            timestamp = Clock.systemDefaultZone().millis();
        }
        return timestamp;
    }


}

MybatisInterceptor 拦截器



import cn.hutool.core.lang.Validator;
import cn.z201.quick.dev.app.utils.SnowflakeTool;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.baomidou.mybatisplus.core.toolkit.EncryptUtils;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.time.Clock;
import java.util.*;

/**
 * @author z201.coding@gmail.com
 **/
@Slf4j
@Intercepts({
        @Signature(type = Executor.class, method = "update", args = {
                MappedStatement.class, Object.class
        }),
})
@Component // 可以直接在插件上增加Component 
public class MybatisInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Long startTime = Clock.systemDefaultZone().millis();
        try {
            Object[] args = invocation.getArgs();
            MappedStatement mappedStatement = (MappedStatement) args[0];
            Object entity = args[1];
            if (SqlCommandType.INSERT.name().equalsIgnoreCase(mappedStatement.getSqlCommandType().name())) {
                // 获取实体集合
                List<Object> entitySet = getEntitySet(entity);
                // 批量设置id
                for (Object object : entitySet) {
                    insertProcess(object);
                }
            } else if (SqlCommandType.UPDATE.name().equalsIgnoreCase(mappedStatement.getSqlCommandType().name())) {
                // 获取实体集合
                List<Object> entitySet = getEntitySet(entity);
                for (Object object : entitySet) {
                    updateProcess(object);
                }
            }
            return invocation.proceed();
        } finally {
            Long timeConsuming = Clock.systemDefaultZone().millis() - startTime;
            log.info("SQL RunTime {} ms", timeConsuming);
        }
    }

    /**
     * object是需要插入的实体数据,它可能是对象,也可能是批量插入的对象。
     * 如果是单个对象,那么object就是当前对象
     * 如果是批量插入对象,那么object就是一个map集合,key值为"list",value为ArrayList集合对象
     */
    private List<Object> getEntitySet(Object object) {
        List<Object> set = new ArrayList<>();
        if (object instanceof Map) {
            Iterator entries = ((Map<?, ?>) object).entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry entry = (Map.Entry) entries.next();
                Object value = entry.getValue();
                if (value instanceof Collection) {
                    set.addAll((Collection<?>) value);
                } else {
                    set.add(value);
                }
            }
        } else {
            //单个插入对象
            set.add(object);
        }
        return set;
    }

    private void insertProcess(Object object) {
        Long time = Clock.systemDefaultZone().millis();
        if (object instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) object;
            baseEntity.setId(SnowflakeTool.getInstance().nextId());
            if (Validator.isNull(baseEntity.getIsEnable())) {
                baseEntity.setIsEnable(true);
            }
            if (Validator.isNull(baseEntity.getCreateTime())) {
                baseEntity.setCreateTime(time);
            }
            if (Validator.isNull(baseEntity.getUpdateTime())) {
                baseEntity.setUpdateTime(time);
            }
        }
    }

    private void updateProcess(Object object) {
        Long time = Clock.systemDefaultZone().millis();
        if (object instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) object;
            if (Validator.isNull(baseEntity.getUpdateTime())) {
                baseEntity.setUpdateTime(time);
            }
        }
    }

}

编码启用拦截器

package cn.z201.mybatis.mybatis;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author z201.coding@gmail.com
 **/
@Configuration
public class MybatisConfig {

    /**
     * #开启返回map结果集的下划线转驼峰
     *
     * @return
     */
    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return configuration -> configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }

    //     可以直接在插件上增加Component 也可以在此处声明
    @Bean
    public String localInterceptor(SqlSessionFactory sqlSessionFactory) {
        //实例化插件
        MybatisInterceptor sqlInterceptor = new MybatisInterceptor();
        sqlSessionFactory.getConfiguration().addInterceptor(sqlInterceptor);
        return "interceptor";
    }

}


测试入口

package cn.z201.mybatis;

import cn.z201.mybatis.dao.AccountDao;
import cn.z201.mybatis.entity.Account;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author z201.coding@gmail.com
 **/
@RestController
public class AppApplicationController {

    @Resource
    AccountDao accountDao;

    @Autowired
    AccountToolService accountToolService;

    @RequestMapping(value = "")
    public Object index() {
        accountDao.insert(accountToolService.create());
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.last("ORDER BY id DESC  LIMIT 0 , 5");
        Map<String, Object> data = new HashMap<>();
        data.put("code", "200");
        data.put("data", accountDao.selectList(accountQueryWrapper));
        return data;
    }
}

END

Last Updated:
Contributors: 庆峰