There are 3 types of mybatis plus entity primary key strategies (Notes> Global> Default)
Annotation
@TableId (type = IdType.AUTO) Add annotations to the entity class
@TableName("t_article")
public class TArticle extends Model<TArticle> {
private static final long serialVersionUID = 1L;
/**
* id
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 正文
*/
private String article;
}
- Global
generate a digital type ID: 895503808246718464, ID length exceeded JavaScript - By default, mybatis plus uses the globally unique number type
ID_WORKER (2, "globally unique ID") by default, and the generated ID format: ccba0a05fcbe46898304d5213d2b5518
class TableInfoHelper
private static boolean initTableId(GlobalConfiguration globalConfig, TableInfo tableInfo, Field field,
Class<?> clazz) {
TableId tableId = field.getAnnotation(TableId.class);
if (tableId != null) {
if (tableInfo.getKeyColumn() == null) {
/*
* 主键策略( 注解 > 全局 > 默认 )
*/
if (IdType.INPUT != tableId.type()) {
tableInfo.setIdType(tableId.type());
} else {
tableInfo.setIdType(globalConfig.getIdType());
}
/* 字段 */
String column = field.getName();
if (StringUtils.isNotEmpty(tableId.value())) {
column = tableId.value();
tableInfo.setKeyRelated(true);
} else {
// 开启字段下划线申明
if (globalConfig.isDbColumnUnderline()) {
column = StringUtils.camelToUnderline(column);
}
// 全局大写命名
if (globalConfig.isCapitalMode()) {
column = column.toUpperCase();
}
}
tableInfo.setKeyColumn(column);
tableInfo.setKeyProperty(field.getName());
return true;
} else {
throwExceptionId(clazz);
}
}
return false;
}