跳至内容
LoadUp Database Processor

LoadUp Database Processor

Database 组件的编译期入口。它依赖并调用官方 MyBatis-Flex processor,统一生成 Mapper、TableDef 和 Tables。 运行时使用 loadup-components-database;编译时使用本模块,两者不存在循环依赖。

能力 契约
官方生成器 通过传递依赖调用 MyBatis-Flex,版本由 framework BOM 管理
统一规则 内置唯一配置,业务模块不能通过本地文件覆盖
主代码与测试代码 分别在当前编译输出目录准备配置
故障处理 配置准备失败时终止编译并保留原因

接入

导入 loadup-framework-bom 后,在父 POM 的 compiler 插件中配置以下内容。Flex 只需声明 LoadUp 这一项, 官方 processor 和 annotation JAR 通过传递依赖进入 processor path,无需单独声明。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.16.0</version>
    <configuration>
        <proc>full</proc>
        <annotationProcessorPaths>
            <path>
                <groupId>io.github.loadup-cloud</groupId>
                <artifactId>loadup-components-database-processor</artifactId>
            </path>
        </annotationProcessorPaths>
        <annotationProcessors>
            <annotationProcessor>io.github.loadup.components.database.processor.LoadUpMyBatisFlexProcessor</annotationProcessor>
        </annotationProcessors>
    </configuration>
</plugin>

annotationProcessors 必须明确选择 LoadUp 入口:官方依赖也注册了 SPI,自动发现会同时运行两个生成器。 项目使用 MapStruct、Spring Boot 配置元数据处理器时,将它们的 path 和入口一起加入上述列表。 Modules 和 Pay 已在根 POM 配置好三种处理器,子模块继承即可。BOM 只管理版本,不继承 compiler 插件配置。 IDEA 应导入 Maven 的 processor path 和显式入口,或委托 Maven 编译。

Framework 内部只在拥有实体的模块接入本处理器;这些模块还声明 provided、optional 的 processor 依赖, 让 Maven reactor、-am 和并行构建先编译处理器,且不会将其传递到业务运行时。 独立消费仓库需要先安装或发布包含本模块的新 framework 版本。

唯一配置

唯一维护源为 src/main/resources/META-INF/loadup/database/mybatis-flex.config,资源 filtering 关闭,保留包表达式。 每次主代码或测试代码编译时,LoadUp 在官方处理器初始化前将内置配置写入当前 CLASS_OUTPUT。 因此生成配置只存在于构建输出目录,无需在任何业务模块提交配置文件或执行 sync。

配置固定 Mapper 注解和基类、mapper/table 包、Tables 聚合类、TableDef 后缀和 DO 名称裁剪规则。 processor.stopBubbling=true 阻止模块目录或祖先目录的配置改变生成结果。资源缺失、输出不可写或官方处理器 找不到 Maven/Gradle 项目根目录时,LoadUp 明确中断编译,避免静默使用默认配置。

配置调整随 framework 制品版本发布;消费方升级版本即可生效。升级 MyBatis-Flex 时应复查其配置加载机制, 并运行本模块的生成回归测试。

验证

回归测试通过 JDK JavaCompiler 编译实体及生成代码,覆盖主/测试输出目录、本地冲突配置和输出失败。 需要本地验证时执行:

mvnd clean test -pl loadup-components/data/loadup-components-database-processor -am \
  -Dtest=LoadUpMyBatisFlexProcessorTest -Dsurefire.failIfNoSpecifiedTests=false \
  -Dspotless.skip=true -Dspotbugs.skip=true

当前改动仅完成静态检查,尚未执行上述编译和测试。


设计与实现

This build-time module owns LoadUp’s fixed MyBatis-Flex generation policy. It has no dependency on Spring or the runtime database component. The framework BOM manages its version and the official processor version together.

LoadUpMyBatisFlexProcessor extends the official processor. Before the upstream init, it writes the bundled UTF-8 policy into the current compilation’s CLASS_OUTPUT and verifies that the upstream filesystem search can locate a Maven/Gradle root. Policy preparation failures emit a compiler error and preserve the original cause. The upstream processor retains annotation handling, generation, supported types and source version behavior.

The compiler plugin explicitly selects the LoadUp entry because the upstream transitive dependency also advertises an SPI provider. Consumers must not select the upstream entry as well. MapStruct and Spring Boot metadata processing remain independent entries on the same path. The processor disables processing when compiling its own sources.

Entity modules keep no policy copies. The output policy stops ancestor lookup, making generation independent of module-local and repository-local configuration. Main and test compilation each prepare their own output policy. Framework entity modules use a provided/optional dependency to establish reactor ordering without runtime export; independent consumer repositories resolve the published processor through annotationProcessorPaths.

The JDK compiler regression tests check generated source compilation, fixed package/naming conventions, main/test output isolation and failure to prepare the policy. These tests are also the compatibility boundary for upstream processor upgrades; no upstream implementation is copied or modified.