在【com.ray.service】包下创建【StudentService】接口:
/**
* @author Ray
* @date 2018/5/27 0027
* Service 层我们可以增加一些非 CRUD 的方法去更好的完成本身抽离出来的 service 服务
*/
public interface StudentService {
/**
* 获取到 Student 的总数
* @return
*/
int getTotal();
/**
* 增加一条数据
* @param student
*/
void addStudent(Student student);
/**
* 删除一条数据
* @param id
*/
void deleteStudent(int id);
/**
* 更新一条数据
* @param student
*/
void updateStudent(Student student);
/**
* 找到一条数据
* @param id
* @return
*/
Student getStudent(int id);
/**
* 列举出从 start 位置开始的 count 条数据
* @param start
* @param count
* @return
*/
List list(int start, int count);
}
在【com.ray.service.impl】包下创建【StudentServiceImpl】:
/**
* @author Ray
* @date 2018/5/27 0027
* StudentService 的实现类
*/
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
StudentDao studentDao;
public int getTotal() {
return studentDao.getTotal();
}
public void addStudent(Student student) {
studentDao.addStudent(student);
}
public void deleteStudent(int id) {
studentDao.deleteStudent(id);
}
public void updateStudent(Student student) {
studentDao.updateStudent(student);
}
public Student getStudent(int id) {
return studentDao.getStudent(id);
}
public List list(int start, int count) {
return studentDao.list(start, count);
}
}
在【com.ray.controller】包下创建【StudentController】控制器:
/**
* @author Ray
* @date 2018/5/27 0027
* Student 控制器
*/
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
/**
* 新增学生视图
*/
@RequestMapping("/addStudentView")
public ModelAndView addStudentView(){
ModelAndView modelAndView = new ModelAndView("addStudentView");
return modelAndView;
}
/**
* 新增学生操作
*/
@RequestMapping("/addStudent")
public String addStudent(HttpServletRequest request, HttpServletResponse response){
Student student = new Student();
int studentId = Integer.parseInt(request.getParameter("student_id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");
Date birthday = null;
// String 类型按照 yyyy-MM-dd 的格式转换为 java.util.Date 类
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try{
birthday = simpleDateFormat.parse(request.getParameter("birthday"));
}catch (Exception e){
e.printStackTrace();
}
student.setStudent_id(studentId);
student.setName(name);
student.setAge(age);
student.setSex(sex);
student.setBirthday(birthday);
studentService.addStudent(student);
return "redirect:listStudent"; // 重定向
}
/**
* 列出所有学生操作
*/
@RequestMapping("/listStudent")
public String listStudent(HttpServletRequest request, HttpServletResponse response){
// 获取分页参数
int start = 0;
int count = 10;
try{
start = Integer.parseInt(request.getParameter("page.start"));
count = Integer.parseInt(request.getParameter("page.count"));
}catch (Exception e){
e.printStackTrace();
}
Page page = new Page(start, count);
List students = studentService.list(page.getStart(), page.getCount());
int total = studentService.getTotal();
page.setTotal(total);
request.setAttribute("students", students);
request.setAttribute("page", page);
return "listStudent";
}
/**
* 修改学生信息视图
*/
@RequestMapping("/editStudent")
public ModelAndView editStudent(int id){
ModelAndView modelAndView = new ModelAndView("editStudent");
Student student = studentService.getStudent(id);
modelAndView.addObject("student",student);
return modelAndView;
}
/**
* 修改学生信息操作
*/
@RequestMapping("/updateStudent")
public String updateStudent(HttpServletRequest request, HttpServletResponse response){
Student student = new Student();
int id = Integer.parseInt(request.getParameter("id"));
int student_id = Integer.parseInt(request.getParameter("student_id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date birthday = null;
try{
birthday = simpleDateFormat.parse(request.getParameter("birthday"));
}catch (Exception e){
e.printStackTrace();
}
student.setId(id);
student.setStudent_id(student_id);
student.setName(name);
student.setAge(age);
student.setSex(sex);
student.setBirthday(birthday);
studentService.updateStudent(student);
return "redirect:listStudent";
}
/**
* 删除学生信息操作
*/
@RequestMapping("/deleteStudent")
public String deleteStudent(int id){
studentService.deleteStudent(id);
return "redirect:listStudent";
}
}
package com.ray.util;
/**
* @author Ray
* @date 2018/5/27 0027
* 分页功能
*/
public class Page {
int start; // 开始数据
int count; // 每一页的数量
int total; // 总共的数据量
public Page(int start, int count){
super();
this.start = start;
this.count = count;
}
public boolean isHasPreviouse(){
if(start == 0){
return false;
}
return true;
}
public boolean isHasNext(){
if(start == getLast()){
return false;
}
return true;
}
public int getTotalPage(){
int totalPage;
//假设总数50,是能够被5整除的,那么就有10页
if(0 == total % count){
totalPage = total / count;
}else{
//假设总数51,不能被5整除的,那么就有11页
totalPage = total / count + 1;
}
if(0 == totalPage){
totalPage = 1;
}
return totalPage;
}
public int getLast(){
int last;
//假设总数是50,能够被5整除的,那么最后一页的开始就是40
if(0 == total % count){
last = total - count;
}else{
//假设总数是51,不能够被5整除的,那么最后一个的开始就是50
last = total - total % count;
}
last = last < 0 ? 0:last;
return last;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}