mybatis-plus - 怎么知道我们那个字段是主键的?
先说结论
- 如果标注了 @TableId 则当前字段为主键
- 如果没有标注则寻找 id 字段为主键
- 两者都不满足,则打印警告
logger.warn(String.format("Warn: Could not find @TableId in Class: %s.", clazz.getName()));
- 如果没有使用 Mybatis-Plus 'xxById' 方法,则仅仅只有警告
- 但如果使用了 Mybatis-Plus 'xxById' 方法,则直接就会报错(因为根本不知道那个字段是主键,你让它怎么办😂)
源码分析
相关逻辑在 com.baomidou.mybatisplus.core.toolkit.TableInfoHelper#initTableFields
,这里把重要代码列出来成伪代码
1 | public static void initTableFields(Class<?> clazz, GlobalConfig globalConfig, TableInfo tableInfo) { |