Talk about the querydsl generation class.

  jdbc

Order

This article mainly talks about the generation class of querydsl.

maven

        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-sql</artifactId>
            <version>4.1.4</version>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>4.1.4</version>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>4.1.4</version>
        </dependency>

plugin

            <plugin>
                <groupId>com.querydsl</groupId>
                <artifactId>querydsl-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources</outputDirectory>
                            <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Generate q class

mvn compile

However, because it is generated in target, the overall feeling is not very friendly to IDE. In addition, this method of generating code is a little chicken ribs. Whenever fields are modified or added or deleted, they have to be generated again, which is very laborious. If it is to be generated, it is more reliable and at least more convenient to generate it dynamically based on bytecode.

Handwritten q class

For classes with few fields, you can write by yourself. Class Q is as follows:

@Generated("com.querydsl.sql.codegen.MetaDataSerializer")
public class QDemo extends RelationalPathBase<QDemo> {

    public final NumberPath<Long> id = createNumber("id",Long.class);
    public final NumberPath<Long> countNum = createNumber("countNum",Long.class);
    public final StringPath  name = createString("name");

    public static QDemo qDemo = new QDemo(QDemo.class.getSimpleName(),null,"demo");

    public QDemo(String path, String schema, String table){
        super(Demo.class, path, schema, table);
        addMeta();
    }
    
    public void addMeta(){
        addMetadata(countNum, ColumnMetadata.named("count_num"));
        addMetadata(name, ColumnMetadata.named("name"));
    }
}

Summary

Querydsl is powerful and powerful, but the Q class is a bit complicated. It is impossible to completely block sql. It has certain applicable scenarios, but it is not omnipotent.

doc