1 package com.itheima.web.servlet; 2 3 import cn.hutool.core.io.IoUtil; 4 import com.fasterxml.jackson.databind.ObjectMapper; 5 import com.itheima.domain.Student; 6 import com.itheima.service.StudentService; 7 import com.itheima.service.impl.StudentServiceImpl; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletInputStream; 11 import javax.servlet.annotation.WebServlet; 12 import javax.servlet.http.HttpServlet; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 import java.io.IOException; 16 import java.util.List; 17 18 @WebServlet("/studentServlet") 19 public class StudentServlet extends HttpServlet { 20 @Override 21 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 22 // 接收action,确定请求目的 23 String action = req.getParameter("action"); 24 if ("findAll".equals(action)) { 25 findAll(req, resp); 26 } else if ("save".equals(action)) { 27 save(req, resp); 28 } else if ("deleteById".equals(action)) { 29 deleteById(req, resp); 30 } else if ("findById".equals(action)) { 31 findById(req, resp); 32 } else if ("update".equals(action)) { 33 update(req, resp); 34 } else { 35 resp.getWriter().write("方法请求错误"); 36 } 37 } 38 39 // 修改 40 private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException { 41 // 1. 获取请求参数 42 ServletInputStream is = req.getInputStream(); 43 String json = IoUtil.read(is, "utf-8"); 44 Student student = new ObjectMapper().readValue(json, Student.class); 45 // 2. 调用业务逻辑 46 StudentService studentService = new StudentServiceImpl(); 47 studentService.update(student); 48 // 3. 响应结果 49 resp.getWriter().write("OK"); 50 } 51 52 // 根据id查询学生(回显) 53 private void findById(HttpServletRequest req, HttpServletResponse resp) throws IOException { 54 // 1. 获取请求参数 55 String id = req.getParameter("id"); 56 // 2. 调用业务逻辑 57 StudentService studentService = new StudentServiceImpl(); 58 Student student = studentService.findById(id); 59 // 3. 响应结果 60 String json = new ObjectMapper().writeValueAsString(student); 61 resp.getWriter().write(json); 62 } 63 64 // 根据id删除 65 private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException { 66 // 1. 获取请求参数 67 String id = req.getParameter("id"); 68 // 2. 调用业务逻辑 69 StudentService studentService = new StudentServiceImpl(); 70 studentService.deleteById(id); 71 // 3. 响应结果 72 resp.getWriter().write("OK"); 73 74 } 75 76 // 新增 77 private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException { 78 // 1. 获取请求参数(如果页面提交的数据为json格式,则req.getPXXX方法不能用了,只能使用字节流获取) 79 // String id = req.getParameter("id"); 80 ServletInputStream is = req.getInputStream(); 81 // 把字节数据转换为指定编码格式的字符串{key:value} 82 String json = IoUtil.read(is, "utf-8"); 83 Student student = new ObjectMapper().readValue(json, Student.class); 84 // 2. 调用业务逻辑 85 StudentService studentService = new StudentServiceImpl(); 86 studentService.save(student); 87 // 3. 响应结果 88 resp.getWriter().write("OK"); 89 } 90 91 // 查询所有 92 private void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException { 93 // 1. 获取请求参数(无) 94 95 // 2. 调用业务逻辑 96 StudentService studentService = new StudentServiceImpl(); 97 List<Student> studentList = studentService.findAll(); 98 // 3. 响应结果 99 String json = new ObjectMapper().writeValueAsString(studentList); 100 resp.getWriter().write(json); 101 } 102 }StudentServlet
1 package com.itheima.service; 2 3 import com.itheima.domain.Student; 4 5 import java.util.List; 6 7 public interface StudentService { 8 9 List<Student> findAll(); 10 11 void save(Student student); 12 13 void deleteById(String id); 14 15 Student findById(String id); 16 17 void update(Student student); 18 }StudentService
1 package com.itheima.service.impl; 2 3 import com.itheima.domain.Student; 4 import com.itheima.mapper.StudentMapper; 5 import com.itheima.service.StudentService; 6 import com.itheima.util.MyBatisUtil; 7 import org.apache.ibatis.session.SqlSession; 8 9 import java.util.List; 10 11 public class StudentServiceImpl implements StudentService { 12 @Override 13 public List<Student> findAll() { 14 SqlSession sqlSession = MyBatisUtil.getSqlSession(); 15 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); 16 List<Student> list = studentMapper.findAll(); 17 MyBatisUtil.close(sqlSession); 18 return list; 19 } 20 21 @Override 22 public void save(Student student) { 23 SqlSession sqlSession = MyBatisUtil.getSqlSession(); 24 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); 25 studentMapper.save(student); 26 MyBatisUtil.close(sqlSession); 27 } 28 29 @Override 30 public void deleteById(String id) { 31 SqlSession sqlSession = MyBatisUtil.getSqlSession(); 32 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); 33 studentMapper.deleteById(id); 34 MyBatisUtil.close(sqlSession); 35 } 36 37 @Override 38 public Student findById(String id) { 39 SqlSession sqlSession = MyBatisUtil.getSqlSession(); 40 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); 41 Student student = studentMapper.findById(id); 42 MyBatisUtil.close(sqlSession); 43 return student; 44 } 45 46 @Override 47 public void update(Student student) { 48 SqlSession sqlSession = MyBatisUtil.getSqlSession(); 49 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); 50 studentMapper.update(student); 51 MyBatisUtil.close(sqlSession); 52 } 53 }StudentServiceImpl
1 package com.itheima.mapper; 2 3 import com.itheima.domain.Student; 4 import org.apache.ibatis.annotations.Delete; 5 import org.apache.ibatis.annotations.Insert; 6 import org.apache.ibatis.annotations.Select; 7 import org.apache.ibatis.annotations.Update; 8 9 import java.util.List; 10 11 public interface StudentMapper { 12 @Select("select * from student") 13 List<Student> findAll(); 14 15 @Insert("insert into student values(#{id},#{name},#{birthday},#{address})") 16 void save(Student student); 17 18 @Delete("delete from student where id = #{id}") 19 void deleteById(String id); 20 21 @Select("select * from student where id = #{id}") 22 Student findById(String id); 23 24 @Update("update student set name = #{name}, birthday = #{birthday}, address = #{address} where id = #{id}") 25 void update(Student student); 26 }StudentMapper
标签:resp,架构,Student,代码,req,student,import,三层,id From: https://www.cnblogs.com/Rover20230226/p/17389372.html