数据:
let questionType = 1;//单选题1 多选题2 判断题3
let label = null;//标签string
let title = null;//题目
let difficulty = null;//难度 1 2 3 4 5 (1最简单 5最难)
let items = null,//单选和多选有4项 【{”a1": "选项1内容", "a2": "选项2内容",”a3": "选项3内容", "a4": "选项4内容"}】 判断题有2项 【{”a1": "选项1内容", "a2": "选项2内容"}】
let correct = null;//单选和判断只有一项如“a1” 多选有多项 如“a2”,“a4”】
let solution = null;//题目解析 可选项,也可以不写解析
数据的标签放在<form class="form-horizontal" id="single-answer-form" action="#" method="post" enctype="multipart/form-data" onsubmit="return false;">
习题只建两个表 question表和textcontent表
前端ajax发送格式
let data = {};
data["title"] = "中国的首都是_____";
let items = {};
items["a1"] = "东京";
items["a2"] = "西京";
items["a3"] = "南京";
items["a4"] = "北京";
data["items"] = items;
data["solution"] = "东京是日本的首都,没有城市叫做西京,南京是中国的城市,北京才是中国的首都";
console.log("要发送的数据是"+data);
$(document).ready(function(){
$("#test").click(function(){
$.ajax({
"url": "/lhm/platform/insert_question",
"type": "post",
"data":{
"questionType": 1,
"difficulty": 2,
"correct": "a1",
"label": "决策树",
"content": JSON.stringify(data)
},
datatype: 'json',
success: function (data) {
console.log(data);
//习题添加成功
showSuccessMsg(data, function() { });
},
error: function (data) {
showErrorMsg("后台失败");
}
});
});
});
后端接收存储格式
@RequestMapping(value = "/insert_question", method = RequestMethod.POST)
@ResponseBody
public String insert_question(Question question, TextContent textContent){
log.info("接收到添加习题的请求");
if( textContentService.save(textContent)==null ){
return "保存失败";
}
if( textContent.getId()==null ){
return "保存失败";
}
question.setInfoTextContentId(textContent);
log.info("要添加的习题: "+question.toString());
if( questionService.save(question)==null ){
return "保存失败";
}
if( question.getId()==null ){
return "保存失败";
}
return question.toString();
}
数据库表的建立
question表
@Entity
@Table(name = "aial_question")
@EntityListeners(AuditingEntityListener.class)
public class Question extends BaseEntity {
@Column( name = "question_type", nullable = false )
private Integer questionType;
@Column( name = "difficulty", nullable = false )
private Integer difficulty;
@Column( name = "correct", nullable = false, length = 36 )
private String correct;
@Column( name = "label", nullable = false, length = 36)
private String label;
// @Column( name = "info_text_content_id", nullable = false )
@OneToOne( cascade = CascadeType.ALL, fetch = FetchType.LAZY )
@JoinColumn( name = "text_content")
private TextContent infoTextContentId;
}
textcontent表
@Entity
@Table(name = "aial_text_content")
@EntityListeners(AuditingEntityListener.class)
public class TextContent extends BaseEntity {
/**
* 内容(Json)
*/
@Column( name = "content", nullable = false, length = 1024)
private String content;
public TextContent(){
}
public TextContent(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "TextContent{" +
"content='" + content + '\'' +
'}';
}
}