一, 引入依赖:
-
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
- "1.0" encoding="UTF-8"?>
- PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
-
-
-
"/Users/lizhihao/.m2/repository/mysql/mysql-connector-java/8.0.19/mysql-connector-java-8.0.19.jar"/> -
-
"testTables" targetRuntime="MyBatis3"> -
-
-
"suppressAllComments" value="true"/> -
-
-
-
"com.mysql.cj.jdbc.Driver" - connectionURL="jdbc:mysql://localhost:3306/generate_id" userId="root"
- password="root">
-
-
-
-
-
"forceBigDecimals" value="false"/> -
-
-
-
"org.com.spi.model" targetProject="src/main/java"> -
-
"enableSubPackages" value="false"/> -
-
"trimStrings" value="true"/> -
-
-
-
"mybatis" targetProject="src/main/resources"> -
"enableSubPackages" value="false"/> -
-
-
-
"XMLMAPPER" targetPackage="org.com.spi.dao" targetProject="src/main/java"> -
"enableSubPackages" value="false"/> -
-
-
"worker_node">
-
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() { - 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, "id");
- return (Criteria) this;
- }
-
- public Criteria andIdLessThanOrEqualTo(Integer value) {
- addCriterion("id <=", value, "id");
- return (Criteria) this;
- }
-
- public Criteria andIdIn(List
values) { - addCriterion("id in", values, "id");
- return (Criteria) this;
- }
-
- public Criteria andIdNotIn(List
values) { - addCriterion("id not in", values, "id");
- return (Criteria) this;
- }
-
- public Criteria andIdBetween(Integer value1, Integer value2) {
- addCriterion("id between", value1, value2, "id");
- return (Criteria) this;
- }
-
- public Criteria andIdNotBetween(Integer value1, Integer value2) {
- addCriterion("id not between", value1, value2, "id");
- return (Criteria) this;
- }
-
- public Criteria andMaxIdIsNull() {
- addCriterion("max_id is null");
- return (Criteria) this;
- }
-
- public Criteria andMaxIdIsNotNull() {
- addCriterion("max_id is not null");
- return (Criteria) this;
- }
-
- public Criteria andMaxIdEqualTo(Long value) {
- addCriterion("max_id =", value, "maxId");
- return (Criteria) this;
- }
-
- public Criteria andMaxIdNotEqualTo(Long value) {
- addCriterion("max_id <>", value, "maxId");
- return (Criteria) this;
- }
-
- public Criteria andMaxIdGreaterThan(Long value) {
- addCriterion("max_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, "maxId");
- return (Criteria) this;
- }
-
- public Criteria andMaxIdLessThanOrEqualTo(Long value) {
- addCriterion("max_id <=", value, "maxId");
- return (Criteria) this;
- }
-
- public Criteria andMaxIdIn(List
values) { - addCriterion("max_id in", values, "maxId");
- return (Criteria) this;
- }
-
- public Criteria andMaxIdNotIn(List
values) { - addCriterion("max_id not in", values, "maxId");
- return (Criteria) this;
- }
-
- public Criteria andMaxIdBetween(Long value1, Long value2) {
- addCriterion("max_id between", value1, value2, "maxId");
- return (Criteria) this;
- }
-
- public Criteria andMaxIdNotBetween(Long value1, Long value2) {
- addCriterion("max_id not between", value1, value2, "maxId");
- return (Criteria) this;
- }
-
- public Criteria andStepIsNull() {
- addCriterion("step is null");
- return (Criteria) this;
- }
-
- public Criteria andStepIsNotNull() {
- addCriterion("step is not null");
- return (Criteria) this;
- }
-
- public Criteria andStepEqualTo(Integer value) {
- addCriterion("step =", value, "step");
- return (Criteria) this;
- }
-
- public Criteria andStepNotEqualTo(Integer value) {
- addCriterion("step <>", value, "step");
- return (Criteria) this;
- }
-
- public Criteria andStepGreaterThan(Integer value) {
- addCriterion("step >", 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, "step");
- return (Criteria) this;
- }
-
- public Criteria andStepLessThanOrEqualTo(Integer value) {
- addCriterion("step <=", value, "step");
- return (Criteria) this;
- }
-
- public Criteria andStepIn(List
values) { - addCriterion("step in", values, "step");
- return (Criteria) this;
- }
-
- public Criteria andStepNotIn(List
values) { - addCriterion("step not in", values, "step");
- return (Criteria) this;
- }
-
- public Criteria andStepBetween(Integer value1, Integer value2) {
- addCriterion("step between", value1, value2, "step");
- return (Criteria) this;
- }
-
- public Criteria andStepNotBetween(Integer value1, Integer value2) {
- addCriterion("step not between", value1, value2, "step");
- return (Criteria) this;
- }
-
- public Criteria andBizTypeIsNull() {
- addCriterion("biz_type is null");
- return (Criteria) this;
- }
-
- public Criteria andBizTypeIsNotNull() {
- addCriterion("biz_type is not null");
- return (Criteria) this;
- }
-
- public Criteria andBizTypeEqualTo(Integer value) {
- addCriterion("biz_type =", value, "bizType");
- return (Criteria) this;
- }
-
- public Criteria andBizTypeNotEqualTo(Integer value) {
- addCriterion("biz_type <>", value, "bizType");
- return (Criteria) this;
- }
-
- public Criteria andBizTypeGreaterThan(Integer value) {
- addCriterion("biz_type >", 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, "bizType");
- return (Criteria) this;
- }
-
- public Criteria andBizTypeLessThanOrEqualTo(Integer value) {
- addCriterion("biz_type <=", value, "bizType");
- return (Criteria) this;
- }
-
- public Criteria andBizTypeIn(List
values) { - addCriterion("biz_type in", values, "bizType");
- return (Criteria) this;
- }
-
- public Criteria andBizTypeNotIn(List
values) { - addCriterion("biz_type not in", values, "bizType");
- return (Criteria) this;
- }
-
- public Criteria andBizTypeBetween(Integer value1, Integer value2) {
- addCriterion("biz_type between", value1, value2, "bizType");
- return (Criteria) this;
- }
-
- public Criteria andBizTypeNotBetween(Integer value1, Integer value2) {
- addCriterion("biz_type not between", value1, value2, "bizType");
- return (Criteria) this;
- }
-
- public Criteria andVersionIsNull() {
- addCriterion("version is null");
- return (Criteria) this;
- }
-
- public Criteria andVersionIsNotNull() {
- addCriterion("version is not null");
- return (Criteria) this;
- }
-
- public Criteria andVersionEqualTo(Integer value) {
- addCriterion("version =", value, "version");
- return (Criteria) this;
- }
-
- public Criteria andVersionNotEqualTo(Integer value) {
- addCriterion("version <>", value, "version");
- return (Criteria) this;
- }
-
- public Criteria andVersionGreaterThan(Integer value) {
- addCriterion("version >", value, "version");
- return (Criteria) this;
- }
-
- public Criteria andVersionGreaterThanOrEqualTo(Integer value) {
- addCriterion("version >=", value, "version");
- return (Criteria) this;
- }
-
- public Criteria andVersionLessThan(Integer value) {
- addCriterion("version <", value, "version");
- return (Criteria) this;
- }
-
- public Criteria andVersionLessThanOrEqualTo(Integer value) {
- addCriterion("version <=", value, "version");
- return (Criteria) this;
- }
-
- public Criteria andVersionIn(List
values) { - addCriterion("version in", values, "version");
- return (Criteria) this;
- }
-
- public Criteria andVersionNotIn(List
values) { - addCriterion("version not in", values, "version");
- return (Criteria) this;
- }
-
- public Criteria andVersionBetween(Integer value1, Integer value2) {
- addCriterion("version between", value1, value2, "version");
- return (Criteria) this;
- }
-
- public Criteria andVersionNotBetween(Integer value1, Integer value2) {
- addCriterion("version not between", value1, value2, "version");
- return (Criteria) this;
- }
- }
-
- public static class Criteria extends GeneratedCriteria {
-
- protected Criteria() {
- super();
- }
- }
-
- public static class Criterion {
- private String condition;
-
- private Object value;
-
- private Object secondValue;
-
- private boolean noValue;
-
- private boolean singleValue;
-
- private boolean betweenValue;
-
- private boolean listValue;
-
- private String typeHandler;
-
- public String getCondition() {
- return condition;
- }
-
- public Object getValue() {
- return value;
- }
-
- public Object getSecondValue() {
- return secondValue;
- }
-
- public boolean isNoValue() {
- return noValue;
- }
-
- public boolean isSingleValue() {
- return singleValue;
- }
-
- public boolean isBetweenValue() {
- return betweenValue;
- }
-
- public boolean isListValue() {
- return listValue;
- }
-
- public String getTypeHandler() {
- return typeHandler;
- }
-
- protected Criterion(String condition) {
- super();
- this.condition = condition;
- this.typeHandler = null;
- this.noValue = true;
- }
-
- protected Criterion(String condition, Object value, String typeHandler) {
- super();
- this.condition = condition;
- this.value = value;
- this.typeHandler = typeHandler;
- if (value instanceof List>) {
- this.listValue = true;
- } else {
- this.singleValue = true;
- }
- }
-
- protected Criterion(String condition, Object value) {
- this(condition, value, null);
- }
-
- protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
- super();
- this.condition = condition;
- this.value = value;
- this.secondValue = secondValue;
- this.typeHandler = typeHandler;
- this.betweenValue = true;
- }
-
- protected Criterion(String condition, Object value, Object secondValue) {
- this(condition, value, secondValue, null);
- }
- }
- }
2, dao层接口: IdGeneratorMapper
- package org.com.spi.dao;
-
- import java.util.List;
-
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Param;
- import org.com.spi.model.IdGenerator;
- import org.com.spi.model.IdGeneratorExample;
-
- @Mapper
- public interface IdGeneratorMapper {
- int countByExample(IdGeneratorExample example);
-
- int deleteByExample(IdGeneratorExample example);
-
- int deleteByPrimaryKey(Integer id);
-
- int insert(IdGenerator record);
-
- int insertSelective(IdGenerator record);
-
- List
selectByExample(IdGeneratorExample example); -
- IdGenerator selectByPrimaryKey(Integer id);
-
- int updateByExampleSelective(@Param("record") IdGenerator record, @Param("example") IdGeneratorExample example);
-
- int updateByExample(@Param("record") IdGenerator record, @Param("example") IdGeneratorExample example);
-
- int updateByPrimaryKeySelective(IdGenerator record);
-
- int updateByPrimaryKey(IdGenerator record);
-
- IdGenerator selectMaxId(int bizType);
-
- int updateMaxId(IdGenerator idGenerator);
- }
3, controller层: GenerateIdController
- @Autowired
- IdGeneratorMapper idGeneratorMapper;
- @PutMapping("/save1")
- public int save1(@RequestBody AutoGenerate1 autoGenerate){
- IdGenerator idGenerator = idGeneratorMapper.selectMaxId(BizTypeEnum.GENERATE_ONE.getType());
- int count = 0;
- if(idGenerator == null){
- idGenerator = new IdGenerator();
- idGenerator.setId(1);
- idGenerator.setStep(4);
- idGenerator.setVersion(1);
- idGenerator.setMaxId(1L);
- idGenerator.setBizType(BizTypeEnum.GENERATE_ONE.getType());
- count = idGeneratorMapper.insertSelective(idGenerator);
- autoGenerate.setId(1L);
- }else{
- Long currentId = idGenerator.getMaxId() + idGenerator.getStep();
- autoGenerate.setId(currentId);
- idGenerator.setMaxId(currentId);
- count = idGeneratorMapper.updateMaxId(idGenerator);
- }
- if(count > 0){//id生成后才能算成功进行新增
- return autoGenerateMapper1.insert(autoGenerate);
- }
- return 0;
- }
4, resources/mybatis目录下建: IdGeneratorMapper.xml
- "1.0" encoding="UTF-8" ?>
- "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
"org.com.spi.dao.IdGeneratorMapper" > -
"BaseResultMap" type="org.com.spi.model.IdGenerator" > -
"id" property="id" jdbcType="INTEGER" /> -
"max_id" property="maxId" jdbcType="BIGINT" /> -
"step" property="step" jdbcType="INTEGER" /> -
"biz_type" property="bizType" jdbcType="INTEGER" /> -
"version" property="version" jdbcType="INTEGER" /> -
-
"Example_Where_Clause" > -
-
"oredCriteria" item="criteria" separator="or" > - <if test="criteria.valid" >
-
"(" suffix=")" prefixOverrides="and" > -
"criteria.criteria" item="criterion" > -
-
"criterion.noValue" > - and ${criterion.condition}
-
-
"criterion.singleValue" > - and ${criterion.condition} #{criterion.value}
-
-
"criterion.betweenValue" > - and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
-
-
"criterion.listValue" > - and ${criterion.condition}
-
"criterion.value" item="listItem" open="(" close=")" separator="," > - #{listItem}
-
-
-
-
-
- if>
-
-
-
-
"Update_By_Example_Where_Clause" > -
-
"example.oredCriteria" item="criteria" separator="or" > - <if test="criteria.valid" >
-
"(" suffix=")" prefixOverrides="and" > -
"criteria.criteria" item="criterion" > -
-
"criterion.noValue" > - and ${criterion.condition}
-
-
"criterion.singleValue" > - and ${criterion.condition} #{criterion.value}
-
-
"criterion.betweenValue" > - and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
-
-
"criterion.listValue" > - and ${criterion.condition}
-
"criterion.value" item="listItem" open="(" close=")" separator="," > - #{listItem}
-
-
-
-
-
- if>
-
-
-
-
"Base_Column_List" > - id, max_id, step, biz_type, version
-
-
- select * from id_generator where biz_type = #{bizType} limit 1;
-
-
- select
- <if test="distinct" >
- distinct
- if>
-
"Base_Column_List" /> - from id_generator
- <if test="_parameter != null" >
-
"Example_Where_Clause" /> - if>
- <if test="orderByClause != null" >
- order by ${orderByClause}
- if>
-
-
- select
-
"Base_Column_List" /> - from id_generator
- where id = #{id,jdbcType=INTEGER}
-
-
"deleteByPrimaryKey" parameterType="java.lang.Integer" > - delete from id_generator
- where id = #{id,jdbcType=INTEGER}
-
-
"deleteByExample" parameterType="org.com.spi.model.IdGeneratorExample" > - delete from id_generator
- <if test="_parameter != null" >
-
"Example_Where_Clause" /> - if>
-
-
"insert" parameterType="org.com.spi.model.IdGenerator" > - insert into id_generator (id, max_id, step,
- biz_type, version)
- values (#{id,jdbcType=INTEGER}, #{maxId,jdbcType=BIGINT}, #{step,jdbcType=INTEGER},
- #{bizType,jdbcType=INTEGER}, #{version,jdbcType=INTEGER})
-
-
"insertSelective" parameterType="org.com.spi.model.IdGenerator" > - insert into id_generator
-
"(" suffix=")" suffixOverrides="," > - <if test="id != null" >
- id,
- if>
- <if test="maxId != null" >
- max_id,
- if>
- <if test="step != null" >
- step,
- if>
- <if test="bizType != null" >
- biz_type,
- if>
- <if test="version != null" >
- version,
- if>
-
-
"values (" suffix=")" suffixOverrides="," > - <if test="id != null" >
- #{id,jdbcType=INTEGER},
- if>
- <if test="maxId != null" >
- #{maxId,jdbcType=BIGINT},
- if>
- <if test="step != null" >
- #{step,jdbcType=INTEGER},
- if>
- <if test="bizType != null" >
- #{bizType,jdbcType=INTEGER},
- if>
- <if test="version != null" >
- #{version,jdbcType=INTEGER},
- if>
-
-
-
- select count(*) from id_generator
- <if test="_parameter != null" >
-
"Example_Where_Clause" /> - if>
-
-
"updateByExampleSelective" parameterType="map" > - update id_generator
-
- <if test="record.id != null" >
- id = #{record.id,jdbcType=INTEGER},
- if>
- <if test="record.maxId != null" >
- max_id = #{record.maxId,jdbcType=BIGINT},
- if>
- <if test="record.step != null" >
- step = #{record.step,jdbcType=INTEGER},
- if>
- <if test="record.bizType != null" >
- biz_type = #{record.bizType,jdbcType=INTEGER},
- if>
- <if test="record.version != null" >
- version = #{record.version,jdbcType=INTEGER},
- if>
-
- <if test="_parameter != null" >
-
"Update_By_Example_Where_Clause" /> - if>
-
-
"updateByExample" parameterType="map" > - update id_generator
- set id = #{record.id,jdbcType=INTEGER},
- max_id = #{record.maxId,jdbcType=BIGINT},
- step = #{record.step,jdbcType=INTEGER},
- biz_type = #{record.bizType,jdbcType=INTEGER},
- version = #{record.version,jdbcType=INTEGER}
- <if test="_parameter != null" >
-
"Update_By_Example_Where_Clause" /> - if>
-
-
"updateMaxId" parameterType="org.com.spi.model.IdGenerator" > - update id_generator set max_id = #{maxId},version = version + 1
- where biz_type = #{bizType} and version = #{version}
-
-
"updateByPrimaryKeySelective" parameterType="org.com.spi.model.IdGenerator" > - update id_generator
-
- <if test="maxId != null" >
- max_id = #{maxId,jdbcType=BIGINT},
- if>
- <if test="step != null" >
- step = #{step,jdbcType=INTEGER},
- if>
- <if test="bizType != null" >
- biz_type = #{bizType,jdbcType=INTEGER},
- if>
- <if test="version != null" >
- version = #{version,jdbcType=INTEGER},
- if>
-
- where id = #{id,jdbcType=INTEGER}
-
-
"updateByPrimaryKey" parameterType="org.com.spi.model.IdGenerator" > - update id_generator
- set max_id = #{maxId,jdbcType=BIGINT},
- step = #{step,jdbcType=INTEGER},
- biz_type = #{bizType,jdbcType=INTEGER},
- version = #{version,jdbcType=INTEGER}
- where id = #{id,jdbcType=INTEGER}
-
特点: 相较于数据库自增,扩展性更好,但缺点是每次取ID都需要加锁,性能较低.