Dropwizard JDBI 3 ResultSetMapper忽略字段

时间:2018-10-13 12:16:48

标签: java postgresql dropwizard pojo jdbi

我有这个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和name,而不获取其他字段。 但是,如果执行此操作,则会得到以下异常:org.skife.jdbi.v2.exceptions.ResultSetException:尝试遍历结果集时引发的异常。我试图创建一个没有其他参数的附加标签构造函数。

这是我尝试运行的查询:

@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);

2 个答案:

答案 0 :(得分:1)

您只需在查询SQL中返回多余的列即可。

if DATEPART(DD, GETDATE()) not BETWEEN 10 AND 17 
    RAISERROR ('Current day is not between 10 and 17, the job must not run', 16, 1)

答案 1 :(得分:0)

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