当前位置:   article > 正文

JavaWeb06-MVC和三层架构_javamvc三层架构

javamvc三层架构

目录

一、MVC模式

1.概述

2.好处

二、三层架构

1.概述

三、MVC与三层架构

四、练习


一、MVC模式

1.概述

MVC是一种分层开发的模式,其中

M:Model,业务模型,处理业务 V: View,视图,界面展示 C:Controller,控制器,处理请求,调用型和视图

2.好处

  • 职责单一,互不影响

  • 有利于分工协作

  • 有利于组件重用

二、三层架构

1.概述

View视图不只是JSP!

数据访问层(持久层):对数据库的CRUD基本操作

一般命名为反转公司网址/controller

业务逻辑层(业务层):对业务逻辑进行封装,组合数据访问层层中基本功能,形成复杂的业务逻辑功能。

一般命名为反转公司网址/service

表现层:接收请求,封装数据,调用业务逻辑层,响应数据

一般命名为反转公司网址/dao或者mapper

三、MVC与三层架构

四、练习

给上次的数据添加一个状态字段,0禁用,1启用,2预售,设个默认值1即可

使用三层架构思想开发

参考下图:

java目录结构

代码,只写主要的了

service包下

  1. public class ProductService {
  2.   final SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
  3.   public List<Product> selectAll(){
  4.       final SqlSession sqlSession = sqlSessionFactory.openSession();
  5.       final ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
  6.       final List<Product> products = mapper.selectAll();
  7.       sqlSession.close();
  8.       return products;
  9.   };
  10. }

web包下

  1. @WebServlet("/selectAll")
  2. public class selectAll extends HttpServlet {
  3.    private final ProductService productService = new ProductService();
  4.    @Override
  5.    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  6.        final List<Product> products = productService.selectAll();
  7.        request.setAttribute("product",products);
  8.        request.getRequestDispatcher("/jsp/product.jsp").forward(request,response);
  9.   }
  10.    @Override
  11.    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12.        this.doGet(request, response);
  13.   }
  14. }

之后回到视图层

jsp:

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%--
  3.  Created by IntelliJ IDEA.
  4.  User: LEGION
  5.  Date: 2024/3/13
  6.  Time: 13:36
  7.  To change this template use File | Settings | File Templates.
  8. --%>
  9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  10. <%@ page isELIgnored="false" %>
  11. <%--<% final Object product = request.getAttribute("product");%>--%>
  12. <html>
  13. <head>
  14.    <title>Title</title>
  15. </head>
  16. <body>
  17.    <h1>product列表</h1>
  18.    <table border="1px solid black">
  19.        <tr>
  20.            <th>商品序号</th>
  21.            <th>商品id</th>
  22.            <th>商品名</th>
  23.            <th>商品图片</th>
  24.            <th>商品价格</th>
  25.            <th>商品评论数</th>
  26.            <th>商品分类</th>
  27.            <th>商品状态</th>
  28.            <th>商品发布时间</th>
  29.            <th>商品更新时间</th>
  30.        </tr>
  31.        <c:forEach items="${product}" var="product" varStatus="status">
  32.            <tr>
  33.                   <%--                index从0开始,count从1开始--%>
  34.                <td>${status.count}</td>
  35.                   <%--                ${user.id} => Id => getId()--%>
  36.                <td>${product.id}</td>
  37.                <td>${product.title}</td>
  38.                <td><img src="${product.imgUrl}" alt="" width="75px" height="75px"></td>
  39.                <td>${product.price}</td>
  40.                <td>${product.comment}</td>
  41.                <td>${product.category}</td>
  42.                <td>${product.status}</td>
  43.                <td>${product.gmtCreate}</td>
  44.                <td>${product.gmtModified}</td>
  45.            </tr>
  46.        </c:forEach>
  47.        </table>
  48.    </body>
  49. </html>

预览图:

添加:

html

  1. <form action="/product_demo_war/add" method="post">
  2.      <input type="text" name="title" placeholder="商品名称"><br>
  3.      <input type="text" name="price" placeholder="商品价格"><br>
  4. <!--       图片上传在这里就先不写了-->
  5.        <input type="number" name="category" placeholder="商品类型(数字就好)"><br>
  6.        <input type="radio" name="status">启用
  7.        <input type="radio" name="status">禁用
  8.        <br>
  9.        <input type="submit" value="添加">
  10.    </form>

