MVC模式和三层架构

MVC模式

MVC分层开发模式

  • MVC是一种分层开发的模式,其中: :::info M:Model,业务模型,处理业务
    V:View,视图,界面展示
    C:Controller,控制器,处理请求,调用模型和视图 ::: image.png

    MVC好处

  • 职责单一,互不影响

  • 有利于分工协作
  • 有利于组件重用

    三层架构

    三层架构(图文)
    image.png

    MVC模式和三层架构图

    image.png

    案例练习

    image.png

    准备环境

  • 创建新的模块 brand_demo,引入坐标

  • 创建三层架构的包结构
  • 数据库表 tb_brand
  • 实体类 Brand
  • MyBatis 基础环境 :::info Mybatis - config.xml
    BrandMapper.xml
    BrandMapper接口 :::

    建立新模块项目

    :::info brand_demo ::: image.png

    pom引入需要坐标

    ```java

    org.mybatis mybatis 3.5.5 mysql mysql-connector-java 5.1.36
  1. <dependency>
  2. <groupId>javax.servlet</groupId>
  3. <artifactId>javax.servlet-api</artifactId>
  4. <version>3.1.0</version>
  5. <scope>provided</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>javax.servlet.jsp</groupId>
  9. <artifactId>jsp-api</artifactId>
  10. <version>2.2</version>
  11. <scope>provided</scope>
  12. </dependency>
  13. <!-- jstl-->
  14. <dependency>
  15. <groupId>jstl</groupId>
  16. <artifactId>jstl</artifactId>
  17. <version>1.2</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>taglibs</groupId>
  21. <artifactId>standard</artifactId>
  22. <version>1.1.2</version>
  23. </dependency>

  1. <a name="p0EUH"></a>
  2. ### pom引入tomcat插件
  3. ```java
  4. <build>
  5. <plugins>
  6. <plugin>
  7. <groupId>org.apache.tomcat.maven</groupId>
  8. <artifactId>tomcat7-maven-plugin</artifactId>
  9. <version>2.2</version>
  10. </plugin>
  11. </plugins>
  12. </build>

创建三层架构包结构

:::info com.taotao.web
com.taotao.service
com.taotao.mapper
com.taotao.pojo
com.taotao.util :::

使用数据库

:::info 仍然使用mybatis数据库中的tb_brand表
image.png ::: image.png

Brand实体类

  1. package com.taotao.pojo;
  2. /**
  3. * create by 刘鸿涛
  4. * 2022/3/29 16:19
  5. */
  6. public class Brand {
  7. private Integer id;
  8. private String brand_name;
  9. private String company_name;
  10. private Integer ordered;
  11. private String description;
  12. private Integer status;
  13. public Brand(){
  14. }
  15. public Brand(Integer id, String brand_name, String company_name, Integer ordered, String description, Integer status) {
  16. this.id = id;
  17. this.brand_name = brand_name;
  18. this.company_name = company_name;
  19. this.ordered = ordered;
  20. this.description = description;
  21. this.status = status;
  22. }
  23. public Integer getId() {
  24. return id;
  25. }
  26. public void setId(Integer id) {
  27. this.id = id;
  28. }
  29. public String getBrand_name() {
  30. return brand_name;
  31. }
  32. public void setBrand_name(String brand_name) {
  33. this.brand_name = brand_name;
  34. }
  35. public String getCompany_name() {
  36. return company_name;
  37. }
  38. public void setCompany_name(String company_name) {
  39. this.company_name = company_name;
  40. }
  41. public Integer getOrdered() {
  42. return ordered;
  43. }
  44. public void setOrdered(Integer ordered) {
  45. this.ordered = ordered;
  46. }
  47. public String getDescription() {
  48. return description;
  49. }
  50. public void setDescription(String description) {
  51. this.description = description;
  52. }
  53. public Integer getStatus() {
  54. return status;
  55. }
  56. public void setStatus(Integer status) {
  57. this.status = status;
  58. }
  59. @Override
  60. public String toString() {
  61. return "Brand{" +
  62. "id=" + id +
  63. ", brand_name='" + brand_name + '\'' +
  64. ", company_name='" + company_name + '\'' +
  65. ", ordered=" + ordered +
  66. ", description='" + description + '\'' +
  67. ", status=" + status +
  68. '}';
  69. }
  70. }

