功能的意思是:傳一張試卷的ID給我,我統計出該張試卷的(時間,題目,分數);做成三張表給出卷人閱讀,讓他來判斷這張試卷是否合適。(使用springmvc+openjpa)
而前臺最主要的是行表頭和列表頭都不確定。所以在後臺就要確定行和列,在用數據庫查詢的時候用distinct來把從表裏面取到的數據去重;
行表頭和列表頭取到以後,其餘的就是統計;根據當前試卷中的題目ID,行表頭和列表頭(條件並列);題目數用count函數,總時間用sum函數。統計出的數據以什麼格式放?才能在前臺用el表達式取出來呢?而且要和行表頭和列表頭對得上。
我先用Map(key,Value)放置每一個數據,key是列表頭;然後放置在List中;在用一個Map(Key,Value)放置上一個List,Key就是行表頭;最後在用一個List放置上一個Map這樣前臺取的時候遍歷List用key取出行表頭所在的那行數據,這樣會得到一個List,而List裏面又是一個Map在用列表頭取出每列的數據這樣就對應起來了。相當於
List<Map<Key,List<Map<Key,Value>>>>這樣的結構。
最後還有一個合計功能,經過這個合計功能我也懂得了el表達式的另一種用法,就是在兩個List中順序相同的放置對應的值,在前臺el遍歷的時候可以通過設置varStatus這個值來取出另一個List中的值
把值放在ModelAndView裏面,在前臺取出。下面是部分源碼:
後臺
//設置返回對象
ModelAndView modelAndView = new ModelAndView("/paper/previewpsheet");
//定義試卷對象
Generatedpaper generatedpaper = null;
//獲得問題列表
TypedQuery<Generatedpaper> query= EntityManagerHelper.createQuery("select o from Generatedpaper o where o.paperId=?1",Generatedpaper.class);
query.setParameter(1, paperId);
//試卷對象獲取值
generatedpaper = query.getSingleResult();
Integer productId = generatedpaper.getProduct().getProductId();
//獲取問題的集合
List<String> proIdList = Arrays.asList(generatedpaper.getProList().split(","));
List<String> scorePointList = Arrays.asList(generatedpaper.getPointsList().split(","));
//獲取列標籤
TypedQuery<Studylevel> colQuery = EntityManagerHelper.createQuery("select distinct o.problem.studylevel from ProductProblem o where o.id.productId = (?1) and o.id.proId in(?2)",Studylevel.class);
colQuery.setParameter(1, productId);
colQuery.setParameter(2, proIdList);
List<Studylevel> colList = colQuery.getResultList();
//獲取行標籤
TypedQuery<Difficulty> rowQuery = EntityManagerHelper.createQuery("select distinct o.problem.difficulty from ProductProblem o where o.id.productId=(?1) and o.id.proId in(?2)",Difficulty.class);
rowQuery.setParameter(1, productId);
rowQuery.setParameter(2, proIdList);
List<Difficulty> rowList = rowQuery.getResultList();
//獲取時間評估表
//循環獲取時間數
List<Object> timeArrayList = new ArrayList<Object>();
List<Integer> timeArrayCountList = new ArrayList<Integer>(); //統計當前行(難度)的總時間
for(int i=0;i<rowList.size();i++){
//存放一行數據的List
List<Object> timeList = new ArrayList<Object>();
//存放一行數據的Map
Map<String,Object> timeArrayMap = new LinkedHashMap<String, Object>();
Integer currRowTimeCount = 0 ;
for(int j=0;j<colList.size();j++){
//定義存放每條數據的Map
Map<String,Integer> timeMap = new LinkedHashMap<String, Integer>();
Query timeQuery = EntityManagerHelper.createQuery("select sum(o.ansTime) from Problem o where o.studylevel.levelId=?1 and o.difficulty.difId=?2 and o.proId in(?3)");
timeQuery.setParameter(1, colList.get(j).getLevelId());
timeQuery.setParameter(2, rowList.get(i).getDifId());
timeQuery.setParameter(3, proIdList);
//定義時間類型的變量
Integer time = 0;
//中轉List獲取整體時間集合
List<Object> tempList = timeQuery.getResultList();
//獲取時間
time = Integer.parseInt(tempList.get(0).toString());
//將時間放置在Map中
timeMap.put(colList.get(j).getLevelName(), time);
//將Map放置在List中
timeList.add(timeMap);
currRowTimeCount += time;
}
//將一行數據放置在Map中
timeArrayMap.put(rowList.get(i).getDifName(), timeList);
timeArrayList.add(timeArrayMap);
timeArrayCountList.add(currRowTimeCount); //加入當前行的統計總和
}
//獲取題目數評估表
//循環獲取題目統計數
List<Object> typeArrayList = new ArrayList<Object>();
List<Integer> typeArrayCountList = new ArrayList<Integer>();
for(int i=0;i<rowList.size();i++){
//存放一行數據的List
List<Object> typeList = new ArrayList<Object>();
//存放一行數據的Map
Map<String,Object> typeArrayMap = new LinkedHashMap<String, Object>();
Integer currRowTypeCount = 0 ;
for(int j=0;j<colList.size();j++){
//定義存放每條數據的Map
Map<String,Integer> typeMap = new LinkedHashMap<String, Integer>();
Query typeQuery = EntityManagerHelper.createQuery("select count(o.proId) from Problem o where o.studylevel.levelId=?1 and o.difficulty.difId=?2 and o.proId in(?3)");
typeQuery.setParameter(1, colList.get(j).getLevelId());
typeQuery.setParameter(2, rowList.get(i).getDifId());
typeQuery.setParameter(3, proIdList);
//定義題目數量的變量
Integer type = 0;
//中轉List獲取整體時間集合
List<Object> tempList = typeQuery.getResultList();
//獲取數量
type = Integer.parseInt(tempList.get(0).toString());
//將時間放置在Map中
typeMap.put(colList.get(j).getLevelName(), type );
//將Map放置在List中
typeList.add(typeMap);
currRowTypeCount += type;
}
//將一行數據放置在Map中
typeArrayMap.put(rowList.get(i).getDifName(), typeList);
typeArrayList.add(typeArrayMap);
typeArrayCountList.add(currRowTypeCount);
}
//獲取分數評估表
//循環獲取分數統計數
List<Object> scoreArrayList = new ArrayList<Object>();
List<Integer> scoreArrayCountList = new ArrayList<Integer>();
for(int i=0;i<rowList.size();i++){
//存放一行數據的List
List<Object> scoreList = new ArrayList<Object>();
//存放一行數據的Map
Map<String,Object> scoreArrayMap = new LinkedHashMap<String, Object>();
Integer currRowScoreCount = 0 ;
for(int j=0;j<colList.size();j++){
//定義存放每條數據的Map
Map<String,Integer> scoreMap = new LinkedHashMap<String, Integer>();
Query scoreQuery = EntityManagerHelper.createQuery("select o.proId from Problem o where o.studylevel.levelId=?1 and o.difficulty.difId=?2 and o.proId in(?3)");
scoreQuery.setParameter(1, colList.get(j).getLevelId());
scoreQuery.setParameter(2, rowList.get(i).getDifId());
scoreQuery.setParameter(3, proIdList);
//中轉List獲取整體選項下問題ID集合
List<Object> tempList = scoreQuery.getResultList();
//問題ID字符串
StringBuilder proIdBuilder= new StringBuilder();
for(int n=0;n<tempList.size();n++){
proIdBuilder.append(tempList.get(n).toString()).append(",");
}
StringUtils.deleteLastChar(proIdBuilder);
String proId = proIdBuilder.toString();
//定義分數
int score = 0;
//分離proId
String str[] = proId.split(",");
for(int k=0;k<str.length;k++){
for(int m=0;m<proIdList.size();m++){
if(proIdList.get(m).toString().equals(str[k])){
score += Integer.parseInt(scorePointList.get(m));
}
}
}
scoreMap.put(colList.get(j).getLevelName(), score);
//將List放置在變量List中
scoreList.add(scoreMap);
currRowScoreCount += score;
}
//將一行數據放置在Map中
scoreArrayMap.put(rowList.get(i).getDifName(), scoreList);
scoreArrayList.add(scoreArrayMap);
scoreArrayCountList.add(currRowScoreCount);
}
modelAndView.addObject("rowList", rowList);
modelAndView.addObject("colList", colList);
modelAndView.addObject("TimeArrayList", timeArrayList);
modelAndView.addObject("timeArrayCountList", timeArrayCountList);
modelAndView.addObject("typeArrayList", typeArrayList);
modelAndView.addObject("typeArrayCountList", typeArrayCountList);
modelAndView.addObject("scoreArrayList", scoreArrayList);
modelAndView.addObject("scoreArrayCountList", scoreArrayCountList);
return modelAndView;
}
前臺:
<body>
<center>
<table border="1px" border-color="black">
<th colspan="20">
時間評估表
</th>
<tr>
<td> </td>
<c:forEach items="${colList}" var="col">
<td>${col.levelName }</td>
</c:forEach>
<td>時間統計</td>
</tr>
<c:forEach items="${rowList }" var="row" varStatus="vars">
<tr>
<td>${row.difName}</td>
<c:forEach items="${colList}" var="clo">
<td>
<c:forEach items="${TimeArrayList}" varStatus="times" var="timeList">
<c:forEach items="${timeList[row.difName]}" var="time">
${time[clo.levelName]}
</c:forEach>
</c:forEach>
</td>
</c:forEach>
<td>${timeArrayCountList[vars.index]}</td>
</tr>
</c:forEach>
</table>
<br>
<table border="1px" border-color="black">
<th colspan="20">
題目評估表
</th>
<tr>
<td> </td>
<c:forEach items="${colList}" var="col">
<td>${col.levelName }</td>
</c:forEach>
<td>題目統計</td>
</tr>
<c:forEach items="${rowList }" var="row" varStatus="vars">
<tr>
<td>${row.difName}</td>
<c:forEach items="${colList}" var="clo" >
<td>
<c:forEach items="${typeArrayList}" varStatus="types" var="typeList">
<c:forEach items="${typeList[row.difName]}" var="type">
${type[clo.levelName]}
</c:forEach>
</c:forEach>
</td>
</c:forEach>
<td>${typeArrayCountList[vars.index]}</td>
</tr>
</c:forEach>
</table>
<br>
<table border="1px" border-color="black">
<th colspan="20">
分數評估表
</th>
<tr>
<td> </td>
<c:forEach items="${colList}" var="col">
<td>${col.levelName }</td>
</c:forEach>
<td>分數統計</td>
</tr>
<c:forEach items="${rowList }" var="row" varStatus="vars">
<tr>
<td>${row.difName}</td>
<c:forEach items="${colList}" var="clo">
<td>
<c:forEach items="${scoreArrayList}" varStatus="scores" var="scoreList">
<c:forEach items="${scoreList[row.difName]}" var="score">
${score[clo.levelName]}
</c:forEach>
</c:forEach>
</td>
</c:forEach>
<td>${scoreArrayCountList[vars.index]}</td>
</tr>
</c:forEach>
</table>
</center>
</body>