自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MongoDB實(shí)現(xiàn)問卷/考試設(shè)計(jì)

數(shù)據(jù)庫 其他數(shù)據(jù)庫 后端 MongoDB
MongoDB是一個(gè)面向文檔存儲(chǔ)的數(shù)據(jù)庫。在MongoDB中,一條記錄叫做document(文檔),由類似于JSON結(jié)構(gòu)的鍵值對(duì)組成。

MongoDB的特點(diǎn)

MongoDB是一個(gè)面向文檔存儲(chǔ)的數(shù)據(jù)庫。在MongoDB中,一條記錄叫做document(文檔),由類似于JSON結(jié)構(gòu)的鍵值對(duì)組成。

由于類似于MongoDB直接存儲(chǔ)JSON的特性,MongoDB天生適合作為存儲(chǔ)結(jié)構(gòu)復(fù)雜的數(shù)據(jù)結(jié)構(gòu)的介質(zhì)。類似于問卷調(diào)查和考試這種需求,用mysql這種關(guān)系型數(shù)據(jù)庫實(shí)現(xiàn)起來太過復(fù)雜,效率低下;而如果使用MongoDB來實(shí)現(xiàn)的話,則會(huì)發(fā)現(xiàn)異常清晰簡單。

需求分析

在一張?jiān)嚲碇?,?huì)有很多個(gè)問題,問題的類型大體上可以分為單選題、多選題、判斷題、簡答題等。每一個(gè)問題又會(huì)有很多個(gè)選項(xiàng),選項(xiàng)可以是文字描述也可以是圖片又或者圖文結(jié)合。

那么一張?jiān)嚲淼腏SON格式應(yīng)該大體上長成這樣:

當(dāng)然這只是最簡單的數(shù)據(jù)結(jié)構(gòu),要完成一張?jiān)嚲?,還需要加入更多的屬性。

結(jié)構(gòu)設(shè)計(jì)

我們采用自底向上的結(jié)構(gòu)設(shè)計(jì)方式,先對(duì)每個(gè)選項(xiàng)的數(shù)據(jù)結(jié)構(gòu)進(jìn)行設(shè)計(jì)。

