项目实训第一周第二篇


xclass系统1.0版本学习


一、本地数据库搭建

下载并安装了HediSQL工具,使用了以前在本地搭建的MySQL会话,通过HediSQL连接并打开会话。
在这里插入图片描述
新建xclass1数据库,单击它,在菜单中选择运行SQL文件,选择xclass.sql,刷新。
在这里插入图片描述
本地数据库搭建完毕。

二、服务器开发

配置文件:

server.port=8081
spring.datasource.username=root
spring.datasource.password=Zzh/121380
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc\:mysql\://localhost\:3306/xclass1?serverTimezone\=UTC
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

1.答题统计API

编写实体类AnswerStatistics:

package com.example.demo.demain;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "t_answer_statistics")
public class AnswerStatistics {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "a_id")
	private Long id; //答题记录id
	@Column(name = "q_id")
	private Long qId; //题ID
	@Column(name = "answerer_date")
	private Date answerDate;//答题日期
	@Column(name = "device_id") //上传数据的TV设备 id
	private String deviceId;
	@Column(name = "answer_sum")
	private int answerSum;//答题人数
	@Column(name = "correct_sum")
	private int correctSum;//正确人数
	//省略getter/setter tostring方法
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getqId() {
		return qId;
	}
	public void setqId(Long qId) {
		this.qId = qId;
	}
	public Date getAnswerDate() {
		return answerDate;
	}
	public void setAnswerDate(Date answerDate) {
		this.answerDate = answerDate;
	}
	public String getDeviceId() {
		return deviceId;
	}
	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}
	public int getAnswerSum() {
		return answerSum;
	}
	public void setAnswerSum(int answerSum) {
		this.answerSum = answerSum;
	}
	public int getCorrectSum() {
		return correctSum;
	}
	public void setCorrectSum(int correctSum) {
		this.correctSum = correctSum;
	}
	@Override
	public String toString() {
		return "AnswerStatistics [id=" + id + ", qId=" + qId + ", answerDate=" + answerDate + ", deviceId=" + deviceId
				+ ", answerSum=" + answerSum + ", correctSum=" + correctSum + "]";
	}
}

编写接口AnswerStatisticsDao:

/**
 * 
 */
package com.example.demo.dao;

/**
 * @author Administrator
 *
 */

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.example.demo.demain.AnswerStatistics;

public interface AnswerStatisticsDao extends JpaRepository<AnswerStatistics, Long>, 
JpaSpecificationExecutor<AnswerStatistics>{

}

编写Controller:

package com.example.demo.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.dao.AnswerStatisticsDao;
import com.example.demo.demain.AnswerStatistics;

@RequestMapping("/as")
@Controller
public class AnswerStatisticsController {
	@Autowired
	private AnswerStatisticsDao answerStatisticsDao;
	@RequestMapping("/findAll")
	@ResponseBody
	public Object findAll(){
	return answerStatisticsDao.findAll();
	}
	@RequestMapping("/findOne")
	@ResponseBody
	public Object findOne(Long id){
	return answerStatisticsDao.findById(id);
	}
	@RequestMapping("/save")
	@ResponseBody
	public Object save(AnswerStatistics answerStatistics){
	answerStatistics.setAnswerDate(new Date());
	return answerStatisticsDao.save(answerStatistics);
	}

}

测试答题统计API:
运行Demo1Application.java,选择作为Java Appilication运行:
在这里插入图片描述
在浏览器中输入网址http://127.0.0.1:8081/as/findAll,测试答题统计API:
在这里插入图片描述
API正常运行

2.题库列表API

编写实体类Question:

package com.example.demo.demain;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 *  题实体类 
 */

