当前位置:   article > 正文

分布式ID选型对比(2)

分布式ID选型对比(2)

数据库号段模式

一, 引入依赖:

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. <version>8.0.19</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.mybatis.spring.boot</groupId>
  8. <artifactId>mybatis-spring-boot-starter</artifactId>
  9. <version>2.2.2</version>
  10. </dependency>

二, 新建表

  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. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!--mysql 连接数据库jar 这里选择自己本地位置;
  7. 如果不知道maven本地仓库地址,可以使用EveryThing工具全局搜索mysql-connector-java,找到jar包位置;
  8. 也可以手动下载一个jar放在指定位置,进行引用。
  9. -->
  10. <classPathEntry location="/Users/lizhihao/.m2/repository/mysql/mysql-connector-java/8.0.19/mysql-connector-java-8.0.19.jar"/>
  11. <context id="testTables" targetRuntime="MyBatis3">
  12. <commentGenerator>
  13. <!-- 是否去除自动生成的注释,true:是,false:否 -->
  14. <property name="suppressAllComments" value="true"/>
  15. </commentGenerator>
  16. <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
  17. <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
  18. connectionURL="jdbc:mysql://localhost:3306/generate_id" userId="root"
  19. password="root">
  20. </jdbcConnection>
  21. <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
  22. NUMERIC 类型解析为java.math.BigDecimal -->
  23. <javaTypeResolver>
  24. <property name="forceBigDecimals" value="false"/>
  25. </javaTypeResolver>
  26. <!-- 指定javaBean生成的位置
  27. targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制;
  28. targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,MBG不会自动建目录
  29. -->
  30. <javaModelGenerator targetPackage="org.com.spi.model" targetProject="src/main/java">
  31. <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false;如果多个数据库改为true分目录 -->
  32. <property name="enableSubPackages" value="false"/>
  33. <!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
  34. <property name="trimStrings" value="true"/>
  35. </javaModelGenerator>
  36. <!-- 指定mapper映射文件生成的位置
  37. targetPackage、targetProject同javaModelGenerator中作用一样-->
  38. <sqlMapGenerator targetPackage="mybatis" targetProject="src/main/resources">
  39. <property name="enableSubPackages" value="false"/>
  40. </sqlMapGenerator>
  41. <!-- 指定mapper接口生成的位置
  42. targetPackage、targetProject同javaModelGenerator中作用一样
  43. -->
  44. <javaClientGenerator type="XMLMAPPER" targetPackage="org.com.spi.dao" targetProject="src/main/java">
  45. <property name="enableSubPackages" value="false"/>
  46. </javaClientGenerator>
  47. <!-- 指定数据库表
  48. domainObjectName:生成的domain类的名字,当表名和domain类的名字有差异时一定要设置,如果不设置,直接使用表名作为domain类的名字;
  49. 可以设置为somepck.domainName,那么会自动把domainName类再放到somepck包里面;
  50. -->
  51. <table tableName="worker_node"></table>
  52. <!-- <table tableName="auto_generate2" domainObjectName="AutoGenerate2"></table>-->
  53. </context>
  54. </generatorConfiguration>

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<Criteria> oredCriteria;
  8. public IdGeneratorExample() {
  9. oredCriteria = new ArrayList<Criteria>();
  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<Criteria> 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<Criterion> criteria;
  52. protected GeneratedCriteria() {
  53. super();
  54. criteria = new ArrayList<Criterion>();
  55. }
  56. public boolean isValid() {
  57. return criteria.size() > 0;
  58. }
  59. public List<Criterion> getAllCriteria() {
  60. return criteria;
  61. }
  62. public List<Criterion> 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<Integer> values) {
  116. addCriterion("id in", values, "id");
  117. return (Criteria) this;
  118. }
  119. public Criteria andIdNotIn(List<Integer> 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<Long> values) {
  164. addCriterion("max_id in", values, "maxId");
  165. return (Criteria) this;
  166. }
  167. public Criteria andMaxIdNotIn(List<Long> 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<Integer> values) {
  212. addCriterion("step in", values, "step");
  213. return (Criteria) this;
  214. }
  215. public Criteria andStepNotIn(List<Integer> 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<Integer> values) {
  260. addCriterion("biz_type in", values, "bizType");
  261. return (Criteria) this;
  262. }
  263. public Criteria andBizTypeNotIn(List<Integer> 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<Integer> values) {
  308. addCriterion("version in", values, "version");
  309. return (Criteria) this;
  310. }
  311. public Criteria andVersionNotIn(List<Integer> 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<IdGenerator> 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. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="org.com.spi.dao.IdGeneratorMapper" >
  4. <resultMap id="BaseResultMap" type="org.com.spi.model.IdGenerator" >
  5. <id column="id" property="id" jdbcType="INTEGER" />
  6. <result column="max_id" property="maxId" jdbcType="BIGINT" />
  7. <result column="step" property="step" jdbcType="INTEGER" />
  8. <result column="biz_type" property="bizType" jdbcType="INTEGER" />
  9. <result column="version" property="version" jdbcType="INTEGER" />
  10. </resultMap>
  11. <sql id="Example_Where_Clause" >
  12. <where >
  13. <foreach collection="oredCriteria" item="criteria" separator="or" >
  14. <if test="criteria.valid" >
  15. <trim prefix="(" suffix=")" prefixOverrides="and" >
  16. <foreach collection="criteria.criteria" item="criterion" >
  17. <choose >
  18. <when test="criterion.noValue" >
  19. and ${criterion.condition}
  20. </when>
  21. <when test="criterion.singleValue" >
  22. and ${criterion.condition} #{criterion.value}
  23. </when>
  24. <when test="criterion.betweenValue" >
  25. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  26. </when>
  27. <when test="criterion.listValue" >
  28. and ${criterion.condition}
  29. <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
  30. #{listItem}
  31. </foreach>
  32. </when>
  33. </choose>
  34. </foreach>
  35. </trim>
  36. </if>
  37. </foreach>
  38. </where>
  39. </sql>
  40. <sql id="Update_By_Example_Where_Clause" >
  41. <where >
  42. <foreach collection="example.oredCriteria" item="criteria" separator="or" >
  43. <if test="criteria.valid" >
  44. <trim prefix="(" suffix=")" prefixOverrides="and" >
  45. <foreach collection="criteria.criteria" item="criterion" >
  46. <choose >
  47. <when test="criterion.noValue" >
  48. and ${criterion.condition}
  49. </when>
  50. <when test="criterion.singleValue" >
  51. and ${criterion.condition} #{criterion.value}
  52. </when>
  53. <when test="criterion.betweenValue" >
  54. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  55. </when>
  56. <when test="criterion.listValue" >
  57. and ${criterion.condition}
  58. <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
  59. #{listItem}
  60. </foreach>
  61. </when>
  62. </choose>
  63. </foreach>
  64. </trim>
  65. </if>
  66. </foreach>
  67. </where>
  68. </sql>
  69. <sql id="Base_Column_List" >
  70. id, max_id, step, biz_type, version
  71. </sql>
  72. <select id="selectMaxId" parameterType="int" resultMap="BaseResultMap">
  73. select * from id_generator where biz_type = #{bizType} limit 1;
  74. </select>
  75. <select id="selectByExample" resultMap="BaseResultMap" parameterType="org.com.spi.model.IdGeneratorExample" >
  76. select
  77. <if test="distinct" >
  78. distinct
  79. </if>
  80. <include refid="Base_Column_List" />
  81. from id_generator
  82. <if test="_parameter != null" >
  83. <include refid="Example_Where_Clause" />
  84. </if>
  85. <if test="orderByClause != null" >
  86. order by ${orderByClause}
  87. </if>
  88. </select>
  89. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  90. select
  91. <include refid="Base_Column_List" />
  92. from id_generator
  93. where id = #{id,jdbcType=INTEGER}
  94. </select>
  95. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
  96. delete from id_generator
  97. where id = #{id,jdbcType=INTEGER}
  98. </delete>
  99. <delete id="deleteByExample" parameterType="org.com.spi.model.IdGeneratorExample" >
  100. delete from id_generator
  101. <if test="_parameter != null" >
  102. <include refid="Example_Where_Clause" />
  103. </if>
  104. </delete>
  105. <insert id="insert" parameterType="org.com.spi.model.IdGenerator" >
  106. insert into id_generator (id, max_id, step,
  107. biz_type, version)
  108. values (#{id,jdbcType=INTEGER}, #{maxId,jdbcType=BIGINT}, #{step,jdbcType=INTEGER},
  109. #{bizType,jdbcType=INTEGER}, #{version,jdbcType=INTEGER})
  110. </insert>
  111. <insert id="insertSelective" parameterType="org.com.spi.model.IdGenerator" >
  112. insert into id_generator
  113. <trim prefix="(" suffix=")" suffixOverrides="," >
  114. <if test="id != null" >
  115. id,
  116. </if>
  117. <if test="maxId != null" >
  118. max_id,
  119. </if>
  120. <if test="step != null" >
  121. step,
  122. </if>
  123. <if test="bizType != null" >
  124. biz_type,
  125. </if>
  126. <if test="version != null" >
  127. version,
  128. </if>
  129. </trim>
  130. <trim prefix="values (" suffix=")" suffixOverrides="," >
  131. <if test="id != null" >
  132. #{id,jdbcType=INTEGER},
  133. </if>
  134. <if test="maxId != null" >
  135. #{maxId,jdbcType=BIGINT},
  136. </if>
  137. <if test="step != null" >
  138. #{step,jdbcType=INTEGER},
  139. </if>
  140. <if test="bizType != null" >
  141. #{bizType,jdbcType=INTEGER},
  142. </if>
  143. <if test="version != null" >
  144. #{version,jdbcType=INTEGER},
  145. </if>
  146. </trim>
  147. </insert>
  148. <select id="countByExample" parameterType="org.com.spi.model.IdGeneratorExample" resultType="java.lang.Integer" >
  149. select count(*) from id_generator
  150. <if test="_parameter != null" >
  151. <include refid="Example_Where_Clause" />
  152. </if>
  153. </select>
  154. <update id="updateByExampleSelective" parameterType="map" >
  155. update id_generator
  156. <set >
  157. <if test="record.id != null" >
  158. id = #{record.id,jdbcType=INTEGER},
  159. </if>
  160. <if test="record.maxId != null" >
  161. max_id = #{record.maxId,jdbcType=BIGINT},
  162. </if>
  163. <if test="record.step != null" >
  164. step = #{record.step,jdbcType=INTEGER},
  165. </if>
  166. <if test="record.bizType != null" >
  167. biz_type = #{record.bizType,jdbcType=INTEGER},
  168. </if>
  169. <if test="record.version != null" >
  170. version = #{record.version,jdbcType=INTEGER},
  171. </if>
  172. </set>
  173. <if test="_parameter != null" >
  174. <include refid="Update_By_Example_Where_Clause" />
  175. </if>
  176. </update>
  177. <update id="updateByExample" parameterType="map" >
  178. update id_generator
  179. set id = #{record.id,jdbcType=INTEGER},
  180. max_id = #{record.maxId,jdbcType=BIGINT},
  181. step = #{record.step,jdbcType=INTEGER},
  182. biz_type = #{record.bizType,jdbcType=INTEGER},
  183. version = #{record.version,jdbcType=INTEGER}
  184. <if test="_parameter != null" >
  185. <include refid="Update_By_Example_Where_Clause" />
  186. </if>
  187. </update>
  188. <update id="updateMaxId" parameterType="org.com.spi.model.IdGenerator" >
  189. update id_generator set max_id = #{maxId},version = version + 1
  190. where biz_type = #{bizType} and version = #{version}
  191. </update>
  192. <update id="updateByPrimaryKeySelective" parameterType="org.com.spi.model.IdGenerator" >
  193. update id_generator
  194. <set >
  195. <if test="maxId != null" >
  196. max_id = #{maxId,jdbcType=BIGINT},
  197. </if>
  198. <if test="step != null" >
  199. step = #{step,jdbcType=INTEGER},
  200. </if>
  201. <if test="bizType != null" >
  202. biz_type = #{bizType,jdbcType=INTEGER},
  203. </if>
  204. <if test="version != null" >
  205. version = #{version,jdbcType=INTEGER},
  206. </if>
  207. </set>
  208. where id = #{id,jdbcType=INTEGER}
  209. </update>
  210. <update id="updateByPrimaryKey" parameterType="org.com.spi.model.IdGenerator" >
  211. update id_generator
  212. set max_id = #{maxId,jdbcType=BIGINT},
  213. step = #{step,jdbcType=INTEGER},
  214. biz_type = #{bizType,jdbcType=INTEGER},
  215. version = #{version,jdbcType=INTEGER}
  216. where id = #{id,jdbcType=INTEGER}
  217. </update>
  218. </mapper>

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/202720
推荐阅读
相关标签
  

闽ICP备14008679号