Skip to content

注解处理器

huiche-apt是huiche提供的注解处理器, 可以自动处理被@Table标注的实体类,生成对应的表和列信息,是一个可选的,辅助工具性质的

如果不使用huiche-apt,你也可以选择lambda方法引用,lombok.@FieldNameConstants等方式当作列,了解查询字段

使用步骤

在pom.xml中添加依赖

因为其是可选功能,使用huiche-spring-boot-starter也不会默认引入,必须手动添加依赖

xml
<dependency>
    <groupId>org.huiche</groupId>
    <artifactId>huiche-apt</artifactId>
    <!--仅在开发时需要-->
    <optional>true</optional>
</dependency>

在pom.xml中配置注解处理器

xml
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
                <execution>
                    <configuration>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.huiche</groupId>
                                <artifactId>huiche-apt</artifactId>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!--如果使用spring-boot可以配置打包时排除huiche-apt,运行时不需要-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.huiche</groupId>
                        <artifactId>huiche-apt</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

给实体类添加@Table注解,不能省略,没有@Table注解的实体类不会被处理

了解@Table注解

java
@Table
public record Artist(Long id,String name){}

执行mvn compile

自动会在./target/generated-sources/annotations/ 下生成映射文件

java
package com.demo.table;

import com.demo.entity.Artist;
import org.huiche.ast.metadata.Column;
import org.huiche.ast.metadata.Table;
import javax.annotation.processing.Generated;

@Generated("org.huiche.apt.TableGenerator")
public class QArtist extends Table.Default {
    public static final QArtist ARTIST = new QArtist(Table.of(Artist.class).name());

    public final Column id;
    public final Column name;

    public QArtist(String name) {
        super(name);
        this.id = Column.of(name, "id");
        this.name = Column.of(name, "name");
    }
}

调整自动生成的配置

可以在resources下创建huiche-apt.config文件,内容格式为Properties,会直接使用Properties进行读取 配置属性:

属性名称说明
package包名默认table,是相对报名,相对于实体类的所在包的父级路径
比如实体类是com.demo.entity,package定义为table,实际生成包名=com.demo.table
prefix映射文件名前缀默认Q,生成时,添加到映射文件类名的开头,比如实体类为Artist
生成的映射文件为QArtist ,可以与suffix同时生效
suffix映射文件名后缀默认"",生成时,添加到映射文件类名的结尾,比如实体类为Artist
suffix为Table时,生成的映射文件为ArtistTable,可以与prefix同时生效

说明

  • 生成时也使用了NamingStrategy,如果你使用了自定义的命名策略,请确保huiche-apt可以获取到NamingStrategy自定义实现类,
  • 建议将实现类所在模块,显式声明在pom的maven-compiler-plugin插件的依赖中
xml
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
                <execution>
                    <configuration>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.huiche</groupId>
                                <artifactId>huiche-apt</artifactId>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.demo</groupId>
                    <artifactId>customized-naming-strategy-module</artifactId>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>