分布式ID选型对比(2)

数据库号段模式

一, 引入依赖:


    mysql
    mysql-connector-java
    8.0.19


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.2.2

二, 新建表

CREATE TABLE `id_generator` (
  `id` int NOT NULL,
  `max_id` bigint NOT NULL COMMENT '当前最大id',
  `step` int NOT NULL COMMENT '号段的布长',
  `biz_type` int NOT NULL COMMENT '业务类型',
  `version` int NOT NULL COMMENT '版本号',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

三, resources目录下编写自动生成代码配置文件: generatorConfig.xml





    
    

    
        
            
            
        

        
        
        

        
        
            
        

        
        
            
            
            
            
        

        
        
            
        

        
        
            
        
        
        

application.properties数据库配置:

#   ???url
spring.datasource.url=jdbc:mysql://localhost:3306/generate_id?characterEncoding=utf8&serverTimezone=UTC    
#  ??????
spring.datasource.username=root
#  ?????
spring.datasource.password=root
#  ?????
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
###################################### MyBatis ??######################################
# ?? mapper.xml ???
mybatis.mapper-locations=classpath:mybatis/*.xml
#????????,?????????????? mapper.xml ??????????????
mybatis.type-aliases-package=net.biancheng.www.bean
#???????????????????
mybatis.configuration.map-underscore-to-camel-case=true

四, 编写层级代码

1, 对象 IdGenerator

package org.com.spi.model;

public class IdGenerator {
    private Integer id;

    private Long maxId;

    private Integer step;

    private Integer bizType;

    private Integer version;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Long getMaxId() {
        return maxId;
    }

    public void setMaxId(Long maxId) {
        this.maxId = maxId;
    }

    public Integer getStep() {
        return step;
    }

    public void setStep(Integer step) {
        this.step = step;
    }

    public Integer getBizType() {
        return bizType;
    }

    public void setBizType(Integer bizType) {
        this.bizType = bizType;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }
}

IdGeneratorExample:

package org.com.spi.model;

import java.util.ArrayList;
import java.util.List;

public class IdGeneratorExample {
    protected String orderByClause;

    protected boolean distinct;

    protected List oredCriteria;

    public IdGeneratorExample() {
        oredCriteria = new ArrayList();
    }

    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }

    public String getOrderByClause() {
        return orderByClause;
    }

    public void setDistinct(boolean distinct) {
        this.distinct = distinct;
    }

    public boolean isDistinct() {
        return distinct;
    }

    public List getOredCriteria() {
        re

你可能感兴趣的:(框架部分,特定功能,springboot,分布式,分布式ID)