StudentSystem 학생 관리 시스템V1(3)
7767 단어 SSM 프로젝트
[com.ray.service] 패키지 아래에 [Student Service] 인터페이스를 만들려면 다음과 같이 하십시오.
/**
* @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";
}
}
페이지 나누기 기능
Packge [util] 패키지 아래에 Page 도구 클래스를 먼저 만듭니다.
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;
}
}