@Entity
@Table(name = "t_question")
public class Question {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "q_id")
    private Long qId;

    @Column(name = "q_num")
    private Integer qNum;

    @Column(name = "q_stem")
    private String qStem; //题干


    @JsonIgnoreProperties({"question"})
    @OneToMany(mappedBy = "question")
    private List<QOption> qOptions =new ArrayList<>();//选项


	public Long getqId() {
		return qId;
	}


	public void setqId(Long qId) {
		this.qId = qId;
	}


	public Integer getqNum() {
		return qNum;
	}


	public void setqNum(Integer qNum) {
		this.qNum = qNum;
	}


	public String getqStem() {
		return qStem;
	}


	public void setqStem(String qStem) {
		this.qStem = qStem;
	}


	public List<QOption> getqOptions() {
		return qOptions;
	}


	public void setqOptions(List<QOption> qOptions) {
		this.qOptions = qOptions;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((qId == null) ? 0 : qId.hashCode());
		result = prime * result + ((qNum == null) ? 0 : qNum.hashCode());
		result = prime * result + ((qOptions == null) ? 0 : qOptions.hashCode());
		result = prime * result + ((qStem == null) ? 0 : qStem.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Question other = (Question) obj;
		if (qId == null) {
			if (other.qId != null)
				return false;
		} else if (!qId.equals(other.qId))
			return false;
		if (qNum == null) {
			if (other.qNum != null)
				return false;
		} else if (!qNum.equals(other.qNum))
			return false;
		if (qOptions == null) {
			if (other.qOptions != null)
				return false;
		} else if (!qOptions.equals(other.qOptions))
			return false;
		if (qStem == null) {
			if (other.qStem != null)
				return false;
		} else if (!qStem.equals(other.qStem))
			return false;
		return true;
	} 
}

编写实体类QOption:

package com.example.demo.demain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 *  选项实体类
 */

@Entity
@Table(name = "t_option")
public class QOption {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "o_id")
    private Integer id;

    @Column(name = "o_num" )
    private String num;
    @Column(name = "o_content")
    private String content;//题干


    @Column(name = "o_answer")
    private boolean answer;

    @ManyToOne(targetEntity = Question.class)
    @JoinColumn(name = "o_q_id",referencedColumnName = "q_id")
    private Question question;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public boolean isAnswer() {
		return answer;
	}

	public void setAnswer(boolean answer) {
		this.answer = answer;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (answer ? 1231 : 1237);
		result = prime * result + ((content == null) ? 0 : content.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((num == null) ? 0 : num.hashCode());
		result = prime * result + ((question == null) ? 0 : question.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		QOption other = (QOption) obj;
		if (answer != other.answer)
			return false;
		if (content == null) {
			if (other.content != null)
				return false;
		} else if (!content.equals(other.content))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (num == null) {
			if (other.num != null)
				return false;
		} else if (!num.equals(other.num))
			return false;
		if (question == null) {
			if (other.question != null)
				return false;
		} else if (!question.equals(other.question))
			return false;
		return true;
	}
    
 
}

编写接口QuestionDao:

package com.example.demo.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.example.demo.demain.Question;

public interface QuestionDao extends JpaRepository<Question, Long>, JpaSpecificationExecutor<Question> {
}

编写接口QOptionDao:

package com.example.demo.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.example.demo.demain.QOption;

public interface QOptionDao extends JpaRepository<QOption, Long>, JpaSpecificationExecutor<QOption> {
}

编写返回类型ResultData类:

package com.example.demo.demain;

import java.io.Serializable;
import java.util.List;

public class ResultData implements Serializable {
    private List<Question> data = null;

    public ResultData(List<Question> data) {
        this.data = data;
    }

    public ResultData() {
    }

    public List<Question> getData() {
        return data;
    }

    public void setData(List<Question> data) {
        this.data = data;
    }
}

编写QuestionController:

package com.example.demo.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.dao.QuestionDao;
import com.example.demo.demain.ResultData;

@RequestMapping("/question")
@Controller
public class QuestionController {
    @Autowired
    private QuestionDao questionDao;

    @RequestMapping("/findAll")
    @ResponseBody
    public Object findAll(){
    	ResultData result = new ResultData();
    	result.setData(questionDao.findAll());
        return result;

    }
    @RequestMapping("/findOne")
    @ResponseBody
    public Object findOne(Long id){
        return questionDao.findById(id);

    }
}

测试题库列表API:
运行Demo1Application.java,选择作为Java Appilication运行:
在这里插入图片描述
在浏览器中输入网址http://127.0.0.1:8081/question/findAll,测试题库列表API:
在这里插入图片描述

API正常运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值