service:

  1. /**
  2. * 添加商品
  3. * @param product 商品对象
  4. */
  5. public void add(Product product){
  6.    final SqlSession sqlSession = sqlSessionFactory.openSession();
  7.    final ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
  8.    mapper.add(product);
  9.    sqlSession.commit();
  10.    sqlSession.close();
  11. }

web:

  1. @WebServlet("/add")
  2. public class Add extends HttpServlet {
  3.    @Override
  4.    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  5.        ProductService productService = new ProductService();
  6.        final String title = request.getParameter("title");
  7.        final String price = request.getParameter("price");
  8.        final Integer status = Integer.parseInt(request.getParameter("status"));
  9.        final Integer category = Integer.parseInt(request.getParameter("category"));
  10.        Product product = new Product(title,price,category,status);
  11.        productService.add(product);
  12.        request.getRequestDispatcher("/selectAll").forward(request,response);
  13.   }
  14.    @Override
  15.    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16.        this.doGet(request, response);
  17.   }
  18. }

预览:

修改:

有两步:

  • 显示原先的数据:回显

  • 修改现有的数据:修改

第一部分回显:根据id显示值

service:

  1. public Product selectById(Long id){
  2.    final SqlSession sqlSession = sqlSessionFactory.openSession();
  3.    final ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
  4.    final Product product = mapper.selectById(id);
  5.    sqlSession.close();
  6.    return product;
  7. }

web:

  1. @WebServlet("/selectById")
  2. public class SelectById extends HttpServlet {
  3.    @Override
  4.    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  5.        final String id = request.getParameter("id");
  6.        ProductService productService = new ProductService();
  7.        final Product product = productService.selectById(Long.parseLong(id));
  8.        request.setAttribute("product",product);
  9.        System.out.println(id);
  10.        request.getRequestDispatcher("/jsp/productUpdate.jsp").forward(request,response);
  11.   }
  12.    @Override
  13.    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14.        this.doGet(request, response);
  15.   }
  16. }

显示层:productUpdate.jsp

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <%@ page isELIgnored="false" %>
  4. <html>
  5. <head>
  6.    <title>修改</title>
  7.  <style>
  8.   .text {
  9.      width: 100%;
  10.   }
  11.  </style>
  12. </head>
  13. <body>
  14. <form action="/product_demo_war/updateById" method="post">
  15.  <input type="hidden" name="id" value="${product.id}">
  16.  <input class="text" type="text" name="title" placeholder="商品名称" value="${product.title}"><br>
  17.  <input class="text" type="text" name="price" placeholder="商品价格" value="${product.price}"><br>
  18.  <!--        图片上传在这里就先不写了-->
  19.  <input class="text" type="number" name="category" placeholder="商品类型(数字就好)" value="${product.category}"><br>
  20.  <c:if test="${product.status == 1}">
  21.    <input type="radio" name="status" value="1" checked>启用
  22.    <input type="radio" name="status" value="0">禁用
  23.  </c:if>
  24.  <c:if test="${product.status == 0}">
  25.    <input type="radio" name="status" value="1">启用
  26.    <input type="radio" name="status" value="0" checked>禁用
  27.  </c:if>
  28.  <br>
  29.  <input type="submit" value="修改">
  30. </form>
  31. </body>
  32. </html>

第二部分修改:

service:

  1. public void UpdateById(Product product){
  2.    final SqlSession sqlSession = sqlSessionFactory.openSession();
  3.    final ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
  4.    mapper.updateById(product);
  5.    sqlSession.commit();
  6. }

servlet:

  1. @WebServlet("/updateById")
  2. public class Update extends HttpServlet {
  3.    @Override
  4.    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  5.        final Long id = Long.parseLong(request.getParameter("id"));
  6.        final String title = request.getParameter("title");
  7.        final String price = request.getParameter("price");
  8.        final Integer category = Integer.parseInt(request.getParameter("category"));
  9.        final Integer status = Integer.parseInt(request.getParameter("status"));
  10.        Date gmtModified = new Date();
  11.        Product product = new Product(id,title,price,category,status,gmtModified);
  12.        ProductService productService = new ProductService();
  13.        productService.UpdateById(product);
  14.        request.getRequestDispatcher("/selectAll").forward(request,response);
  15.   }
  16.    @Override
  17.    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  18.        this.doGet(request, response);
  19.   }
  20. }

测试:

删除:不写了,jsp知道怎么写就行了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/992048
推荐阅读
相关标签
  

闽ICP备14008679号