编译期实体元数据
郭胜凯2026/08/17
Smart MyBatis 3.0.2 提供可选的 @SmartMeta 注解和注解处理器,为 PO 生成编译期元数据类。没有使用该能力时,框架仍会通过反射构建 MapperDeclaration,普通 CRUD 不受影响。
标记实体
@SmartMeta
public class Student extends PO {
@ID
private Integer id;
private String name;
private Integer age;
}
编译后会在实体包的 M 子包生成 $Student:
ink.icoding.smartmybatis.example.entity.Student
ink.icoding.smartmybatis.example.entity.M.$Student
生成类型包含实体的 MapperDeclaration INSTANCE,以及每个非静态字段对应的 ColumnDeclaration。运行时 MapperUtil 会优先解析生成元数据,找不到时自动回退到反射。
Maven 注解处理器
如果业务工程没有自动启用处理器,可以显式配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>ink.icoding.smart-mybatis</groupId>
<artifactId>core</artifactId>
<version>3.0.2</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
约束
@SmartMeta只能标记PO的具体子类。- 注解保留策略是
SOURCE,运行时不会读取它。 - 抽象类不会生成元数据。
- 生成源码通常位于 Maven 的
target/generated-sources/annotations。 - 不要手工修改
$Entity文件;它会在下次编译时重新生成。
这项能力主要用于减少运行时声明构建成本和提供稳定的元数据入口,不改变实体映射、方言或 Mapper API。
