• 分布式ID选型对比(2)


    数据库号段模式

    一, 引入依赖:

    1. mysql
    2. mysql-connector-java
    3. 8.0.19
    4. org.mybatis.spring.boot
    5. mybatis-spring-boot-starter
    6. 2.2.2

    二, 新建表

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

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

    1. "1.0" encoding="UTF-8"?>
    2. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    3. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    4. "/Users/lizhihao/.m2/repository/mysql/mysql-connector-java/8.0.19/mysql-connector-java-8.0.19.jar"/>
    5. "testTables" targetRuntime="MyBatis3">
    6. "suppressAllComments" value="true"/>
    7. "com.mysql.cj.jdbc.Driver"
    8. connectionURL="jdbc:mysql://localhost:3306/generate_id" userId="root"
    9. password="root">
    10. "forceBigDecimals" value="false"/>
    11. "org.com.spi.model" targetProject="src/main/java">
    12. "enableSubPackages" value="false"/>
    13. "trimStrings" value="true"/>
    14. "mybatis" targetProject="src/main/resources">
    15. "enableSubPackages" value="false"/>
    16. "XMLMAPPER" targetPackage="org.com.spi.dao" targetProject="src/main/java">
    17. "enableSubPackages" value="false"/>
    18. "worker_node">

    application.properties数据库配置:

    1. # ???url
    2. spring.datasource.url=jdbc:mysql://localhost:3306/generate_id?characterEncoding=utf8&serverTimezone=UTC
    3. # ??????
    4. spring.datasource.username=root
    5. # ?????
    6. spring.datasource.password=root
    7. # ?????
    8. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    9. ###################################### MyBatis ??######################################
    10. # ?? mapper.xml ???
    11. mybatis.mapper-locations=classpath:mybatis/*.xml
    12. #????????,?????????????? mapper.xml ??????????????
    13. mybatis.type-aliases-package=net.biancheng.www.bean
    14. #???????????????????
    15. mybatis.configuration.map-underscore-to-camel-case=true

    四, 编写层级代码

    1, 对象 IdGenerator

    1. package org.com.spi.model;
    2. public class IdGenerator {
    3. private Integer id;
    4. private Long maxId;
    5. private Integer step;
    6. private Integer bizType;
    7. private Integer version;
    8. public Integer getId() {
    9. return id;
    10. }
    11. public void setId(Integer id) {
    12. this.id = id;
    13. }
    14. public Long getMaxId() {
    15. return maxId;
    16. }
    17. public void setMaxId(Long maxId) {
    18. this.maxId = maxId;
    19. }
    20. public Integer getStep() {
    21. return step;
    22. }
    23. public void setStep(Integer step) {
    24. this.step = step;
    25. }
    26. public Integer getBizType() {
    27. return bizType;
    28. }
    29. public void setBizType(Integer bizType) {
    30. this.bizType = bizType;
    31. }
    32. public Integer getVersion() {
    33. return version;
    34. }
    35. public void setVersion(Integer version) {
    36. this.version = version;
    37. }
    38. }

    IdGeneratorExample:

    1. package org.com.spi.model;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. public class IdGeneratorExample {
    5. protected String orderByClause;
    6. protected boolean distinct;
    7. protected List oredCriteria;
    8. public IdGeneratorExample() {
    9. oredCriteria = new ArrayList();
    10. }
    11. public void setOrderByClause(String orderByClause) {
    12. this.orderByClause = orderByClause;
    13. }
    14. public String getOrderByClause() {
    15. return orderByClause;
    16. }
    17. public void setDistinct(boolean distinct) {
    18. this.distinct = distinct;
    19. }
    20. public boolean isDistinct() {
    21. return distinct;
    22. }
    23. public List getOredCriteria() {
    24. return oredCriteria;
    25. }
    26. public void or(Criteria criteria) {
    27. oredCriteria.add(criteria);
    28. }
    29. public Criteria or() {
    30. Criteria criteria = createCriteriaInternal();
    31. oredCriteria.add(criteria);
    32. return criteria;
    33. }
    34. public Criteria createCriteria() {
    35. Criteria criteria = createCriteriaInternal();
    36. if (oredCriteria.size() == 0) {
    37. oredCriteria.add(criteria);
    38. }
    39. return criteria;
    40. }
    41. protected Criteria createCriteriaInternal() {
    42. Criteria criteria = new Criteria();
    43. return criteria;
    44. }
    45. public void clear() {
    46. oredCriteria.clear();
    47. orderByClause = null;
    48. distinct = false;
    49. }
    50. protected abstract static class GeneratedCriteria {
    51. protected List criteria;
    52. protected GeneratedCriteria() {
    53. super();
    54. criteria = new ArrayList();
    55. }
    56. public boolean isValid() {
    57. return criteria.size() > 0;
    58. }
    59. public List getAllCriteria() {
    60. return criteria;
    61. }
    62. public List getCriteria() {
    63. return criteria;
    64. }
    65. protected void addCriterion(String condition) {
    66. if (condition == null) {
    67. throw new RuntimeException("Value for condition cannot be null");
    68. }
    69. criteria.add(new Criterion(condition));
    70. }
    71. protected void addCriterion(String condition, Object value, String property) {
    72. if (value == null) {
    73. throw new RuntimeException("Value for " + property + " cannot be null");
    74. }
    75. criteria.add(new Criterion(condition, value));
    76. }
    77. protected void addCriterion(String condition, Object value1, Object value2, String property) {
    78. if (value1 == null || value2 == null) {
    79. throw new RuntimeException("Between values for " + property + " cannot be null");
    80. }
    81. criteria.add(new Criterion(condition, value1, value2));
    82. }
    83. public Criteria andIdIsNull() {
    84. addCriterion("id is null");
    85. return (Criteria) this;
    86. }
    87. public Criteria andIdIsNotNull() {
    88. addCriterion("id is not null");
    89. return (Criteria) this;
    90. }
    91. public Criteria andIdEqualTo(Integer value) {
    92. addCriterion("id =", value, "id");
    93. return (Criteria) this;
    94. }
    95. public Criteria andIdNotEqualTo(Integer value) {
    96. addCriterion("id <>", value, "id");
    97. return (Criteria) this;
    98. }
    99. public Criteria andIdGreaterThan(Integer value) {
    100. addCriterion("id >", value, "id");
    101. return (Criteria) this;
    102. }
    103. public Criteria andIdGreaterThanOrEqualTo(Integer value) {
    104. addCriterion("id >=", value, "id");
    105. return (Criteria) this;
    106. }
    107. public Criteria andIdLessThan(Integer value) {
    108. addCriterion("id <", value, "id");
    109. return (Criteria) this;
    110. }
    111. public Criteria andIdLessThanOrEqualTo(Integer value) {
    112. addCriterion("id <=", value, "id");
    113. return (Criteria) this;
    114. }
    115. public Criteria andIdIn(List values) {
    116. addCriterion("id in", values, "id");
    117. return (Criteria) this;
    118. }
    119. public Criteria andIdNotIn(List values) {
    120. addCriterion("id not in", values, "id");
    121. return (Criteria) this;
    122. }
    123. public Criteria andIdBetween(Integer value1, Integer value2) {
    124. addCriterion("id between", value1, value2, "id");
    125. return (Criteria) this;
    126. }
    127. public Criteria andIdNotBetween(Integer value1, Integer value2) {
    128. addCriterion("id not between", value1, value2, "id");
    129. return (Criteria) this;
    130. }
    131. public Criteria andMaxIdIsNull() {
    132. addCriterion("max_id is null");
    133. return (Criteria) this;
    134. }
    135. public Criteria andMaxIdIsNotNull() {
    136. addCriterion("max_id is not null");
    137. return (Criteria) this;
    138. }
    139. public Criteria andMaxIdEqualTo(Long value) {
    140. addCriterion("max_id =", value, "maxId");
    141. return (Criteria) this;
    142. }
    143. public Criteria andMaxIdNotEqualTo(Long value) {
    144. addCriterion("max_id <>", value, "maxId");
    145. return (Criteria) this;
    146. }
    147. public Criteria andMaxIdGreaterThan(Long value) {
    148. addCriterion("max_id >", value, "maxId");
    149. return (Criteria) this;
    150. }
    151. public Criteria andMaxIdGreaterThanOrEqualTo(Long value) {
    152. addCriterion("max_id >=", value, "maxId");
    153. return (Criteria) this;
    154. }
    155. public Criteria andMaxIdLessThan(Long value) {
    156. addCriterion("max_id <", value, "maxId");
    157. return (Criteria) this;
    158. }
    159. public Criteria andMaxIdLessThanOrEqualTo(Long value) {
    160. addCriterion("max_id <=", value, "maxId");
    161. return (Criteria) this;
    162. }
    163. public Criteria andMaxIdIn(List values) {
    164. addCriterion("max_id in", values, "maxId");
    165. return (Criteria) this;
    166. }
    167. public Criteria andMaxIdNotIn(List values) {
    168. addCriterion("max_id not in", values, "maxId");
    169. return (Criteria) this;
    170. }
    171. public Criteria andMaxIdBetween(Long value1, Long value2) {
    172. addCriterion("max_id between", value1, value2, "maxId");
    173. return (Criteria) this;
    174. }
    175. public Criteria andMaxIdNotBetween(Long value1, Long value2) {
    176. addCriterion("max_id not between", value1, value2, "maxId");
    177. return (Criteria) this;
    178. }
    179. public Criteria andStepIsNull() {
    180. addCriterion("step is null");
    181. return (Criteria) this;
    182. }
    183. public Criteria andStepIsNotNull() {
    184. addCriterion("step is not null");
    185. return (Criteria) this;
    186. }
    187. public Criteria andStepEqualTo(Integer value) {
    188. addCriterion("step =", value, "step");
    189. return (Criteria) this;
    190. }
    191. public Criteria andStepNotEqualTo(Integer value) {
    192. addCriterion("step <>", value, "step");
    193. return (Criteria) this;
    194. }
    195. public Criteria andStepGreaterThan(Integer value) {
    196. addCriterion("step >", value, "step");
    197. return (Criteria) this;
    198. }
    199. public Criteria andStepGreaterThanOrEqualTo(Integer value) {
    200. addCriterion("step >=", value, "step");
    201. return (Criteria) this;
    202. }
    203. public Criteria andStepLessThan(Integer value) {
    204. addCriterion("step <", value, "step");
    205. return (Criteria) this;
    206. }
    207. public Criteria andStepLessThanOrEqualTo(Integer value) {
    208. addCriterion("step <=", value, "step");
    209. return (Criteria) this;
    210. }
    211. public Criteria andStepIn(List values) {
    212. addCriterion("step in", values, "step");
    213. return (Criteria) this;
    214. }
    215. public Criteria andStepNotIn(List values) {
    216. addCriterion("step not in", values, "step");
    217. return (Criteria) this;
    218. }
    219. public Criteria andStepBetween(Integer value1, Integer value2) {
    220. addCriterion("step between", value1, value2, "step");
    221. return (Criteria) this;
    222. }
    223. public Criteria andStepNotBetween(Integer value1, Integer value2) {
    224. addCriterion("step not between", value1, value2, "step");
    225. return (Criteria) this;
    226. }
    227. public Criteria andBizTypeIsNull() {
    228. addCriterion("biz_type is null");
    229. return (Criteria) this;
    230. }
    231. public Criteria andBizTypeIsNotNull() {
    232. addCriterion("biz_type is not null");
    233. return (Criteria) this;
    234. }
    235. public Criteria andBizTypeEqualTo(Integer value) {
    236. addCriterion("biz_type =", value, "bizType");
    237. return (Criteria) this;
    238. }
    239. public Criteria andBizTypeNotEqualTo(Integer value) {
    240. addCriterion("biz_type <>", value, "bizType");
    241. return (Criteria) this;
    242. }
    243. public Criteria andBizTypeGreaterThan(Integer value) {
    244. addCriterion("biz_type >", value, "bizType");
    245. return (Criteria) this;
    246. }
    247. public Criteria andBizTypeGreaterThanOrEqualTo(Integer value) {
    248. addCriterion("biz_type >=", value, "bizType");
    249. return (Criteria) this;
    250. }
    251. public Criteria andBizTypeLessThan(Integer value) {
    252. addCriterion("biz_type <", value, "bizType");
    253. return (Criteria) this;
    254. }
    255. public Criteria andBizTypeLessThanOrEqualTo(Integer value) {
    256. addCriterion("biz_type <=", value, "bizType");
    257. return (Criteria) this;
    258. }
    259. public Criteria andBizTypeIn(List values) {
    260. addCriterion("biz_type in", values, "bizType");
    261. return (Criteria) this;
    262. }
    263. public Criteria andBizTypeNotIn(List values) {
    264. addCriterion("biz_type not in", values, "bizType");
    265. return (Criteria) this;
    266. }
    267. public Criteria andBizTypeBetween(Integer value1, Integer value2) {
    268. addCriterion("biz_type between", value1, value2, "bizType");
    269. return (Criteria) this;
    270. }
    271. public Criteria andBizTypeNotBetween(Integer value1, Integer value2) {
    272. addCriterion("biz_type not between", value1, value2, "bizType");
    273. return (Criteria) this;
    274. }
    275. public Criteria andVersionIsNull() {
    276. addCriterion("version is null");
    277. return (Criteria) this;
    278. }
    279. public Criteria andVersionIsNotNull() {
    280. addCriterion("version is not null");
    281. return (Criteria) this;
    282. }
    283. public Criteria andVersionEqualTo(Integer value) {
    284. addCriterion("version =", value, "version");
    285. return (Criteria) this;
    286. }
    287. public Criteria andVersionNotEqualTo(Integer value) {
    288. addCriterion("version <>", value, "version");
    289. return (Criteria) this;
    290. }
    291. public Criteria andVersionGreaterThan(Integer value) {
    292. addCriterion("version >", value, "version");
    293. return (Criteria) this;
    294. }
    295. public Criteria andVersionGreaterThanOrEqualTo(Integer value) {
    296. addCriterion("version >=", value, "version");
    297. return (Criteria) this;
    298. }
    299. public Criteria andVersionLessThan(Integer value) {
    300. addCriterion("version <", value, "version");
    301. return (Criteria) this;
    302. }
    303. public Criteria andVersionLessThanOrEqualTo(Integer value) {
    304. addCriterion("version <=", value, "version");
    305. return (Criteria) this;
    306. }
    307. public Criteria andVersionIn(List values) {
    308. addCriterion("version in", values, "version");
    309. return (Criteria) this;
    310. }
    311. public Criteria andVersionNotIn(List values) {
    312. addCriterion("version not in", values, "version");
    313. return (Criteria) this;
    314. }
    315. public Criteria andVersionBetween(Integer value1, Integer value2) {
    316. addCriterion("version between", value1, value2, "version");
    317. return (Criteria) this;
    318. }
    319. public Criteria andVersionNotBetween(Integer value1, Integer value2) {
    320. addCriterion("version not between", value1, value2, "version");
    321. return (Criteria) this;
    322. }
    323. }
    324. public static class Criteria extends GeneratedCriteria {
    325. protected Criteria() {
    326. super();
    327. }
    328. }
    329. public static class Criterion {
    330. private String condition;
    331. private Object value;
    332. private Object secondValue;
    333. private boolean noValue;
    334. private boolean singleValue;
    335. private boolean betweenValue;
    336. private boolean listValue;
    337. private String typeHandler;
    338. public String getCondition() {
    339. return condition;
    340. }
    341. public Object getValue() {
    342. return value;
    343. }
    344. public Object getSecondValue() {
    345. return secondValue;
    346. }
    347. public boolean isNoValue() {
    348. return noValue;
    349. }
    350. public boolean isSingleValue() {
    351. return singleValue;
    352. }
    353. public boolean isBetweenValue() {
    354. return betweenValue;
    355. }
    356. public boolean isListValue() {
    357. return listValue;
    358. }
    359. public String getTypeHandler() {
    360. return typeHandler;
    361. }
    362. protected Criterion(String condition) {
    363. super();
    364. this.condition = condition;
    365. this.typeHandler = null;
    366. this.noValue = true;
    367. }
    368. protected Criterion(String condition, Object value, String typeHandler) {
    369. super();
    370. this.condition = condition;
    371. this.value = value;
    372. this.typeHandler = typeHandler;
    373. if (value instanceof List) {
    374. this.listValue = true;
    375. } else {
    376. this.singleValue = true;
    377. }
    378. }
    379. protected Criterion(String condition, Object value) {
    380. this(condition, value, null);
    381. }
    382. protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
    383. super();
    384. this.condition = condition;
    385. this.value = value;
    386. this.secondValue = secondValue;
    387. this.typeHandler = typeHandler;
    388. this.betweenValue = true;
    389. }
    390. protected Criterion(String condition, Object value, Object secondValue) {
    391. this(condition, value, secondValue, null);
    392. }
    393. }
    394. }

    2, dao层接口: IdGeneratorMapper

    1. package org.com.spi.dao;
    2. import java.util.List;
    3. import org.apache.ibatis.annotations.Mapper;
    4. import org.apache.ibatis.annotations.Param;
    5. import org.com.spi.model.IdGenerator;
    6. import org.com.spi.model.IdGeneratorExample;
    7. @Mapper
    8. public interface IdGeneratorMapper {
    9. int countByExample(IdGeneratorExample example);
    10. int deleteByExample(IdGeneratorExample example);
    11. int deleteByPrimaryKey(Integer id);
    12. int insert(IdGenerator record);
    13. int insertSelective(IdGenerator record);
    14. List selectByExample(IdGeneratorExample example);
    15. IdGenerator selectByPrimaryKey(Integer id);
    16. int updateByExampleSelective(@Param("record") IdGenerator record, @Param("example") IdGeneratorExample example);
    17. int updateByExample(@Param("record") IdGenerator record, @Param("example") IdGeneratorExample example);
    18. int updateByPrimaryKeySelective(IdGenerator record);
    19. int updateByPrimaryKey(IdGenerator record);
    20. IdGenerator selectMaxId(int bizType);
    21. int updateMaxId(IdGenerator idGenerator);
    22. }

    3, controller层: GenerateIdController

    1. @Autowired
    2. IdGeneratorMapper idGeneratorMapper;
    3. @PutMapping("/save1")
    4. public int save1(@RequestBody AutoGenerate1 autoGenerate){
    5. IdGenerator idGenerator = idGeneratorMapper.selectMaxId(BizTypeEnum.GENERATE_ONE.getType());
    6. int count = 0;
    7. if(idGenerator == null){
    8. idGenerator = new IdGenerator();
    9. idGenerator.setId(1);
    10. idGenerator.setStep(4);
    11. idGenerator.setVersion(1);
    12. idGenerator.setMaxId(1L);
    13. idGenerator.setBizType(BizTypeEnum.GENERATE_ONE.getType());
    14. count = idGeneratorMapper.insertSelective(idGenerator);
    15. autoGenerate.setId(1L);
    16. }else{
    17. Long currentId = idGenerator.getMaxId() + idGenerator.getStep();
    18. autoGenerate.setId(currentId);
    19. idGenerator.setMaxId(currentId);
    20. count = idGeneratorMapper.updateMaxId(idGenerator);
    21. }
    22. if(count > 0){//id生成后才能算成功进行新增
    23. return autoGenerateMapper1.insert(autoGenerate);
    24. }
    25. return 0;
    26. }

    4, resources/mybatis目录下建: IdGeneratorMapper.xml

    1. "1.0" encoding="UTF-8" ?>
    2. "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    3. "org.com.spi.dao.IdGeneratorMapper" >
    4. "BaseResultMap" type="org.com.spi.model.IdGenerator" >
    5. "id" property="id" jdbcType="INTEGER" />
    6. "max_id" property="maxId" jdbcType="BIGINT" />
    7. "step" property="step" jdbcType="INTEGER" />
    8. "biz_type" property="bizType" jdbcType="INTEGER" />
    9. "version" property="version" jdbcType="INTEGER" />
    10. "Example_Where_Clause" >
    11. "oredCriteria" item="criteria" separator="or" >
    12. <if test="criteria.valid" >
    13. "(" suffix=")" prefixOverrides="and" >
    14. "criteria.criteria" item="criterion" >
    15. "criterion.noValue" >
    16. and ${criterion.condition}
    17. "criterion.singleValue" >
    18. and ${criterion.condition} #{criterion.value}
    19. "criterion.betweenValue" >
    20. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
    21. "criterion.listValue" >
    22. and ${criterion.condition}
    23. "criterion.value" item="listItem" open="(" close=")" separator="," >
    24. #{listItem}
    25. if>
    26. "Update_By_Example_Where_Clause" >
    27. "example.oredCriteria" item="criteria" separator="or" >
    28. <if test="criteria.valid" >
    29. "(" suffix=")" prefixOverrides="and" >
    30. "criteria.criteria" item="criterion" >
    31. "criterion.noValue" >
    32. and ${criterion.condition}
    33. "criterion.singleValue" >
    34. and ${criterion.condition} #{criterion.value}
    35. "criterion.betweenValue" >
    36. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
    37. "criterion.listValue" >
    38. and ${criterion.condition}
    39. "criterion.value" item="listItem" open="(" close=")" separator="," >
    40. #{listItem}
    41. if>
    42. "Base_Column_List" >
    43. id, max_id, step, biz_type, version
    44. "deleteByPrimaryKey" parameterType="java.lang.Integer" >
    45. delete from id_generator
    46. where id = #{id,jdbcType=INTEGER}
    47. "deleteByExample" parameterType="org.com.spi.model.IdGeneratorExample" >
    48. delete from id_generator
    49. <if test="_parameter != null" >
    50. "Example_Where_Clause" />
    51. if>
    52. "insert" parameterType="org.com.spi.model.IdGenerator" >
    53. insert into id_generator (id, max_id, step,
    54. biz_type, version)
    55. values (#{id,jdbcType=INTEGER}, #{maxId,jdbcType=BIGINT}, #{step,jdbcType=INTEGER},
    56. #{bizType,jdbcType=INTEGER}, #{version,jdbcType=INTEGER})
    57. "insertSelective" parameterType="org.com.spi.model.IdGenerator" >
    58. insert into id_generator
    59. "(" suffix=")" suffixOverrides="," >
    60. <if test="id != null" >
    61. id,
    62. if>
    63. <if test="maxId != null" >
    64. max_id,
    65. if>
    66. <if test="step != null" >
    67. step,
    68. if>
    69. <if test="bizType != null" >
    70. biz_type,
    71. if>
    72. <if test="version != null" >
    73. version,
    74. if>
    75. "values (" suffix=")" suffixOverrides="," >
    76. <if test="id != null" >
    77. #{id,jdbcType=INTEGER},
    78. if>
    79. <if test="maxId != null" >
    80. #{maxId,jdbcType=BIGINT},
    81. if>
    82. <if test="step != null" >
    83. #{step,jdbcType=INTEGER},
    84. if>
    85. <if test="bizType != null" >
    86. #{bizType,jdbcType=INTEGER},
    87. if>
    88. <if test="version != null" >
    89. #{version,jdbcType=INTEGER},
    90. if>
    91. "updateByExampleSelective" parameterType="map" >
    92. update id_generator
    93. <if test="record.id != null" >
    94. id = #{record.id,jdbcType=INTEGER},
    95. if>
    96. <if test="record.maxId != null" >
    97. max_id = #{record.maxId,jdbcType=BIGINT},
    98. if>
    99. <if test="record.step != null" >
    100. step = #{record.step,jdbcType=INTEGER},
    101. if>
    102. <if test="record.bizType != null" >
    103. biz_type = #{record.bizType,jdbcType=INTEGER},
    104. if>
    105. <if test="record.version != null" >
    106. version = #{record.version,jdbcType=INTEGER},
    107. if>
    108. <if test="_parameter != null" >
    109. "Update_By_Example_Where_Clause" />
    110. if>
    111. "updateByExample" parameterType="map" >
    112. update id_generator
    113. set id = #{record.id,jdbcType=INTEGER},
    114. max_id = #{record.maxId,jdbcType=BIGINT},
    115. step = #{record.step,jdbcType=INTEGER},
    116. biz_type = #{record.bizType,jdbcType=INTEGER},
    117. version = #{record.version,jdbcType=INTEGER}
    118. <if test="_parameter != null" >
    119. "Update_By_Example_Where_Clause" />
    120. if>
    121. "updateMaxId" parameterType="org.com.spi.model.IdGenerator" >
    122. update id_generator set max_id = #{maxId},version = version + 1
    123. where biz_type = #{bizType} and version = #{version}
    124. "updateByPrimaryKeySelective" parameterType="org.com.spi.model.IdGenerator" >
    125. update id_generator
    126. <if test="maxId != null" >
    127. max_id = #{maxId,jdbcType=BIGINT},
    128. if>
    129. <if test="step != null" >
    130. step = #{step,jdbcType=INTEGER},
    131. if>
    132. <if test="bizType != null" >
    133. biz_type = #{bizType,jdbcType=INTEGER},
    134. if>
    135. <if test="version != null" >
    136. version = #{version,jdbcType=INTEGER},
    137. if>
    138. where id = #{id,jdbcType=INTEGER}
    139. "updateByPrimaryKey" parameterType="org.com.spi.model.IdGenerator" >
    140. update id_generator
    141. set max_id = #{maxId,jdbcType=BIGINT},
    142. step = #{step,jdbcType=INTEGER},
    143. biz_type = #{bizType,jdbcType=INTEGER},
    144. version = #{version,jdbcType=INTEGER}
    145. where id = #{id,jdbcType=INTEGER}

    特点: 相较于数据库自增,扩展性更好,但缺点是每次取ID都需要加锁,性能较低.

  • 相关阅读:
    IFD-x 微型红外成像仪与手机APP连接时光学相机图像与热成像叠加说明
    Hive 导出数据到 CSV 文件
    [ASPX]常用笔记
    golang的错误处理
    巴特沃斯、切比雪夫I型、切比雪夫Ⅱ型和椭圆型滤波器的相同和不同之处
    【每日知识】进制转换:二进制、八进制、十六进制
    【毕业设计】57-基于单片机的超声波测距仿真倒车雷达系统设计(源程序+原理图工程+PCB+仿真工程+答辩论文)
    存储服务器特征是什么
    自动化驱动程序管理
    C++ - 开放地址法的哈希介绍 - 哈希表的仿函数例子
  • 原文地址:https://blog.csdn.net/qq_35255384/article/details/136415505