Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何以自定义方式处理多行结果?_Java_Jdbc_Java 8_Spring Jdbc - Fatal编程技术网

Java 如何以自定义方式处理多行结果?

Java 如何以自定义方式处理多行结果?,java,jdbc,java-8,spring-jdbc,Java,Jdbc,Java 8,Spring Jdbc,目前,我有一个方法体,其外观如下: jdbcTemplate.query(queryJoiningTwoTables, (rs, rowNum) -> { final long id= rs.getLong("id"); MyObject obj = jobStatusBunchMap.get(id); if (obj== null) { OffsetDateTime offsetDateTime = rs.getObject("creation_t

目前,我有一个方法体,其外观如下:

jdbcTemplate.query(queryJoiningTwoTables, (rs, rowNum) -> {
    final long id= rs.getLong("id");
    MyObject obj = jobStatusBunchMap.get(id);
    if (obj== null) {
        OffsetDateTime offsetDateTime = rs.getObject("creation_timestamp", OffsetDateTime.class);
        ...
        obj = new MyObject (offsetDateTime ...);
        map.put(id, obj );
    }
    String jobId = rs.getString("job_id");
    obj.getJobIds().add(jobId);
    return null;
});
return map.values();
看起来我使用API的方式不正确

有没有更好的方法达到同样的效果

附言


我尝试使用jdbcTemplatequeryForRowSet,但在这种情况下,rs.getObjectcreation\u timestamp,OffsetDateTime.class抛出异常,表示不支持该操作。

有许多选项可以使用jdbcTemplate映射结果,包括您的选项。 也许这会帮助您更好地理解它:

public List<Action> findAllActions() {
    final String selectStatement = "SELECT id,name FROM Actions"; // or your query
    try {

        return jdbcTemplate.query(selectStatement,(resultSet, rowNum) -> {

            int actionId = resultSet.getInt("id");
            String actionName = resultSet.getString("name");

            Action action = new Action();
            action.setId(actionId);
            action.setName(actionName);

            return action;
        });

    } catch (EmptyResultDataAccessException e) {
        LOGGER.error("Get all actions - empty set", e);
        return Collections.emptyList();
    }
}

1.它不是查询时间-业务实体创建时间。2我在实体之间有1-M关系,我想使用单个sql查询创建内存中的对象。看起来你误解了我的问题。例如,我的select返回100行,但我希望得到的对象少于100,比如说20个,因为它是带有joinQuery的查询,所以最好在SQL查询中使用mysql子句。每次有机会时都使用MySQL子句,而不是java代码。让MySQL做你根本不懂的工作吧。假设我在一个表组中有5行,每个组在STUDENT表中有20个对应的行。如果执行连接两个表的查询,我们将得到100行。但是我想得到5个组的结果,每个组都有填充的学生集合请查看我答案中的最后一个代码片段我已经修改了它,告诉我这是否是你需要知道的,在这种情况下,请不要忘记覆盖组和学生类中的equals和hashCode方法是,这就是我一直在寻找的
public List<Action> findAllActions() {
    final String selectStatement = "SELECT id,name FROM Actions"; // or your query
    try {

        return jdbcTemplate.query(selectStatement, getActionRowMapper());

    } catch (EmptyResultDataAccessException e) {
        LOGGER.error("Get all actions - empty set", e);
        return Collections.emptyList();
    }
}
private RowMapper<Action> getActionRowMapper() {
    return (resultSet, rowNum) -> {

        int actionId = resultSet.getInt("id");
        String actionName = resultSet.getString("name");

        return action;
    };
}
public List<Group> findGroups() {
    final String selectStatement = "SELECT stud.id, stud.name, gr.id, gr.name FROM student stud INNER JOIN group gr ON gr.id=stud.group_id ORDER BY stud.id"; // or your query
    try {

        return jdbcTemplate.query(selectStatement, new GroupExtractor() );

    } catch (EmptyResultDataAccessException e) {
        LOGGER.error("Get all groups - empty set", e);
        return Collections.emptyList();
    }
}


public class GroupExtractor implements ResultSetExtractor<List<Group>> {
@Override
public List<Group> extractData(ResultSet resultSet) {
    Map<Group, List<Student>> studentsGroup= new HashMap<>();
    List<Group> groups = new ArrayList<>();
    try {
        while (resultSet.next()) {
          int studentId = resultSet.getInt("stud.id");
            String studentName = resultSet.getString("stud.name");
            int groupId = resultSet.getInt("gr.id");
            String groupName = resultSet.getString("gr.name");

            Group group = createGroup(groupId, groupName);
            Student student = createStudent(studentId, studentName);

            studentsGroup.putIfAbsent(group, new ArrayList<>());
            studentsGroup.get(group).add(student);

        }

        studentsGroup.forEach((group,students) ->{
            group.setStudents(students);
            groups.add(group);
        }

        return groups;

    } catch (SQLException e) {
        LOGGER.info("An error occured during extracting data", e);
    }
    return actions;
 }

private Student createStudent(String studentId, String studentName)
{
  Student student=new Student();
  student.setId(studentId);
  student.setName(studentName);
  return student;
}

//idem for createGroup
}