xml配置文件

mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!-- 起别名-->
  7. <typeAliases>
  8. <package name="com.taotao.pojo"/>
  9. </typeAliases>
  10. <environments default="development">
  11. <environment id="development">
  12. <transactionManager type="JDBC"/>
  13. <dataSource type="POOLED">
  14. <property name="driver" value="com.mysql.jdbc.Driver"/>
  15. <property name="url" value="jdbc:mysql:///mybatis?useSSL=false&amp;useServerPrepStmts=true"/>
  16. <property name="username" value="root"/>
  17. <property name="password" value="12345"/>
  18. </dataSource>
  19. </environment>
  20. </environments>
  21. <mappers>
  22. <!-- 扫描mapper-->
  23. <package name="com/taotao/mapper"/>
  24. </mappers>
  25. </configuration>

创建映射文件夹

:::info resources 下的 com.taotao.mapper :::

创建映射文件

:::info BrandMapper.xml :::

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.taotao.mapper.BrandMapper">
  6. <select id="selectAll" resultType="com.taotao.pojo.Brand">
  7. select * from tb_brand
  8. </select>
  9. </mapper>

创建BrandMapper接口

:::info com.taotao.mapper.BrandMapper.java :::

  1. package com.taotao.mapper;
  2. /**
  3. * create by 刘鸿涛
  4. * 2022/3/29 16:12
  5. */
  6. public interface BrandMapper {
  7. }

完成“查询所有”功能

流程分析

image.png

  1. package com.taotao.mapper;
  2. import com.taotao.pojo.Brand;
  3. import org.apache.ibatis.annotations.Select;
  4. import java.util.List;
  5. /**
  6. * create by 刘鸿涛
  7. * 2022/3/29 16:12
  8. */
  9. public interface BrandMapper {
  10. @Select("select * from tb_brand")
  11. List<Brand> selectAll();
  12. }

编写service

:::info 新建BrandService.java :::

  1. package com.taotao.service;
  2. import com.taotao.mapper.BrandMapper;
  3. import com.taotao.pojo.Brand;
  4. import com.taotao.util.SqlSessionfactoryUtils;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import java.util.List;
  8. /**
  9. * create by 刘鸿涛
  10. * 2022/3/29 16:59
  11. */
  12. @SuppressWarnings({"all"})
  13. public class BrandService {
  14. /**
  15. * 查询所有
  16. * @return
  17. */
  18. public List<Brand> selectAll(){
  19. //调用BrandMapper.selectAll()
  20. SqlSessionFactory factory = SqlSessionfactoryUtils.getSqlSessionFactory();
  21. //2.获取sqlSession
  22. SqlSession sqlSession = factory.openSession();
  23. //3.获取BrandMapper
  24. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
  25. //4.调用方法
  26. List<Brand> brands = mapper.selectAll();
  27. //5.关闭资源
  28. sqlSession.close();
  29. return brands;
  30. }
  31. }

编写index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <a href="/brand_demo/selectAllServlet">查询所有</a>
  9. </body>
  10. </html>

编写Servlet

  1. package com.taotao.web;
  2. import com.taotao.pojo.Brand;
  3. import com.taotao.service.BrandService;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.util.List;
  11. /**
  12. * create by 刘鸿涛
  13. * 2022/3/29 17:31
  14. */
  15. @WebServlet("/selectAllServlet")
  16. public class selectAllServlet extends HttpServlet {
  17. private BrandService service = new BrandService();
  18. @Override
  19. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  20. //1.调用BrandService完成查询
  21. List<Brand> brands = service.selectAll();
  22. //2.存入request域中
  23. req.setAttribute("brands",brands);
  24. //3.转发到brand.jsp
  25. req.getRequestDispatcher("/brand.jsp").forward(req,resp);
  26. }
  27. @Override
  28. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  29. this.doGet(req, resp);
  30. }
  31. }

