薄情先生|java开发:S S M 整合开发详解( 四 )

实现类
package com.md.service.impl;import com.md.dao.StudentDao;import com.md.domain.Student;import com.md.service.StudentService;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;/** * @author MD * @create 2020-08-13 20:32 */@Servicepublic class StudentServiceImpl implements StudentService {// 引用数据类型的自动注入@Resource、@Autowired@Resourceprivate StudentDao studentDao;@Overridepublic int addStudent(Student student) {int nums = studentDao.insertStudent(student);return nums;}@Overridepublic List findStudents() {return studentDao.selectStudents();}}10. 处理器在controller包下
package com.md.controller;import com.md.domain.Student;import com.md.service.StudentService;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import javax.annotation.Resource;import java.util.List;/** * @author MD * @create 2020-08-13 20:37 */@Controller@RequestMapping("/student")public class StudentController {// 自动注入@Resourceprivate StudentService service;// 注册学生@RequestMapping("/addStudent.do")public ModelAndView addStudent(Student student){ModelAndView mv = new ModelAndView();String tips = "注册失败";// 调用service处理studentint nums = service.addStudent(student);if (nums > 0){tips = "注册成功";}// 添加数据 , 指定页面mv.addObject("tips",tips);mv.setViewName("result");return mv;}// 处理查询,响应ajax@RequestMapping("/queryStudent.do")@ResponseBodypublic List queryStudent(){List students = service.findStudents();// 会被框架转为json的数组return students;}}11. 首页index.jsp
功能入口SSM整合学生注册学生信息12. 注册页面addStudent.jsp
学生注册

姓名:年龄:
13. 注册结果在/WEB-INF/jsp/result.jsp
Title注册结果:${tips}14. 浏览页面listStudent.jsp
学生信息学号姓名年龄此时再配置Tomcat运行即可 , 之后再添加新的功能


推荐阅读