1. 根据ID查询所有学生信息
2. 实现对学生成绩的增删改查以及分页
- 增
- 删
- 改
- 模糊查询
- 分页
package com.example.ewspace.mapper;
import com.example.ewspace.enty.Student;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface StudentMapper {
// 新增
@Insert("insert into student(name,classes,subject,score) values(#{name},#{classes},#{subject},#{score})")
@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
boolean addStudent(Student s);
// 修改
@Update("update student set name=#{name},classes=#{classes},subject=#{subject},score=#{score} where id=#{id}")
boolean updateStudent(Student s);
// 删除
@Delete("delete from student where id = #{id}")
boolean delStudentById(int id);
// 查
@Select("select * from student where #{id}")
List<Student> selectStudentById(int id);
/**
* 分页查询
* 动态查询 type:指定一个类
* method:使用这个类中的selectWhitParamSql方法返回的sql字符串
* 作为查询的语句
*/
@Select("select * from student")
List<Student> selectStudentList();
}
package com.example.ewspace.controller;
import com.example.ewspace.enty.Student;
import com.example.ewspace.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/testStudent")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("selectStudent/{id}")
public List<Student> SelectStudent(@PathVariable int id) {
return studentService.SelectStudent(id);
}
public boolean addStudent(@PathVariable Student s) {
return studentService.addStudent(s);
}
@RequestMapping("/fy")
public PageInfo<Student> selectStudentListByName(@RequestParam(value="pageNum",defaultValue="1") int pageNum,
@RequestParam(value="pageSize",defaultValue="5") int pageSize) {
PageInfo<Student> pageInfo = studentService.selectStudentList(pageNum,pageSize);
return pageInfo;
}
}
package com.example.ewspace.service;
import com.example.ewspace.enty.Student;
import com.example.ewspace.mapper.StudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
StudentMapper studentMapper;
public List<Student> SelectStudent(int id) {
return studentMapper.selectStudentById(id);
}
public boolean addStudent(Student s) {
return studentMapper.addStudent(s);
}
public PageInfo<Student> selectStudentList(int pageNum, int pageSize) {
//1.startPage--start,设置当前页和每页显示条数
PageHelper.startPage(pageNum,pageSize);
//2.填充自己的sql查询逻辑
List<Student> list = studentMapper.selectStudentList();
//3.pageHelper收尾,将返回结果信息进行分页处理
PageInfo<Student> pageResult = new PageInfo<Student>(list);
return pageResult;
}
}
package test;
import com.example.ewspace.EwspaceApplication;
import com.example.ewspace.enty.Student;
import com.example.ewspace.mapper.StudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@SpringBootTest(classes = EwspaceApplication.class)
@RunWith(SpringRunner.class)
public class StudentTest {
@Autowired
private StudentMapper studentMapper;
@Test
public void testSelectStudentById() {
List<Student> list = studentMapper.selectStudentById(1);
list.forEach(System.out::println);
}
@Test
public void testaddStudent() {
Student s = new Student();
s.setName("zhangshan");
s.setClasses("3");
s.setSubject("s2");
s.setScore("88");
boolean i = studentMapper.addStudent(s);
System.out.println(i ? "添加成功":"添加失败");
}
@Test
public void updateStudent() {
Student s = new Student();
s.setId(2);
s.setName("nanjing");
s.setClasses("2");
s.setSubject("s2");
s.setScore("78");
boolean i = studentMapper.updateStudent(s);
System.out.println(i ? "修改成功":"修改失败");
}
@Test
public void delStudent() {
boolean i = studentMapper.delStudentById(13);
System.out.println(i ? "删除成功":"删除失败");
}
@Test
public void selectStudentListById() {
int pageNum = 1;
int pageSize = 5;
PageHelper.startPage(pageNum,pageSize);
List<Student> list = studentMapper.selectStudentList();
PageInfo<Student> pageResult = new PageInfo<Student>(list);
System.out.println(pageResult);
}
}
自己举一反三吧:
访问网址:http://localhost:8081/testStudent/fy/