There are 3 kinds of mybatis plus entity key strategy

There are 3 types of mybatis plus entity primary key strategies (Notes> Global> Default)

  1. 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;
    }
  1. Global
    generate a digital type ID: 895503808246718464, ID length exceeded JavaScript
  2. 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;
    }
Published 100 original articles · Like 106 · Visit 270,000+

Guess you like

Origin blog.csdn.net/lglglgl/article/details/82621689