Dropwizard JDBI 3 ResultSetMapper 忽略字段

Dropwizard JDBI 3 ResultSetMapper ignore field

我有这个 Pojo:

private long id;

    @NotEmpty
    @JsonProperty("name")
    private String name;

    @NotEmpty
    @JsonProperty("id")
    private String tagUuid;

    @NotEmpty
    @JsonProperty("archived")
    private boolean archived;

    @NotEmpty
    @JsonProperty("creationDate")
    private DateTime creationDate;

    private Integer count;

    @JsonCreator
    public Tag() {
    }

    public Tag(long id, String tagUuid, String name, boolean archived, Timestamp creationDate, Integer count) {
        this.id = id;
        this.tagUuid = tagUuid;
        this.name = name;
        this.archived = archived;
        this.creationDate = new DateTime(creationDate);
        this.count = count;
    }

这是我的结果集映射器:

public class TagMapper implements ResultSetMapper<Tag> {

    @Override
    public Tag map(int index, ResultSet r, StatementContext ctx) throws SQLException {
        return new Tag(
                r.getLong("id"),
                r.getString("tag_uuid"),
                r.getString("name"),
                r.getBoolean("archived"),
                r.getTimestamp("creation_date"),
                r.getInt("count")
        );
    }
}

如何从数据库中少取一列。例如,在某些查询中,我只获取 tagUuid 和名称,而不获取其他字段。 但是如果我这样做,我会得到这个异常: org.skife.jdbi.v2.exceptions.ResultSetException: Exception thrown while trying to traverse the result set.我尝试创建一个没有其他参数的附加标记构造函数。

这是我尝试的查询 运行:

@SqlQuery("SELECT t.id, t.tag_uuid as tag_uuid, t.name, t.archived, t.creation_date FROM tags t WHERE t.tag_uuid = :tag_uuid LIMIT 1")
    public Tag fetchTagByUuid(@Bind("tag_uuid") String tagUuid);

您可以 return 查询中的额外列 SQL。

@SqlQuery("SELECT t.id, t.tag_uuid as tag_uuid, t.name, t.archived, " +
          "t.creation_date, 0 AS count FROM tags t " +
          "WHERE t.tag_uuid = :tag_uuid LIMIT 1")
public Tag fetchTagByUuid(@Bind("tag_uuid") String tagUuid);

您可以随心所欲地检索值,并在将值传递给 Tag 构造函数之前检查它们是否存在于 ResultSet 中。如果该属性不存在,那么您可以传递属性的默认值。 您可以将值检查为 r.getString("tag_uuid") != null (对于字符串) 然后 tag_uuid = r.getString("tag_uuid")