如何在不使用 JOOQ 代码生成的情况下获取 Pojo 中的数据库行?

How can I fetch database rows in Pojo without using JOOQ code generation?

我想在没有代码生成的情况下使用 JOOQ。我有一个 dao class 看起来像这样

public class FilesDao { 
public List<FilePojo> getAllFiles() {
    DataSource dataSource = DataSourceFactory.getTestiDataSource();
    List<FilePojo> filePojos = new ArrayList<>();
    try (Connection con = dataSource.getConnection()) {
        DSLContext create = DSL.using(con, SQLDialect.MARIADB);
        filePojos = create.select(field("tiedosto.id"), field("tiedosto.nimi"), field("tiedosto.koko_tavua"),
                field("tiedosto.sisalto"), field("tiedosto.hlo_id"))
                .from(table("tiedosto"))
                .where(field("minioupload").eq((byte) 0))
                .fetch().into(FilePojo.class);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return filePojos;
   }
}

和一个看起来像这样的 Pojo class

import javax.persistence.Column;
import javax.persistence.Table;

@Table(name="tiedosto")
public class FilePojo {

@Column(name = "id")
private Integer id;

@Column(name = "hlo_id")
private Integer customerId;

@Column(name = "koko_tavua")
private Integer fileSize;

@Column(name = "nimi")
private String fileName;

@Column(name = "sisalto")
private byte[] content;}
//Getters setters omitted

当我尝试使用像这样的主要方法从 table 中读取时

public class App {
public static void main(String[] args) {
    FilesDao mydao = new FilesDao();
    List<FilePojo> myList = mydao.getAllFiles();
    for (FilePojo filePojo : myList) {
        System.out.println("==========================================" + "\n" +
                filePojo.getId() + " " +
                filePojo.getCustomerId() + " " +
                filePojo.getFileName() + " " +
                filePojo.getFileSize() + " " +
                filePojo.getContent() + " " +
                "==========================================");
    }
  }
}

输出结果如下

我可以看到 SQL 查询 运行 正常并列出了所有匹配的行,但 pojo 返回空值。我在这里做错了什么?有人可以指出我正确的方向吗?如果能提供任何帮助,我将不胜感激。

我不确定这是否是 bug or a feature. You're using the plain SQL templating API when you should probably be using the identifier building API。当你写

field("tiedosto.id")

然后,jOOQ(可能是错误的)认为您的专栏名为 `tiedosto.id`,名称中有一个句点。什么时候才真正应该限定为`tiedosto`.`id`。有一些可能的修复:

继续使用简单的 SQL 模板 API

但是,不要限定名称:

field("id")

使用标识符构建API

field(name("tiedosto", "id"))

使用代码生成器

当然,这应该始终是您的首选。