Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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 除了forloop内部之外,还有什么有效的方法来调用对象列表的查询吗_Java_Jdbctemplate - Fatal编程技术网

Java 除了forloop内部之外,还有什么有效的方法来调用对象列表的查询吗

Java 除了forloop内部之外,还有什么有效的方法来调用对象列表的查询吗,java,jdbctemplate,Java,Jdbctemplate,我们有一个用例,如果我们有不同学生借的相同书籍列表,我们希望将学生id分组,这样我们可以将StudentID作为列表传递,并将书籍id作为查询1传递。而不是将查询作为查询2运行两次。我不想调用服务方法来执行循环中的查询 问题1: [Student1,Student2]和[Book1,Book2] 问题2: 像学生一样,第1册,第2册 学生2——第一册,第二册 模型 公立学校{ 私人字符串学生ID; 私人列表图书ID; } 公共班级学生响应{ 私人字符串学生ID; 私有字符串totalBookCo

我们有一个用例,如果我们有不同学生借的相同书籍列表,我们希望将学生id分组,这样我们可以将StudentID作为列表传递,并将书籍id作为查询1传递。而不是将查询作为查询2运行两次。我不想调用服务方法来执行循环中的查询

问题1: [Student1,Student2]和[Book1,Book2]
问题2: 像学生一样,第1册,第2册 学生2——第一册,第二册

模型

公立学校{
私人字符串学生ID;
私人列表图书ID;
}
公共班级学生响应{
私人字符串学生ID;
私有字符串totalBookCount;
私有字符串平均BookCount
}
控制器

public class SchoolControler {

  @Value("${features.enable.query.groupByStudentBook:false}")
  private boolean IsgroupStudentByBook;

  @RequestMapping(value = "/students", method = POST, consumes = "application/json", produces = "application/json")
  public Results < StudentResponse > getAggregatedStudentByBook(
    @ApiParam("Get Total and Average of Book count") @RequestBody List < AggregateStudentBook > aggregateStudentBookRequest) {


    if (IsgroupStudentByBook) {
      return groupingStudentId(aggregateStudentBookRequest);
    } else {
      Collection < StudentResponse > AggregateStudentBookResponse = new ArrayList < > ();

      for (AggregateStudentBook aggregateStudentBookRequest: aggregateStudentBookRequest) {

        AggregateStudentBookResponse.addAll(StudentService.getAggregateStudentBook(aggregateStudentBookRequest.getBookIds, new ArrayList < > (Arrays.asList(aggregateStudentBookRequest.getStudentId())), false));

      }
      return new Results < > (AggregateStudentBookResponse);

    }



    private Results < StudentResponse > groupingStudentId(List < AggregateStudentBookRequest > aggregateStudentBookRequest) {
      Collection < StudentResponse > aggregateStudentResponse = new ArrayList < > ();
      List < String > studentIds = new ArrayList < > ();;
      for (int i = 0; i <= aggregateStudentBookRequest.size() - 1; i++) {
        if (studentIds.contains(aggregateStudentBookRequest.get(i).getstudentId())) {
          continue;
        }
        List < String > commonstudentIds = new ArrayList < > ();
        for (int j = 1; j < aggregatedStudentRequest.size(); j++) {
          if (studentIds.contains(aggregatedStudentRequest.get(j).getstudentId())) {
            continue;
          }
          if (i == j) {
            aggregatedstudentResponse.addAll(StudentService.getAggregatedstudentBook(aggregatedStudentRequest.get(i).getBookIds(), new ArrayList < > (Arrays.asList(aggregatedStudentRequest.get(i).getstudentId())), false));
            break;

          }
          Collections.sort(aggregatedStudentRequest.get(i).getBookIds());
          Collections.sort(aggregatedStudentRequest.get(j).getBookIds());
          if (aggregatedStudentRequest.get(i).getBookIds().containsAll(aggregatedStudentRequest.get(j).getBookIds())) {
            commonstudentIds.add(aggregatedStudentRequest.get(j).getstudentId());
            commonstudentIds.add(aggregatedStudentRequest.get(i).getstudentId());
            studentIds.add(aggregatedStudentRequest.get(j).getstudentId());
            studentIds.add(aggregatedStudentRequest.get(i).getstudentId());

          }


        }
        if (!commonstudentIds.isEmpty()) {
          aggregatedstudentResponse.addAll(StudentService.getAggregatedstudentBook(aggregatedStudentRequest.get(i).getBookIds(), commonstudentIds, false));
        }

        if (studentIds.size() == aggregatedStudentRequest.size()) {
          Results < AggregatedstudentResponse > result = new Results < > (aggregatedstudentResponse);
          return result;
        }

      }



      return new Results < > (aggregatedstudentResponse);
    }


  }
}
public class school controller{
@值(${features.enable.query.groupByStudentBook:false}”)
私有布尔值是groupstudentbybook;
@RequestMapping(value=“/students”,method=POST,consumes=“application/json”,products=“application/json”)
公共结果getAggregatedStudentByBook(
@ApiParam(“获取书籍计数的总数和平均值”)@RequestBody ListaggregateStudentBookRequest){
如果(IsgroupStudentByBook){
返回groupingStudentId(aggregateStudentBookRequest);
}否则{
CollectionAggregateStudentBookResponse=new ArrayList<>();
for(AggregateStudentBook aggregateStudentBookRequest:aggregateStudentBookRequest){
AggregateStudentBookResponse.addAll(StudentService.getAggregateStudentBook(aggregateStudentBookRequest.GetBookId,new ArrayList<>(Arrays.asList(aggregateStudentBookRequest.getStudentId()),false));
}
返回新结果<>(AggregateStudentBookResponse);
}
私有结果groupingStudentId(列表AggregateStudentBookRequest){
集合aggregateStudentResponse=newarraylist<>();
ListstudentIds=newarraylist<>();;
for(int i=0;i commonstudentIds=new ArrayList<>();
对于(int j=1;j(Arrays.asList(aggregatedStudentRequest.get(i).getstudentId()),false);
打破
}
Collections.sort(aggregatedStudentRequest.get(i).getBookIds());
Collections.sort(aggregatedStudentRequest.get(j.getBookIds());
if(aggregatedStudentRequest.get(i).getBookIds().containsAll(aggregatedStudentRequest.get(j).getBookIds()){
add(aggregatedStudentRequest.get(j.getstudentId());
add(aggregatedStudentRequest.get(i).getstudentId());
add(aggregatedStudentRequest.get(j.getstudentId());
add(aggregatedStudentRequest.get(i).getstudentId());
}
}
如果(!commonstudentIds.isEmpty()){
aggregatedstudentResponse.addAll(StudentService.getAggregatedstudentBook(aggregatedStudentRequest.get(i).GetBookId(),CommonStudentId,false));
}
if(studentId.size()==aggregatedStudentRequest.size()){
结果结果=新结果<>(AggregatedstudentResponse);
返回结果;
}
}
返回新结果<>(聚合StudentResponse);
}
}
}
服务

public class StudentService {

  @Autowire
  StudentRepository studentRepository;

  public Collection < AggregateStudentBook > getAggregateStudentBook(List < String > bookIds, List < String > studentIds) {
    Collection < AggregateStudentBook > aggregateStudentBook = studentRepository.fetchAggregatedStoreTargetInventory(bookIds, studentIds);
    return aggregateStudentBook;

  }
}
公共班级学生服务{
@自动连线
StudentRepository StudentRepository;
公共集合getAggregateStudentBook(列表BookId,列表StudentId){
CollectionAggregateStudentBook=studentRepository.fetchAggregatedStoreTargetInventory(BookId,StudentId);
返回聚合学生簿;
}
}

提前感谢

一个更好的解决方案是使用Yes,我在添加equals和hascode方法后使用了团购,但问题是需要使用下面的代码将两个重复对象收集到列表中,从两个重复对象中仅获取一个对象,这对于理想情况来说是正确的,但这里的学生ID不同,因此需要两个重复对象ate objects.Code belowaggregateStudentBookRequest.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()).entrySet().stream().filter(e->e.getValue()>1L).map(e->e.getKey()).Collectors(Collectors.toList()).forEach(aggregateStudentBookRequest->.System.out.println(aggregateStudentBookRequest.getStudentId()+“=>”+aggregateStudentBookRequest.getBookId());
public class StudentService {

  @Autowire
  StudentRepository studentRepository;

  public Collection < AggregateStudentBook > getAggregateStudentBook(List < String > bookIds, List < String > studentIds) {
    Collection < AggregateStudentBook > aggregateStudentBook = studentRepository.fetchAggregatedStoreTargetInventory(bookIds, studentIds);
    return aggregateStudentBook;

  }
}