首页 > 其他分享 >三层架构 —— 具体代码

三层架构 —— 具体代码

时间:2023-05-10 21:23:17浏览次数:34  
标签:resp 架构 Student 代码 req student import 三层 id

  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

相关文章

  • AI别来搅局,chatGPT的世界不懂低代码
    ChatGPT单月访问量再创新高根据SimilarWeb统计,ChatGPT上月全球访问量17.6亿次,已超越必应、鸭鸭走DuckDuckGo等其他国际搜索引擎,并达到谷歌的2%,百度的60%。 这会,程序员失业的段子又得再来一遍了:拖拽建站出来的时候,他们人说程序员会失业;低代码出来了,他们说程序员会失业;Copilo......
  • 使用 Python 语言实现的简单版俄罗斯方块的代码示例
    importpygameimportrandompygame.init()#定义颜色BLACK=(0,0,0)WHITE=(255,255,255)GRAY=(128,128,128)CYAN=(0,255,255)BLUE=(0,0,255)ORANGE=(255,165,0)YELLOW=(255,255,0)GREEN=(0,128,0)PURPLE=(128,0,128)#定义方块......
  • 五种应该避免的代码注释
    酷壳:http://CoolShell.cn/在酷壳,有很多文章都提到了代码注释,如:《十条不错的编程观点》、《优质代码的十诫》、《整洁代码的4个提示》、《惹恼程序员的十件事》等等。今天,某国外的程序员在这里列举五种应该避免的程序注释,我觉得比较有道理,但我觉得有少数几个观点也并不绝对。所以,......
  • 万字长文详解如何使用Swift提高代码质量
    前言京喜APP最早在2019年引入了Swift,使用Swift完成了第一个订单模块的开发。之后一年多我们持续在团队/公司内部推广和普及Swift,目前Swift已经支撑了70%+以上的业务。通过使用Swift提高了团队内同学的开发效率,同时也带来了质量的提升,目前来自Swift的Crash的占比不到1%。在这过程......
  • 异步电机有速度传感器矢量控制算法的C代码+仿真模型,仿真采用C代码直接在Simulink模型
    异步电机有速度传感器矢量控制算法的C代码+仿真模型,仿真采用C代码直接在Simulink模型里进行仿真的方式,当你不具备硬件调试的条件时,可以通过这种方法直接对代码进行仿真验证,所见即所得!采用双闭环解耦控制算法,转速外环电流内环,转矩与励磁解耦控制,SVPWM空间电压矢量调制,电流谐波很小,......
  • JAVA代码编写的30条建议推荐
    JAVA代码编写的30条建议推荐http://topmanopensource.iteye.com/blog/667247 (1)类名首字母应该大写。字段、方法以及对象(句柄)的首字母应小写。对于所有标识符,其中包含的所有单词都应紧靠在一起,而且大写中间单词的首字母。例如:ThisIsAClassNamethisIsMethodOrFieldName   ......
  • 低代码开发平台 新型企业中台解决方案
    企业业务系统按照价值链分为前台和后台,前台是发散的一端,是运用与演绎,后台是收敛的一端,是归纳与抽象。前台敏态地响应需求,后台稳定有序地面向内部系统或者固定业务逻辑实现管理。数字化创新时代,前台的创新速度、敏捷性和后台系统的稳定性之间,存在前快、后慢的“齿轮差距”,这就需要......
  • 无需代码绘制人工神经网络ANN模型结构图的方法
      本文介绍几种基于在线网页或软件的、不用代码的神经网络模型结构可视化绘图方法。  之前向大家介绍了一种基于Python第三方ann_visualizer模块的神经网络结构可视化方法,大家可以直接点击文章Python绘制神经网络模型图进行查看;这一方法可以对Dense隐藏层以及MaxPooling层、D......
  • app直播源代码,高仿软件评论底部弹出框
    app直播源代码,高仿软件评论底部弹出框这个弹窗的效果是使用BottomSheetDialogFragment做的,第一个弹出的对话框为CommentListDialogFragment,第二个弹出的对话框为SendCommentDialogFragment,代码如下: 展示CommentListDialogFragment。  publicclassCommentListDialogFra......
  • 百人研发团队百亿销售规模的技术架构实践分享
    公司背景公司融资10亿,剥离B2B生鲜业务板块为独立公司运营。除部分核心产品经理,运营,采购角色外,诸如研发团队等重新组建,并承接部分历史系统重新打造一整套供应链平台去支撑大规模业务扩张。全国70个左右仓(包含前置仓,中转仓和实体仓),实际达到百亿业务规模(营收),预计达到千亿业务规模......