解决遍历List时都只能取到list的最后一条数据

后台controller中的生成List的部分

List majorsName = classService.findAllMajorsByYear(clGrade);
		MajorAnalysisData majorAnalysisData = new MajorAnalysisData();    //注意这个地方就是错误的原因,把对象创建放在了循环的外面,这样在循环里面给对象赋值时,每循环一次都会覆盖前一次的值
		for (int i =0;i studentlist = studentService.findAllStudentsByClMajorAndStGrade(majorsName.get(i), String.valueOf(clGrade));
			majorAnalysisData.setMaStudentNum(studentlist.size());
			//设置专业挂科人数
			List failStudentlist = studentService.findFailStudent(majorsName.get(i), String.valueOf(clGrade));
			majorAnalysisData.setMaFailStudentNum(failStudentlist.size());
			//设置补考未通过人数
			List makeupFailStudentlist = studentService.makeupFailStudent(majorsName.get(i), String.valueOf(clGrade));
			majorAnalysisData.setMakeupFailNum(makeupFailStudentlist.size());
			//设置补考通过人数
			majorAnalysisData.setMakeupSuccessNum(failStudentlist.size()-makeupFailStudentlist.size());
			//设置重修人数
			majorAnalysisData.setRebuildNum(failStudentlist.size()-makeupFailStudentlist.size());
			majorAnalysisDatalist.add(majorAnalysisData);
			System.out.println(majorAnalysisData.toString());
		}

前端通过get发送请求,取list里面的值,并赋值给表格

$.get(url,grade,function(data){
	 			var table_makeup = document.getElementById("table_makeup");
				var body_makeup = document.getElementById("body_makeup");
				
				var table_rebuild = document.getElementById("table_rebuild");
				var body_rebuild = document.getElementById("body_rebuild");
				for(var i = 0; i < data.length; i++){
					var a = data[i];
					var tr_makeup = document.createElement("tr");

					var td_clMajor = document.createElement("td");
					var td_maStudentNum = document.createElement("td");
					var td_maFailStudentNum = document.createElement("td");
					var td_makeupSuccessNum = document.createElement("td");
					
					td_clMajor.innerHTML =a.clMajor;
					td_maStudentNum.innerHTML = a.maStudentNum;
					td_maFailStudentNum.innerHTML = a.maFailStudentNum;
					td_makeupSuccessNum.innerHTML = a.makeupSuccessNum;
					tr_makeup.appendChild(td_clMajor);
					tr_makeup.appendChild(td_maStudentNum);
					tr_makeup.appendChild(td_maFailStudentNum);
					tr_makeup.appendChild(td_makeupSuccessNum);
					
					body_makeup.appendChild(tr_makeup);
					
				var tr_rebuild= document.createElement("tr");
					
					var td_clMajor = document.createElement("td");
					var td_maStudentNum = document.createElement("td");
					var td_rebuildNum=document.createElement("td");
					td_clMajor.innerHTML =a.clMajor;
					td_maStudentNum.innerHTML = a.maStudentNum;
					td_rebuildNum.innerHTML = a.rebuildNum;
					
					tr_rebuild.appendChild(td_clMajor);
					tr_rebuild.appendChild(td_maStudentNum);
					tr_rebuild.appendChild(td_rebuildNum);
					
					body_rebuild.appendChild(tr_rebuild);
					
				}
				 table_makeup.appendChild(body_makeup);
				 table_rebuild.appendChild(body_rebuild);
				 
	 		});

前端页面显示:

解决遍历List时都只能取到list的最后一条数据_第1张图片

发现都只取到了list中的最后一个值

但是后台打印出来的结果是正确的(这里我还不太清楚为什么后台的数据是正确的,没有覆盖)

 

 

错误是出现在controller中,在向List中存对象时,对象的创建应该放在for循环里面

for (int i =0;i studentlist = studentService.findAllStudentsByClMajorAndStGrade(majorsName.get(i), String.valueOf(clGrade));
			majorAnalysisData.setMaStudentNum(studentlist.size());
			//设置专业挂科人数
			List failStudentlist = studentService.findFailStudent(majorsName.get(i), String.valueOf(clGrade));
			majorAnalysisData.setMaFailStudentNum(failStudentlist.size());
			//设置补考未通过人数
			List makeupFailStudentlist = studentService.makeupFailStudent(majorsName.get(i), String.valueOf(clGrade));
			majorAnalysisData.setMakeupFailNum(makeupFailStudentlist.size());
			//设置补考通过人数
			majorAnalysisData.setMakeupSuccessNum(failStudentlist.size()-makeupFailStudentlist.size());
			//设置重修人数
			majorAnalysisData.setRebuildNum(failStudentlist.size()-makeupFailStudentlist.size());
			majorAnalysisDatalist.add(majorAnalysisData);
			System.out.println(majorAnalysisData.toString());
		}

 

 

你可能感兴趣的:(毕设)