新建【mapper】包,并在其下新增StudentMapper接口:
package com.wmyskxz.demo.mapper;
import com.wmyskxz.demo.pojo.Student;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface StudentMapper {
@Select("select * from student")
List<Student> findAll();
@Insert("insert into student ( name ) values (#{name}) ")
int save(Student student);
@Delete("delete from student where id= #{id} ")
void delete(int id);
@Select("select * from student where id= #{id} ")
Student get(int id);
@Update("update student set name=#{name} where id=#{id} ")
int update(Student student);
}
在【controller】包下新增一个【StudentController】类:
package com.wmyskxz.demo.controoler;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wmyskxz.demo.mapper.StudentMapper;
import com.wmyskxz.demo.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
public class StudentController {
@Autowired
StudentMapper studentMapper;
@RequestMapping("/addStudent")
public String listStudent(Student student) throws Exception {
studentMapper.save(student);
return "redirect:listStudent";
}
@RequestMapping("/deleteStudent")
public String deleteStudent(Student student) throws Exception {
studentMapper.delete(student.getId());
return "redirect:listStudent";
}
@RequestMapping("/updateStudent")
public String updateStudent(Student student) throws Exception {
studentMapper.update(student);
return "redirect:listStudent";
}
@RequestMapping("/editStudent")
public String listStudent(int id, Model m) throws Exception {
Student student = studentMapper.get(id);
m.addAttribute("student", student);
return "editStudent";
}
@RequestMapping("/listStudent")
public String listStudent(Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
PageHelper.startPage(start, size, "id desc");
List<Student> students = studentMapper.findAll();
PageInfo<Student> page = new PageInfo<>(students);
m.addAttribute("page", page);
return "listStudent";
}
}
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-91180-11.html
一心想控制住南海
一楼