• springMVC


    什么是MVC

    MVC 是 模型(model)、视图(view)、控制器(controller) 的简写,是一种软件设计规范。

    模型(dao、service)、视图(jps)、控制器(servlet)

    MVC 的作用是 :降低了视图与业务逻辑间的双向耦合

    MVC不是一种设计模式,他是一种架构模式

    职责分析 :

    controller 控制器

    • 取得表单数据
    • 调用业务逻辑
    • 转向指定的页面

    Model :模型

    • 业务逻辑
    • 保存数据的状态

    view :视图

    • 显示页面

    在这里插入图片描述

    回顾 servlet

    1、创建一个 maven 项目(父工程),引入依赖

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.3.9version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.0version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    2、创建 web 项目spring-01-servlet,引入依赖

    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>servlet-apiartifactId>
        <version>2.5version>
    dependency>
    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>jsp-apiartifactId>
        <version>2.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3、编写一个 servlet 类,用来处理用户请求

    package com.hkp.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class HelloServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取前端参数
            String method = req.getParameter("method");
            if(method.equals("add")){
                req.getSession().setAttribute("msg","执行了add方法");
            }
            if(method.equals("delete")){
                req.getSession().setAttribute("msg","执行了 delete 方法");
            }
            if(method.equals("update")){
                req.getSession().setAttribute("msg","执行了 update 方法");
            }
            //调用业务层
            //视图跳转
            req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    4、编写页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
    ${msg}
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    5、在 web.xml 中注册 servlet

    <servlet>
        <servlet-name>helloservlet-name>
        <servlet-class>com.hkp.servlet.HelloServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>helloservlet-name>
        <url-pattern>/hellourl-pattern>
    servlet-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    6、配置 Tomcat 启动测试

    访问路径 :

    http://localhost:8080/servlet/hello?method=add

    http://localhost:8080/servlet/hello?method=delete

    http://localhost:8080/servlet/hello?method=update

    在这里插入图片描述

    MVC 做的事情

    • 封装用户提交的数据
    • 处理请求 – 调用相关业务处理
    • 将响应的数据进行渲染 到页面

    注意 :

    常见的 MVC 框架有 :Struts、spring MVC、ASP .NET MVC、JSF

    spring MVC

    什么是spring MVC

    spring MVC 是 spring的一部分,是基于java 实现的MVC 的轻量级 Web 框架。

    spring MVC 特点

    • 轻量级,简单易学
    • 高效,基于请求响应的 MVC 框架
    • 与 spring 无缝结合
    • 约定大于配置
    • 功能强大 :RESTful、数据验证、格式化、本地化等

    spring 的web框架围绕着 DispatcherServlet [ 调度 servlet ] 设计。

    DispatcherServlet 的作用是将请求分发到不同的处理器

    springMVC 框架和其他 MVC 框架一样,以请求为驱动,围绕一个中心 servlet 分派请求及提供其他功能,DispatcherServlet 是一个实际的 servlet (他继承了 HTTPServlet基类)

    在这里插入图片描述

    在这里插入图片描述

    代码演示

    1、在 WEB-INF 文件夹下面创建一个 jsp 文件夹,在jsp 文件夹下创建一个 test.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
    ${msg}
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、在 web.xml 中配置DispatcherServlet

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
       
       <servlet>
           <servlet-name>springmvcservlet-name>
           <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            
           <init-param>
               <param-name>contextConfigLocationparam-name>
               <param-value>classpath:spring-mvc.xmlparam-value>
           init-param>
    
           <load-on-startup>1load-on-startup>
       servlet> 
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    3、在 resources 文件夹中 创建 spring-mvc.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
        
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value="jsp"/>
        bean>
        
        <bean id="/hello" class="com.hkp.controller.HelloController" />
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    4、创建 controller

    package com.hkp.controller;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class HelloController implements Controller {
        public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
            ModelAndView mv = new ModelAndView();
            String name = "Hello SpringMVC";
            mv.addObject("msg",name);
            //视图跳转
            mv.setViewName("test");
            return mv;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    5、启动 Tomcat 测试 localhost:8080/hello

    在这里插入图片描述

    注意 :如果报404

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    使用注解开发 mvc

    1、在 WEB-INF 文件夹下面创建一个 jsp 文件夹,在jsp 文件夹下创建一个 test.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
    ${msg}
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、在 web.xml 中配置DispatcherServlet

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
       
       <servlet>
           <servlet-name>springmvcservlet-name>
           <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            
           <init-param>
               <param-name>contextConfigLocationparam-name>
               <param-value>classpath:spring-mvc.xmlparam-value>
           init-param>
    
           <load-on-startup>1load-on-startup>
       servlet> 
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    3、在 resources 文件夹中 创建 spring-mvc.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <context:component-scan base-package="com.hkp.controller"/>
    
        <mvc:default-servlet-handler/>
        <mvc:annotation-driven/>
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        bean>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    4、创建 controller

    package com.hkp.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloController{
        @RequestMapping("/hello")
        public String test(Model model){
            model.addAttribute("msg","hello springmvc");
            return "test";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    5、启动Tomcat运行localhost:8080/hello

    在这里插入图片描述

    如果报404 解决办法和上面一样,添加lib

    RestFul 风格

    RestFul 风格是一个资源定位和资源操作的风格。不是标准也不是协议,只是一种风格。

    功能 :

    • 可以改变路径传参数的方式

    • 使用 post 、delete、put 、get 使用不同的方法对资源进行操作。

    传统的连接访问方式 :

    • http 😕/127.0.0.1:8080/item?id=1

    RestFul 风格访问方式

    • http 😕/127.0.0.1:8080/item/1

    学习测试

    1、新建一个controller类

    package com.hkp.controller;
    import org.springframework.stereotype.Controller;
    
    @Controller
    public class ControllerDemo {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、在springmvc 中可以使用 @PathVariable 注解,让方法的参数对应绑定到一个 url 上

    package com.hkp.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class ControllerDemo {
        @RequestMapping("/hello/{a}/{b}")
        public String test(Model model, @PathVariable int a,@PathVariable int b){
            model.addAttribute("msg","a--->"+a+",b--->"+b);
            return "test";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3、访问测试

    在这里插入图片描述

    数据处理 :接收参数

    1、请求路径中的参数名称与方法中的参数名称一样,可以直接 接收

    路径 :localhost:8080/mvc?name=“admin”

    在这里插入图片描述

    2、直接接收一个对象

    会根据路径中的参数名称和对象中的属性名称匹配,名称一致则赋值,不一致则使用默认值

    在这里插入图片描述

    乱码问题

    1、在web目录下创建两个jsp页面 test.jsp index.jsp

    test.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
    ${msg}
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    index.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
      
        $Title$
      
      
      $END$
      
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、controller 中方法

    @PostMapping("/t/form")
    public String testForm(String name,Model model){
        model.addAttribute("msg",name);
        return "test";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    解决办法 :

    在web.xml 中配置 springMVC 乱码过滤器

    
    <filter>
        <filter-name>encodingfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    ssm整合 小案例

    数据库

    在这里插入图片描述

    项目环境搭建

    导入依赖

    
    <dependencies>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.25version>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.0version>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
        
        <dependency>
            <groupId>com.mchangegroupId>
            <artifactId>c3p0artifactId>
            <version>0.9.5.5version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.7version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.0.6version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.1.6.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>4.1.6.RELEASEversion>
        dependency>
    dependencies>
    
    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
        resources>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    给项目添加web支持

    在这里插入图片描述

    在这里插入图片描述

    建包结构

    在这里插入图片描述

    在resources 目录下创建 配置文件

    mybatis-config.xml 、db.properties 、spring-mvc.xml 、spring-dao.xml 、spring-service.xml 和 applicationContext.xml 文件

    mybatis-config.xml

    
    DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
    <configuration>
        
        <typeAliases>
            <package name="com.hkp.entity"/>
        typeAliases>
        
        
        <mappers>
            <mapper class="com.hkp.dao.BookMapper"/>
        mappers>
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    db.properties

    driver=com.mysql.cj.jdbc.Driver
    url=jdbc:mysql://127.0.0.1:3306/ssmbuild?useUnicode=true&characterEncoding=utf-8&useSSL=true
    user=root
    password=admin
    
    • 1
    • 2
    • 3
    • 4

    spring-mvc.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        
        <context:component-scan base-package="com.hkp.controller"/>
        
        <mvc:default-servlet-handler/>
        
        <mvc:annotation-driven/>
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    spring-dao.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
    
        
        <context:property-placeholder
                location="classpath:db.properties"/>
    
        
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${driver}">property>
            <property name="jdbcUrl" value="${url}">property>
            <property name="user" value="${user}">property>
            <property name="password" value="${password}">property>
        bean>
    
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
        bean>
    
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
            
            <property name="basePackage" value="com.hkp.dao"/>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    spring-service.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
    
        
        <context:component-scan base-package="com.hkp.service"/>
    
        
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource">property>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    applicationContext.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <import resource="spring-mvc.xml"/>
        <import resource="spring-dao.xml"/>
        <import resource="spring-service.xml"/>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在web.xml 中配置

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
        
        <servlet>
            <servlet-name>springmvcservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:applicationContext.xmlparam-value>
            init-param>
            
            <load-on-startup>1load-on-startup>
        servlet>
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
        
        <filter>
            <filter-name>encodingfilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            <init-param>
                <param-name>encodingparam-name>
                <param-value>utf-8param-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>encodingfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    
    
        <session-config>
            <session-timeout>15session-timeout>
        session-config>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    实现 CRUD

    创建实体类

    package com.hkp.entity;
    public class Books {
        private Integer id;
        private String name;
        private Integer count;
        private String detail;
    
        //有参无参构造、getter和setter方法、toString方法
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    dao 层

    BookMapper.java

    package com.hkp.dao;
    
    import com.hkp.entity.Books;
    
    import java.util.List;
    
    public interface BookMapper {
        /*
        增加一本书
         */
        int addBook(Books book);
    
        /*
        删除一本书
         */
        int deleteBook(int id);
    
        /*
        修改一本书
         */
        int updateBook(Books book);
    
        /*
        根据id查询一本书
         */
        Books selectBookById(int id);
    
        /*
        查询全部书籍
         */
        List<Books> selectAll();
    
        /*
        根据书名模糊查询一本书
         */
        Books selectBook(String name);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    BookMapper.xml

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.hkp.dao.BookMapper">
        <insert id="addBook" parameterType="books">
            INSERT INTO `ssmbuild`.`books`(`name`, `count`, `detail`)
            VALUES (#{name},#{count},#{detail});
        insert>
    
        <delete id="deleteBook" parameterType="int">
            delete from books where id = #{id};
        delete>
    
        <update id="updateBook" parameterType="books">
            UPDATE `ssmbuild`.`books`
            SET `name` = #{name}, `count` = #{count}, `detail` = #{detail}
            WHERE `id` = #{id};
        update>
        
        <select id="selectBookById" parameterType="int" resultType="books">
            select * from books where id = #{id}
        select>
    
        <select id="selectBook" parameterType="string" resultType="books">
            select * from books where `name` like concat('%',#{name},'%')
        select>
        
        <select id="selectAll" resultType="books">
            select * from books
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    service 层

    接口

    package com.hkp.service;
    
    import com.hkp.entity.Books;
    
    import java.util.List;
    
    public interface BookService {
        /*
        增加一本书
         */
        int addBook(Books book);
    
        /*
        删除一本书
         */
        int deleteBook(int id);
    
        /*
        修改一本书
         */
        int updateBook(Books book);
    
        /*
        根据id查询一本书
         */
        Books selectBookById(int id);
    
        /*
        查询全部书籍
         */
        List<Books> selectAll();
    
        /*
        根据书名模糊查询一本书
         */
        Books selectBook(String name);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    实现类

    package com.hkp.service;
    
    import com.hkp.dao.BookMapper;
    import com.hkp.entity.Books;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class BookServiceImpl implements
            BookService{
    
        //业务层调用 dao 层
        @Autowired
        private BookMapper bookMapper;
    
        public int addBook(Books book) {
            return bookMapper.addBook(book);
        }
    
        public int deleteBook(int id) {
            return bookMapper.deleteBook(id);
        }
    
        public int updateBook(Books book) {
            return bookMapper.updateBook(book);
        }
    
        public Books selectBookById(int id) {
            return bookMapper.selectBookById(id);
        }
    
        public List<Books> selectAll() {
            return bookMapper.selectAll();
        }
    
        public Books selectBook(String name) {
            return bookMapper.selectBook(name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    controller 层

    package com.hkp.controller;
    
    import com.hkp.entity.Books;
    import com.hkp.service.BookService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Controller
    public class BooksController {
    
        //controller 调用 service层
        @Autowired
        private BookService bookService;
    
    
        /**
         * 查询全部书籍
         * @param model
         * @return
         */
        @RequestMapping("/allBook")
        public String getAll(Model model){
            List<Books> books = bookService.selectAll();
            model.addAttribute("books",books);
            return "books";
        }
    
        /**
         * 跳转到新增书籍页面
         * @return
         */
        @RequestMapping("/toAddBook")
        public String toAddBook(){
            return "addbook";
        }
    
        /**
         * 增加书籍
         * @param book
         * @return
         */
        @RequestMapping("/addBook")
        public String addBook(Books book){
            bookService.addBook(book);
            return "redirect:/allBook";
        }
    
        /**
         * 跳转到修改书籍页面
         * @param model
         * @return
         */
        @RequestMapping("/toUpdateBook")
        public String toUpdateBook(Model model,int id) {
            Books books = bookService.selectBookById(id);
            model.addAttribute("books",books);
            return "updatebook";
        }
    
        /**
         * 修改书籍
         * @param book
         * @return
         */
        @RequestMapping("/updateBook")
        public String updateBook(Books book){
            bookService.updateBook(book);
            return "redirect:/allBook";
        }
    
    
        /**
         * 删除书籍
         * @param id
         * @return
         */
        @RequestMapping("/deleteBook/{bookId}")
        public String deleteBook(@PathVariable("bookId") int id){
            bookService.deleteBook(id);
            return "redirect:/allBook";
        }
    
    
        /**
         * 根据书名模糊查询书籍
         * @param name
         * @param model
         * @return
         */
        @RequestMapping("/selectOne")
        public String selectOne(String name,Model model){
            Books book = bookService.selectBook(name);
            List<Books> books = new ArrayList<Books>();
            books.add(book);
            model.addAttribute("books",books);
            return "books";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104

    jsp

    index.jsp 首页

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
      Title
    
    
    

    进入

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    books.jsp 展示全部书籍

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
        
    
    
        
    编号 名称 数量 详情
    ${book.id} ${book.name} ${book.count} ${book.detail} 修改  |  删除
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    addbook.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    updatebook.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
  • 相关阅读:
    算法通关村-----海量数据的处理方法
    致远OA(A8) REST接口 python版
    基于C#+unity的2D跑酷闯关对战冒险游戏设计 课程报告+答辩PPT+源码
    机械原理习题-(附答案)
    关于SAP APO RPMCALL 指定生产订单的BOM更新
    offline RL · PbRL | Preference Transformer:反正感觉 transformer 很强大
    大咖说*计算讲谈社|如何提出关键问题?
    MySQL事务
    Python3 API 的封装及调用
    【DataRoom】- 基于VUE的开源的大屏可视化设计器
  • 原文地址:https://blog.csdn.net/weixin_46047612/article/details/126134176