Talk about the querydsl generation class.



This article mainly talks about the generation class of querydsl.





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:

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);
    public void addMeta(){
        addMetadata(countNum, ColumnMetadata.named("count_num"));
        addMetadata(name, ColumnMetadata.named("name"));


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.