标题目录
Utils(工具类)
package f.demo.Utils;
/*
* 返回json工具类
* @Param date
* @Param code msg
* @return
* */
public class JsonResult<T> {
private T data;
private String code;
private String msg;
/**
* 若没有数据返回,默认状态码为 0,提示信息为“操作成功!”
*/
public JsonResult() {
this.code = "0";
this.msg = "操作成功!";
}
/**
* 若没有数据返回,可以人为指定状态码和提示信息
* @param code
* @param msg
*/
public JsonResult(String code, String msg) {
this.code = code;
this.msg = msg;
}
/**
* 有数据返回时,状态码为 0,默认提示信息为“操作成功!”
* @param data
*/
public JsonResult(T data) {
this.code = "0";
this.msg = "操作成功!";
this.data = data;
}
/**
* 有数据返回,状态码为 0,人为指定提示信息
* @param data
* @param msg
*/
public JsonResult(T data, String msg) {
this.code = "0";
this.msg = msg;
this.data = data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
Contorller部分
student
package f.demo.Contorller;
import f.demo.Entity.Student;
import f.demo.StudentService.StudentService;
import f.demo.Utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
@RequestMapping("student1")
public class StudentContorller {
@Autowired//自动注入Student服务层
StudentService studentService;
@RequestMapping("/toindex1")
public String find(Model model){
return "vue";
}
@RequestMapping("/vue1")
@ResponseBody
public JsonResult date(){
//显示页面
List<Student> data =studentService.find1();
if(!CollectionUtils.isEmpty(data)){
//判断集合是否为空
return new JsonResult<>(data,"获取班级列表成功");
}else {
return new JsonResult<>("-1","获取数据失败");
}
}
@RequestMapping("/doAdd1")//添加
@ResponseBody
public String doAdd(Student student) {
studentService.doCreat(student);
return "添加";
}
@RequestMapping("/doDel1") //删除
@ResponseBody
public JsonResult doDel1(HttpServletRequest request) {
int sid = Integer.parseInt(request.getParameter("sid"));
studentService.doDelete(sid);
return new JsonResult<>("0","删除数据成功");
}
@RequestMapping("/edit1")
@ResponseBody
public JsonResult edit(HttpServletRequest request) {
//修改数据
int sid = Integer.parseInt(request.getParameter("sid"));
Student student=studentService.getOne(sid);
return new JsonResult<>(student,"获取编号"+sid+"数据成功");
}
//vue自定义查询和分页
@RequestMapping("/index1")
@ResponseBody
public JsonResult index(Student student, HttpServletRequest request){
//List<Student> data= studentService.findAll(Sort.by("sid"));
int page=Integer.parseInt(request.getParameter("page"));
// System.out.print(request.getParameter("page"));
Page<Student> data= studentService.queryFlows(page,5,student);
return new JsonResult<>(data, "获取学生列表成功");
}
}
Score
package f.demo.Contorller;
import f.demo.Entity.Course;
import f.demo.Entity.Score;
import f.demo.StudentService.ScoreService;
import f.demo.Utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("score1")
public class ScoreController {
@Autowired
ScoreService scoreService;
@RequestMapping("/sc1")
public JsonResult index(){
List<Score> data =scoreService.findAll();
if(!CollectionUtils.isEmpty(data)){
//判断集合是否为空
return new JsonResult<>(data,"获取班级列表成功");
}else {
return new JsonResult<>("-1","获取数据失败");
}
}
}
Course
package f.demo.Contorller;
import f.demo.Entity.Course;
import f.demo.StudentService.CourseService