StudentSystem 학생 관리 시스템V1(3)

7767 단어 SSM 프로젝트
비즈니스 클래스 설계
  • 질문: 왜 Dao 클래스를 직접 사용하지 않고 서비스 레이어를 위에 봉인해야 합니까?
  • 대답: 책임 분리의 원칙을 바탕으로 Dao층은 데이터베이스에 대한 조작에 전념해야 한다. 서비스 층에서 우리는 CRUD가 아닌 방법을 추가하여 자신이 추출한 서비스(업무 처리)를 더욱 잘 완성할 수 있다.

  • [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;
        }
    }
    

    좋은 웹페이지 즐겨찾기