赞
踩
一, 引入依赖:
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.19</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.2.2</version>
- </dependency>
二, 新建表
- 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
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE generatorConfiguration
- PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
-
- <generatorConfiguration>
- <!--mysql 连接数据库jar 这里选择自己本地位置;
- 如果不知道maven本地仓库地址,可以使用EveryThing工具全局搜索mysql-connector-java,找到jar包位置;
- 也可以手动下载一个jar放在指定位置,进行引用。
- -->
- <classPathEntry location="/Users/lizhihao/.m2/repository/mysql/mysql-connector-java/8.0.19/mysql-connector-java-8.0.19.jar"/>
-
- <context id="testTables" targetRuntime="MyBatis3">
- <commentGenerator>
- <!-- 是否去除自动生成的注释,true:是,false:否 -->
- <property name="suppressAllComments" value="true"/>
- </commentGenerator>
-
- <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
- <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
- connectionURL="jdbc:mysql://localhost:3306/generate_id" userId="root"
- password="root">
- </jdbcConnection>
-
- <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
- NUMERIC 类型解析为java.math.BigDecimal -->
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false"/>
- </javaTypeResolver>
-
- <!-- 指定javaBean生成的位置
- targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制;
- targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,MBG不会自动建目录
- -->
- <javaModelGenerator targetPackage="org.com.spi.model" targetProject="src/main/java">
- <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false;如果多个数据库改为true分目录 -->
- <property name="enableSubPackages" value="false"/>
- <!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
- <property name="trimStrings" value="true"/>
- </javaModelGenerator>
-
- <!-- 指定mapper映射文件生成的位置
- targetPackage、targetProject同javaModelGenerator中作用一样-->
- <sqlMapGenerator targetPackage="mybatis" targetProject="src/main/resources">
- <property name="enableSubPackages" value="false"/>
- </sqlMapGenerator>
-
- <!-- 指定mapper接口生成的位置
- targetPackage、targetProject同javaModelGenerator中作用一样
- -->
- <javaClientGenerator type="XMLMAPPER" targetPackage="org.com.spi.dao" targetProject="src/main/java">
- <property name="enableSubPackages" value="false"/>
- </javaClientGenerator>
- <!-- 指定数据库表
- domainObjectName:生成的domain类的名字,当表名和domain类的名字有差异时一定要设置,如果不设置,直接使用表名作为domain类的名字;
- 可以设置为somepck.domainName,那么会自动把domainName类再放到somepck包里面;
- -->
- <table tableName="worker_node"></table>
- <!-- <table tableName="auto_generate2" domainObjectName="AutoGenerate2"></table>-->
- </context>
- </generatorConfiguration>
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<Criteria> oredCriteria;
-
- public IdGeneratorExample() {
- oredCriteria = new ArrayList<Criteria>();
- }
-
- 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<Criteria> 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<Criterion> criteria;
-
- protected GeneratedCriteria() {
- super();
- criteria = new ArrayList<Criterion>();
- }
-
- public boolean isValid() {
- return criteria.size() > 0;
- }
-
- public List<Criterion> getAllCriteria() {
- return criteria;
- }
-
- public List<Criterion> 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<Integer> values) {
- addCriterion("id in", values, "id");
- return (Criteria) this;
- }
-
- public Criteria andIdNotIn(List<Integer> 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<Long> values) {
- addCriterion("max_id in", values, "maxId");
- return (Criteria) this;
- }
-
- public Criteria andMaxIdNotIn(List<Long> 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<Integer> values) {
- addCriterion("step in", values, "step");
- return (Criteria) this;
- }
-
- public Criteria andStepNotIn(List<Integer> 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<Integer> values) {
- addCriterion("biz_type in", values, "bizType");
- return (Criteria) this;
- }
-
- public Criteria andBizTypeNotIn(List<Integer> 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<Integer> values) {
- addCriterion("version in", values, "version");
- return (Criteria) this;
- }
-
- public Criteria andVersionNotIn(List<Integer> 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<IdGenerator> 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
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="org.com.spi.dao.IdGeneratorMapper" >
- <resultMap id="BaseResultMap" type="org.com.spi.model.IdGenerator" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="max_id" property="maxId" jdbcType="BIGINT" />
- <result column="step" property="step" jdbcType="INTEGER" />
- <result column="biz_type" property="bizType" jdbcType="INTEGER" />
- <result column="version" property="version" jdbcType="INTEGER" />
- </resultMap>
- <sql id="Example_Where_Clause" >
- <where >
- <foreach collection="oredCriteria" item="criteria" separator="or" >
- <if test="criteria.valid" >
- <trim prefix="(" suffix=")" prefixOverrides="and" >
- <foreach collection="criteria.criteria" item="criterion" >
- <choose >
- <when test="criterion.noValue" >
- and ${criterion.condition}
- </when>
- <when test="criterion.singleValue" >
- and ${criterion.condition} #{criterion.value}
- </when>
- <when test="criterion.betweenValue" >
- and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
- </when>
- <when test="criterion.listValue" >
- and ${criterion.condition}
- <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
- #{listItem}
- </foreach>
- </when>
- </choose>
- </foreach>
- </trim>
- </if>
- </foreach>
- </where>
- </sql>
- <sql id="Update_By_Example_Where_Clause" >
- <where >
- <foreach collection="example.oredCriteria" item="criteria" separator="or" >
- <if test="criteria.valid" >
- <trim prefix="(" suffix=")" prefixOverrides="and" >
- <foreach collection="criteria.criteria" item="criterion" >
- <choose >
- <when test="criterion.noValue" >
- and ${criterion.condition}
- </when>
- <when test="criterion.singleValue" >
- and ${criterion.condition} #{criterion.value}
- </when>
- <when test="criterion.betweenValue" >
- and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
- </when>
- <when test="criterion.listValue" >
- and ${criterion.condition}
- <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
- #{listItem}
- </foreach>
- </when>
- </choose>
- </foreach>
- </trim>
- </if>
- </foreach>
- </where>
- </sql>
- <sql id="Base_Column_List" >
- id, max_id, step, biz_type, version
- </sql>
- <select id="selectMaxId" parameterType="int" resultMap="BaseResultMap">
- select * from id_generator where biz_type = #{bizType} limit 1;
- </select>
- <select id="selectByExample" resultMap="BaseResultMap" parameterType="org.com.spi.model.IdGeneratorExample" >
- select
- <if test="distinct" >
- distinct
- </if>
- <include refid="Base_Column_List" />
- from id_generator
- <if test="_parameter != null" >
- <include refid="Example_Where_Clause" />
- </if>
- <if test="orderByClause != null" >
- order by ${orderByClause}
- </if>
- </select>
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from id_generator
- where id = #{id,jdbcType=INTEGER}
- </select>
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from id_generator
- where id = #{id,jdbcType=INTEGER}
- </delete>
- <delete id="deleteByExample" parameterType="org.com.spi.model.IdGeneratorExample" >
- delete from id_generator
- <if test="_parameter != null" >
- <include refid="Example_Where_Clause" />
- </if>
- </delete>
- <insert id="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})
- </insert>
- <insert id="insertSelective" parameterType="org.com.spi.model.IdGenerator" >
- insert into id_generator
- <trim prefix="(" 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>
- </trim>
- <trim prefix="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>
- </trim>
- </insert>
- <select id="countByExample" parameterType="org.com.spi.model.IdGeneratorExample" resultType="java.lang.Integer" >
- select count(*) from id_generator
- <if test="_parameter != null" >
- <include refid="Example_Where_Clause" />
- </if>
- </select>
- <update id="updateByExampleSelective" parameterType="map" >
- update id_generator
- <set >
- <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>
- </set>
- <if test="_parameter != null" >
- <include refid="Update_By_Example_Where_Clause" />
- </if>
- </update>
- <update id="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" >
- <include refid="Update_By_Example_Where_Clause" />
- </if>
- </update>
- <update id="updateMaxId" parameterType="org.com.spi.model.IdGenerator" >
- update id_generator set max_id = #{maxId},version = version + 1
- where biz_type = #{bizType} and version = #{version}
- </update>
- <update id="updateByPrimaryKeySelective" parameterType="org.com.spi.model.IdGenerator" >
- update id_generator
- <set >
- <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>
- </set>
- where id = #{id,jdbcType=INTEGER}
- </update>
- <update id="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}
- </update>
- </mapper>
特点: 相较于数据库自增,扩展性更好,但缺点是每次取ID都需要加锁,性能较低.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。