reference:
https://mp.baomidou.com/guide/id-generator.html
General method for automatically generating a database field modification
1, the database entity class
For the primary key is
@TableId (value = "The REFID", type = IdType.ID_WORKER_STR)
Private String the refid;
package com.uih.servicecenter.common.entity; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import lombok.Data; import java.io.Serializable; import java.sql.Timestamp; /** * @ClassName: BaseEntity * @Description: 基础实体类 * @Author: [email protected] * @date: 2019/8/15 * * */ @Data public class BaseEntity implements Serializable { private static final long serialVersionUID = 7732699947578613249L; /** * 主键 */ @TableId(value = "REFID",type = IdType.ID_WORKER_STR) private String refid; /** *createUserRefid 创建者关联id */ @TableField(value = "CREATE_USER_REFID",fill = FieldFill.INSERT) private String createUserRefid; /** *createDateTime 创建时间 */ @TableField(value = "CREATE_DATETIME",fill = FieldFill.INSERT) private Timestamp createDatetime; /** *modifyUserRefid 修改者关联id */ @TableField(value = "MODIFY_USER_REFID",fill = FieldFill.UPDATE) private String modifyUserRefid; /** *modifyDateTime 修改时间 */ @TableField(value = "MODIFY_DATETIME", fill = FieldFill.INSERT_UPDATE) private Timestamp modifyDatetime; /** *version 版本号 */ @TableField(value = "VERSION") private Integer version; /** * 是否有效 */ @TableField(value = "ACTIVE",fill = FieldFill.INSERT) private Integer active; /** * deleted 删除状态 */ @TableField(value = "DELETED",fill = FieldFill.INSERT) private Integer deleted; }
2, database fields
CREATE TABLE `device_info` ( `REFID` varchar(32) NOT NULL, `ACTIVE` tinyint(1) DEFAULT NULL, `DELETED` tinyint(1) DEFAULT NULL, `CREATE_USER_REFID` varchar(32) DEFAULT NULL, `CREATE_DATETIME` datetime DEFAULT NULL, `MODIFY_USER_REFID` varchar(32) DEFAULT NULL, `MODIFY_DATETIME` datetime DEFAULT NULL, `VERSION` smallint(6) DEFAULT NULL, PRIMARY KEY (`REFID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3, Configuration
package com.uih.servicecenter.common.config; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import java.sql.Timestamp; /** * @ClassName: MyMetaObjectHandler * @Description: 设置数据库字段的字段填充 * @Author: [email protected] * @date: 2019/9/16 * * */ @Component public class MyMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { this.setFieldValByName("createDatetime",new Timestamp(System.currentTimeMillis()),metaObject); this.setFieldValByName("modifyDatetime",new Timestamp(System.currentTimeMillis()),metaObject); this.setFieldValByName("active",1,metaObject); this.setFieldValByName("deleted",0,metaObject); } @Override public void updateFill(MetaObject metaObject) { this.setFieldValByName("modifyDatetime",new Timestamp(System.currentTimeMillis()),metaObject); } }
Primary key generation algorithm snow id
https://blog.csdn.net/weixin_38657051/article/details/94713695
Overview of
distributed systems, some need to use the globally unique ID of the scene, this time in order to prevent ID conflicts can use 36-bit UUID, UUID but there are some drawbacks, first of all he is relatively long, the other UUID is generally disorderly.
There are times when we want to use a simple number of ID, ID and want to be able to generate in chronological order.
The twitter of snowflake addresses this need, the original Twitter storage systems migrate from MySQL to Cassandra, because Cassandra is no order ID generation mechanism, so the development of such a globally unique ID generation services.
Structure
Structure snowflake follows (per part by - separately):
0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000
The first bit is not used,
the next 41 millisecond time (length 41 may be used 69 years),
then 5 and 5 datacenterId workerId (10 bits up to the length of the deployment of support nodes 1024),
and finally 12 is counted in milliseconds (each node counts 12 bits to generate the sequence number of supports per millisecond ID number 4096)
Add up to a total of just 64, is a Long type. (After conversion to a maximum length of the string 19)
Snowflake ID whole generated in a time increment sort, and do not generate collisions ID (as distinguished by the datacenter and workerId) throughout the distributed system, and high efficiency. Tested snowflake able to produce 260,000 ID per second.