

- 在某些业务中为了安全已经扩展性需要弃用mysql自增id。采用Snowflake生成方式。
- 全局唯一性,不能出现重复的id。
- 趋势递增,MysqlInnoDB引擎使用的是是聚集索引,使用B-tree的数据结构来存储索引数据。尽量使用有序的主键保证写入性能。
- 单调递增:保证下一个ID一定大于上一个ID,例如事务版本号、排序等特殊需求。
- id是无序的,连续的id容易被社会工程。
- 创建BaseEntity,将基础字段存放此处。实体基类。
- Snowflake工具类,生成SnowflakeId。
- MybatisInterceptor,用于拦截sql执行,根据Insert、update语句拦截。并修改BaseEntity参数。
- http://localhost:9002/mybatis/ 每次刷新这个地址都会新增一条记录

源码地址
├── Dockerfile
├── docker-compose.yml 
├── 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 
    │   │                   ├── MybatisInterceptor.java 
    │   │                   └── SnowflakeTool.java 
    │   └── resources
    │       ├── application-dev.yml
    │       ├── application-test.yml
    │       ├── application.yml
    │       ├── logback.xml
    │       └── mapper
    └── test
        └── java
            └── cn
                └── z201
                    └── mybatis
                        ├── AppApplicationTest.java
                        └── CodeGeneratorTest.java
- Spring boot 2.4.*
- Mybatis plus 3.*
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;
}
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();
        
        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;
        }
        
        lastTimestamp = timestamp;
        
        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;
    }
}
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 
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);
                
                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;
    }
    
    @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;
    }
}