一天上午,小明突然叫我說數據庫數據很奇怪,我看了下的確好奇怪
爲什麼自增id字段突然變得怎麼大了,後面我比對下其他的庫表,有點自增id是ok,有的也出現上述的情況,我一開始就納悶了,然後開啓數據庫的打印語句,發現入庫語句如下:
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@21f78019] will be managed by Spring
==> Preparing: INSERT INTO user ( id, name, age, email, modify_time ) VALUES ( ?, ?, ?, ?, ? )
==> Parameters: 1263010382649716737(Long), stringbxsdf(String), 0(Integer), string(String), 2020-05-20 15:35:15.432(Timestamp)
<== Updates: 1
正如上面所示,sql居然顯示指定自增id的值,我調試了測試樣本代碼,發現沒有顯示指定id的值
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3ffe858f] will be managed by Spring
==> Preparing: INSERT INTO user ( name, age, email, modify_time ) VALUES ( ?, ?, ?, ? )
==> Parameters: stringbxsdf(String), 0(Integer), string(String), 2020-05-20 15:36:56.419(Timestamp)
<== Updates: 1
這個差別讓我對比下雙方的entity的定義發現了差別如下,會顯示指定id的值的定義是
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@TableId
private Long id;
private String name;
private Integer age;
private String email;
@TableField(value = "modify_time", fill = FieldFill.INSERT_UPDATE, update = "now()", strategy = FieldStrategy.NOT_NULL)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Timestamp modifyTime;
}
沒有指定id值的定義如下:
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
@TableField(value = "modify_time", fill = FieldFill.INSERT_UPDATE, update = "now()", strategy = FieldStrategy.NOT_NULL)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Timestamp modifyTime;
}
對比發現指定了tableId,但是沒有指定type的值,之前都是每次順手把AUTO設置了,只知道採用的數據的自增類型這麼設置,那爲什麼不指定,就會出現這麼大的值。
查看源碼發現不設置,就會採用系統全局設置,這部分代碼在源碼這裏設置 com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig
/**
* 主鍵類型(默認 ID_WORKER)
*/
private IdType idType = IdType.ID_WORKER;
那麼這個ID_WORKER
究竟幹了什麼呢?爲什麼可以當做主鍵,如何保證唯一性, 繼續分析源碼,發現mp提供主鍵器IdWorker
,還可以支持三種格式
/* 以下3種類型、只有當插入對象ID 爲空,才自動填充。 */
/**
* 全局唯一ID (idWorker)
*/
ID_WORKER(3),
/**
* 全局唯一ID (UUID)
*/
UUID(4),
/**
* 字符串全局唯一ID (idWorker 的字符串表示)
*/
ID_WORKER_STR(5);
在構建preparestatement時,源碼執行如下:
protected static Object populateKeys(MetaObjectHandler metaObjectHandler, TableInfo tableInfo,
MappedStatement ms, Object parameterObject, boolean isInsert) {
if (null == tableInfo) {
/* 不處理 */
return parameterObject;
}
/* 自定義元對象填充控制器 */
MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
// 填充主鍵
if (isInsert && !StringUtils.isEmpty(tableInfo.getKeyProperty())
&& null != tableInfo.getIdType() && tableInfo.getIdType().getKey() >= 3) {
Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
/* 自定義 ID */
if (StringUtils.checkValNull(idValue)) {
if (tableInfo.getIdType() == IdType.ID_WORKER) {
metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getId());
} else if (tableInfo.getIdType() == IdType.ID_WORKER_STR) {
metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getIdStr());
} else if (tableInfo.getIdType() == IdType.UUID) {
metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.get32UUID());
}
}
}
if (metaObjectHandler != null) {
if (isInsert && metaObjectHandler.openInsertFill()) {
// 插入填充
metaObjectHandler.insertFill(metaObject);
} else if (!isInsert) {
// 更新填充
metaObjectHandler.updateFill(metaObject);
}
}
return metaObject.getOriginalObject();
}
由於是insert且id默認都是空的,所以就會觸發設置id的內容,這裏採用全局缺省配置ID_WORKER
。
他們底層使用的分佈式高效有序ID生產黑科技(sequence)
這裏爲什麼mp會出現自增主鍵id很大的情況。
處理方法
記得加上AUTOTYPE,如果確定使用數據庫的自增主鍵的話。不要使用缺省值,不然會觸發使用mp自身的主鍵生成器,從而導致自增id很大的問題。