0

我有一个嵌套 SQL 查询来使用他们的 ID 获取员工详细信息。

现在我正在使用 BeanListHandler 来获取数据,List<Details>但希望将其存储为Map<String, Details>我最初传递的 ID 需要成为易于检索的关键,而不是每次都使用流搜索列表。

我试图转换为 Maps,但我不确定如何将 ID 映射为字符串,也不知道如何将原始 ID 作为最终结果中的列传递给内部查询。

MainTest.java:

String candidateId = "('1111', '2222', '3333', '4444')";

String detailsQuery =
    "select PARTNER, BIRTHDT, XSEXM, XSEXF from \"schema\".\"platform.view/table2\" where partner IN \r\n"
            + "(select SID from \"schema\".\"platform.view/table1\" where TYPE='BB' and CLASS='yy' and ID IN \r\n"
            + "(select SID from \"schema\".\"platform.view/table1\" where TYPE='AA' and CLASS='zz' and ID IN"
            + candidateId + "\r\n" + "))";

Map<String, Details> detailsView = queryRunner.query(conn, detailsQuery, new DetailsViewHandler());

详细信息.java:

public class Details {

    private String candidateId;
    private String birthDate;
    private String maleSex;
    private String femaleSex;

    // getter and setter
}

详细信息ViewHandler.java:

public class DetailsViewHandler extends BeanMapHandler<String, Details> {

    public DetailsViewHandler() {
        super(Details.class, new BasicRowProcessor(new BeanProcessor(getColumnsToFieldsMap())));
    }

    public static Map<String, String> getColumnsToFieldsMap() {
        Map<String, String> columnsToFieldsMap = new HashMap<>();
        columnsToFieldsMap.put("PARTNER", "candidateId");
        columnsToFieldsMap.put("BIRTHDT", "birthDate");
        columnsToFieldsMap.put("XSEXM", "maleSex");
        columnsToFieldsMap.put("XSEXF", "femaleSex");
        return columnsToFieldsMap;
    }
}

有没有办法获得ID (candidateId)结果,在创建键值对方面我缺少什么?

4

1 回答 1

0

来自您正在使用的构造函数的文档https://commons.apache.org/proper/commons-dbutils/apidocs/org/apache/commons/dbutils/handlers/BeanMapHandler.html

public BeanMapHandler(Class<V> type,
                      RowProcessor convert)

// Creates a new instance of BeanMapHandler. The value of the first column of each row will be a key in the Map.

以上应该工作。

您也可以尝试createKey像这样覆盖

protected K createKey(ResultSet rs)
               throws SQLException {
return rs.getString("PARTNER"); // or getInt whatever suits

}
于 2020-05-08T07:36:43.667 回答