選項(xiàng)設(shè)計(jì) 

  1. public class Option {  
  2.     /**  
  3.      * 選項(xiàng)類型  
  4.      */  
  5.     private Integer oType = 1 
  6.     /**  
  7.      * 選項(xiàng)內(nèi)容  
  8.      */  
  9.     private String text;    
  10.     /**  
  11.      * 選項(xiàng)圖片  
  12.      */  
  13.     private String img;  
  14.     /**  
  15.      * 是否正確答案  
  16.      */  
  17.     private Boolean right;  
  18.     /**  
  19.      * 用戶是否選擇  
  20.      */  
  21.     private Boolean selected;  
  22.     ... 

選項(xiàng)類型oType用來標(biāo)志選項(xiàng)是普通文本還是圖片或者圖文;right用來標(biāo)志這個(gè)選項(xiàng)是否是正確答案,用于自動(dòng)判卷;selected用來標(biāo)志用戶有沒有選擇這個(gè)答案。

問題設(shè)計(jì) 

  1. public class Question extends MongoBean {  
  2.     /**  
  3.      * 數(shù)據(jù)的id  
  4.      */  
  5.     private String dataId;  
  6.     /**  
  7.      * 題目類型,1判斷題;2單選題;3多選題  
  8.      */  
  9.     private Integer qType;  
  10.     /**  
  11.      * 題目標(biāo)題  
  12.      */  
  13.     private String title;  
  14.     /**  
  15.      * 題目選項(xiàng)  
  16.      */  
  17.     private List<Option> options;  
  18.     /**  
  19.      * 數(shù)據(jù)類型  
  20.      * @see rmjk.enums.BizTypeEnum  
  21.      */  
  22.     private Integer dataType;  
  23.     /**  
  24.      * 數(shù)據(jù)標(biāo)題  
  25.      */  
  26.     private String dataTitle;  
  27.     /**  
  28.      * 解析  
  29.      */  
  30.     private String analysis;  
  31.     /**  
  32.      * 這題是否答對(duì)  
  33.      */  
  34.     private Boolean right;  
  35.     /**  
  36.      * 這題答的時(shí)長  
  37.      */  
  38.     private Long duration;  
  39.     /**  
  40.      * 這題的得分  
  41.      */  
  42.     private Long points;  
  43.     ... 

dataId用于將這個(gè)問題同一個(gè)業(yè)務(wù)數(shù)據(jù)綁定,dataType用來標(biāo)志這個(gè)業(yè)務(wù)數(shù)據(jù)的類型,這兩個(gè)字段方便數(shù)據(jù)的擴(kuò)展;dataTitle是業(yè)務(wù)數(shù)據(jù)的標(biāo)題;options是這個(gè)問題的選項(xiàng);analysis問題的解析,用于用戶答題結(jié)束后的自查;right用來記錄問題的正確與否。

新增問題

上層接口

提供新增問題的接口:

  1. @PostMapping("/saveOrUpdateQuestion")  
  2. public JsonData saveOrUpdateQuestion(@RequestBody Question data) {  
  3.     questionService.saveOrUpdateQuestion(data);  
  4.     return JsonData.success();  

QuestionService: 

  1. public void saveOrUpdateQuestion(Question data) {  
  2.     if (StringUtils.isEmpty(data.getId())) {// 新增  
  3.         writer.insert(manager.getExamDataBase(), ExamConstant.QUESTION_COLLECT, data);  
  4.     } else {//修改  
  5.         writer.updateDocument(data, ExamConstant.QUESTION_COLLECT);  
  6.     }  

DAO

Writer: 

  1. public void insert(String dataBase, String collect, MongoBean data) {  
  2.     if (data.getId() == null) {  
  3.         data.setId(BsonTool.uuid());  
  4.     }  
  5.     MongoCollection<Document> collection = getCollection(dataBase, collect);  
  6.     collection.insertOne(Document.parse(JSONObject.toJSONString(data))); 
  7.  
  8. public Document updateDocument(MongoBean data, String questionCollect) {  
  9.     Document filter = new Document();  
  10.     filter.put("id", data.getId());  
  11.     Document res = new Document();  
  12.     res.put("$set", BsonDocument.parse(JSONObject.toJSONString(data)));  
  13.     update(manager.getExamDataBase(), questionCollect, filter, res);  
  14.     return res;  
  15.  
  16. public boolean update(String dataBase, String collect, Bson filter, Bson update) {  
  17.     MongoCollection<Document> collection = getCollection(dataBase, collect);  
  18.     UpdateResult ur = collection.updateOne(filter, update);  
  19.     return ur.getModifiedCount() > 0;  

這樣后端的工作就全部完成了,接下來就是前端怎么給后端提供這樣的數(shù)據(jù)結(jié)構(gòu)了。

前端實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)

前端使用vue實(shí)現(xiàn)JSON的構(gòu)造: 

  1. <Modal title="問題編輯" v-model="showEdit" :closable="false" :mask-closable="false">  
  2.     <Form ref="question" :model="question" :rules="ruleValidate">  
  3.         <FormItem label="題目類型:" prop="qType">  
  4.             <Select v-model="question.qType" class="input-180" placeholder="題目類型" @on-change="changeQType(question)">  
  5.                 <Option v-for="d in qTypes" :value="d.value" :key="d.value">{{ d.label }}</Option>  
  6.             </Select>  
  7.         </FormItem>  
  8.         <FormItem label="題目:" prop="title">  
  9.             <Input  
  10.                    class="input-95-per"  
  11.                    v-model="question.title"  
  12.                    type="textarea"  
  13.                    row="1"  
  14.                    placeholder="題目"  
  15.                    ></Input>  
  16.         </FormItem>  
  17.         <FormItem label="選項(xiàng):">  
  18.             <div v-for="(o, i2) in question.options" :key="i2" style="display:flex">  
  19.                 <Input class="input-95-per margin-bot-8 margin-right-10" v-model="o.text">  
  20.                     <span slot="prepend">{{i2+1}}:</span>  
  21.                 </Input>  
  22.                 <Button size="small" @click="addOpt(question)" v-if="i2===0">+</Button>  
  23.                 <Button size="small" @click="delOpt(question, o)" v-if="i2">-</Button>  
  24.                 <Checkbox v-model="o.right">正確答案</Checkbox>  
  25.             </div>  
  26.         </FormItem>  
  27.         <FormItem label="答案解析:">  
  28.             <Input  
  29.                    class="input-95-per"  
  30.                    v-model="question.analysis"  
  31.                    type="textarea"  
  32.                    row="1"  
  33.                    placeholder="答案解析"  
  34.                    ></Input>  
  35.         </FormItem>  
  36.     </Form>  
  37.     <div slot="footer">  
  38.         <Button type="text" @click="cancelQuestion">取消</Button>  
  39.         <Button type="primary" :loading="saveLoading" @click="saveQuestion">保存</Button>  
  40.     </div>  
  41. </Modal> 

這里綁定的question就是一個(gè)問題了。而一張?jiān)嚲韯t是由多個(gè)問題,再加上試卷的額外屬性構(gòu)成的。

在question上的dataId剛好就能綁定上試卷的id 

  1. Exam exam = new Exam();  
  2. List<Question> questions = reader.findRandom(manager.getExamDataBase(), ExamConstant.QUESTION_COLLECT, new Document(), Question.class, no);  
  3. exam.setTitle(title);  
  4. exam.setDuration(dutation);  
  5. return exam; 

 

 

責(zé)任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2021-12-08 09:43:46

細(xì)節(jié)Google問卷工具

2017-07-07 10:55:14

數(shù)據(jù)庫MongoDB設(shè)計(jì)模式

2017-07-25 16:34:06

數(shù)據(jù)庫sqlmongodb

2009-09-03 11:17:01

PHP問卷調(diào)查系統(tǒng)

2011-07-15 09:11:39

MySQLMongoDB

2018-05-21 08:07:35

聚合MongoDBSchema

2011-12-14 18:33:18

IBM

2010-09-02 14:17:32

網(wǎng)絡(luò)釣魚

2014-03-28 09:35:11

MongoDBSharding

2011-07-13 16:25:46

Imperva調(diào)查

2012-02-17 14:55:25

Fujitsu富士通

2009-09-10 09:53:40

CCNA考試流程CCNA

2009-06-26 11:54:25

2009-12-24 13:31:25

WPF UI設(shè)計(jì)

2009-02-12 10:13:00

綜合布線系統(tǒng)設(shè)計(jì)

2020-12-28 07:33:21

SkipListJava跳表

2015-08-14 13:34:55

斯諾登NSA

2015-11-09 17:28:01

OpenStack調(diào)查

2014-02-17 17:29:43

CocoaChinaCocos2d-x
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)