编写JSP

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: guigui
  4. Date: 2022/3/29
  5. Time: 10:32
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  10. <%@ page isELIgnored="false" %>
  11. <html>
  12. <head>
  13. <title>Title</title>
  14. </head>
  15. <body>
  16. <input type="button" value="新增"><br>
  17. <hr>
  18. <table border="1" cellspacing="0" width="800">
  19. <tr>
  20. <th>序号</th>
  21. <th>品牌名称</th>
  22. <th>公司名称</th>
  23. <th>价格</th>
  24. <th>描述</th>
  25. <th>状态</th>
  26. </tr>
  27. <c:forEach items="${brands}" var="brand" varStatus="status">
  28. <tr align="center">
  29. <td>${brand.id}</td>
  30. <td>${brand.brand_name}</td>
  31. <td>${brand.company_name}</td>
  32. <td>${brand.ordered}</td>
  33. <td>${brand.description}</td>
  34. <c:if test="${brand.status == 1}">
  35. <td>启用</td>
  36. </c:if>
  37. <c:if test="${brand.status != 1}">
  38. <td>禁用</td>
  39. </c:if>
  40. </tr>
  41. </c:forEach>
  42. </table>
  43. </body>
  44. </html>

运行测试

:::info 因为数据库字段名与 实体类 字段名不同,所以没有显示brandName和companyName ::: image.png

显示品牌名称、公司名称resultMap

mybatis开发sql

:::info 使用resultMap :::

编写BrandMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.taotao.mapper.BrandMapper">
  6. <select id="selectAll" resultMap="brandResultMap">
  7. select * from tb_brand
  8. </select>
  9. <resultMap id="brandResultMap" type="brand">
  10. <result column="brandName" property="brand_name"></result>
  11. <result column="companyName" property="company_name"></result>
  12. </resultMap>
  13. </mapper>

编写BrandMapper.java

:::info 为BrandMapper.java加上@ResultMap注解 :::

  1. package com.taotao.mapper;
  2. import com.taotao.pojo.Brand;
  3. import org.apache.ibatis.annotations.ResultMap;
  4. import org.apache.ibatis.annotations.Select;
  5. import java.util.List;
  6. /**
  7. * create by 刘鸿涛
  8. * 2022/3/29 16:12
  9. */
  10. public interface BrandMapper {
  11. // @Select("select * from tb_brand")
  12. // @ResultMap("brandResultMap")
  13. List<Brand> selectAll();
  14. }

测试运行

image.png

注解开发SQL

编写BrandMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.taotao.mapper.BrandMapper">
  6. <!-- <select id="selectAll" resultMap="brandResultMap">-->
  7. <!-- select * from tb_brand-->
  8. <!-- </select>-->
  9. <resultMap id="brandResultMap" type="brand">
  10. <result column="brandName" property="brand_name"></result>
  11. <result column="companyName" property="company_name"></result>
  12. </resultMap>
  13. </mapper>

编写BrandMapper.java

  1. package com.taotao.mapper;
  2. import com.taotao.pojo.Brand;
  3. import org.apache.ibatis.annotations.ResultMap;
  4. import org.apache.ibatis.annotations.Select;
  5. import java.util.List;
  6. /**
  7. * create by 刘鸿涛
  8. * 2022/3/29 16:12
  9. */
  10. public interface BrandMapper {
  11. @Select("select * from tb_brand")
  12. @ResultMap("brandResultMap")
  13. List<Brand> selectAll();
  14. }

测试运行

image.png