mysql資料庫隨機生成某個範圍內的資料
阿新 • • 發佈:2020-08-14
一、完成內容
1. 根據組卷規則隨機生成試題
2. 獲取考生答案,計算分數
3. 判斷答案正誤,錯題存到錯題表
4. 判斷考試是否第一次答題,若不是,使用第一次生成的試題
二、功能截圖
三、核心程式碼
/** * @param context 頁面傳過來的題號及其對應的答案,形式是 題號:ABCD */ @Override public Integer stuTest(String testId, String context, String stuname) { // TODO Auto-generated method stub//宣告一個變數分數 int rightNum =0; String bids=""; //處理答案,計算分數 //1.處理文字 String str[] = context.split(","); for (int i = 0; i < str.length; i++) { //獲取對應的試題id String bid = str[i].substring(0, str[i].indexOf(":"));//獲取對應答案 String answer = str[i].substring(str[i].indexOf(":")+1); //根據bid查詢出題庫中正確的答案 ItemBank itemBank = itemBankDao.selectByBid(new Integer(bid)); //判斷答案是否一致 answer = answer.replaceAll(" ", ""); String itemBankAnswer= itemBank.getAnswer().replace(" ", ""); if(answer.equalsIgnoreCase(itemBankAnswer)) { rightNum=rightNum+1; System.out.println("rightNum>>>"+rightNum); }else { //查詢判斷是否含有此bid MisCollection collection = misCollectionDao.selectByStuname(new Integer(stuname)); if(collection!=null) { String misbids = collection.getBids(); if(!misbids.contains(bid)) { misbids+=","+bid; misCollectionDao.update(misbids,new Integer(stuname)); } }else { //第一次做題 if(i==0) { bids=bid; }else { bids+=","+bid; } //將錯題存到表中 misCollectionDao.add(bids,new Integer(stuname)); } } } Double score = (double)((rightNum/str.length)*100); //將分數,答案更新到試卷表(testpaper) return testPaperDao.updateScoreAndAnswer(score,context,new Integer(testId),new Integer(stuname)); }
四、資料庫隨機範圍查詢
<!-- 隨機生成試卷 -->
<select id="makePaper" resultMap="itemBank">
SELECT * FROM itembank
WHERE bid >= (SELECT floor((select MIN(bid) from itembank where subid=#{0})+RAND() * (SELECT MAX(bid) FROM itembank where subid=#{0})))
ORDER BY bid LIMIT #{1}
</select>