行业知识
分布式ID选型对比(2)
2026-11-03 04:56:48
来源:互联网转载
数据库号段模式
一, 引入���赖:
()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数据库配置:
# ???urlspring.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() { return oredCriteria; } public void or(Criteria criteria) { oredCriteria.add(criteria); } public Criteria or() { Criteria criteria = createCriteriaInternal(); oredCriteria.add(criteria); return criteria; } public Criteria createCriteria() { Criteria criteria = createCriteriaInternal(); if (oredCriteria.size() == 0) { oredCriteria.add(criteria); } return criteria; } protected Criteria createCriteriaInternal() { Criteria criteria = new Criteria(); return criteria; } public void clear() { oredCriteria.clear(); orderByClause = null; distinct = false; } protected abstract static class GeneratedCriteria { protected List criteria; protected GeneratedCriteria() { super(); criteria = new ArrayList(); } public boolean isValid() { return criteria.size() > 0; } public List getAllCriteria() { return criteria; } public List getCriteria() { return criteria; } protected void addCriterion(String condition) { if (condition == null) { throw new RuntimeException("Value for condition cannot be null"); } criteria.add(new Criterion(condition)); } protected void addCriterion(String condition, Object value, String property) { if (value == null) { throw new RuntimeException("Value for " + property + " cannot be null"); } criteria.add(new Criterion(condition, value)); } protected void addCriterion(String condition, Object value1, Object value2, String property) { if (value1 == null || value2 == null) { throw new RuntimeException("Between values for " + property + " cannot be null"); } criteria.add(new Criterion(condition, value1, value2)); } public Criteria andIdIsNull() { addCriterion("id is null"); return (Criteria) this; } public Criteria andIdIsNotNull() { addCriterion("id is not null"); return (Criteria) this; } public Criteria andIdEqualTo(Integer value) { addCriterion("id =", value, "id"); return (Criteria) this; } public Criteria andIdNotEqualTo(Integer value) { addCriterion("id ", value, "id"); return (Criteria) this; } public Criteria andIdGreaterThan(Integer value) { addCriterion("id >", value, "id"); return (Criteria) this; } public Criteria andIdGreaterThanOrEqualTo(Integer value) { addCriterion("id >=", value, "id"); return (Criteria) this; } public Criteria andIdLessThan(Integer value) { addCriterion("id ", value, "maxId"); return (Criteria) this; } public Criteria andMaxIdGreaterThanOrEqualTo(Long value) { addCriterion("max_id >=", value, "maxId"); return (Criteria) this; } public Criteria andMaxIdLessThan(Long value) { addCriterion("max_id ", value, "step"); return (Criteria) this; } public Criteria andStepGreaterThanOrEqualTo(Integer value) { addCriterion("step >=", value, "step"); return (Criteria) this; } public Criteria andStepLessThan(Integer value) { addCriterion("step ", value, "bizType"); return (Criteria) this; } public Criteria andBizTypeGreaterThanOrEqualTo(Integer value) { addCriterion("biz_type >=", value, "bizType"); return (Criteria) this; } public Criteria andBizTypeLessThan(Integer value) { addCriterion("biz_type ", value, "version"); return (Criteria) this; } public Criteria andVersionGreaterThanOrEqualTo(Integer value) { addCriterion("version >=", value, "version"); return (Criteria) this; } public Criteria andVersionLessThan(Integer value) { addCriterion("version
上一篇:沐珩的正确读音与名字解析揭秘
下一篇:萤石云视频监控怎么重新设置主机