• SpringMVC-整合详解


    SpringMVC-整合详解

    MVC
    • 什么是MVC?

    它是一种开发模式,它是模型视图控制器的简称。所有的web应用都是基于MVC开发的

    • M: 模型层,它是模型视图控制器的简称。所有的web应用都是基于MVC开发

    • V: 视图层,html、javascript、vue等都是视图层,用来显示数据

    • C: 控制器,它是用来接受客户端的请求,并返回响应到客户端的组件,Servlet就是组件

    SpringMVC 框架的有点
    • 轻量级,基于MVC的框架
    • 易于上手,容易理解,功能强大
    • 它具备IoCAOP
    • 完全基于注解开发
    基于注解的 SpringMVC 框架开发的步骤
    1. 新建maven项目,选择模板webapp
    创建项目
    1. 修改目录,添加缺失目录,并修改目录属性
    添加java目录...webapp目录配置
    1. 修改pom.xml,添加SpringMVC、Servlet的依赖
    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0modelVersion>
        <groupId>org.examplegroupId>
    
        <artifactId>springmvc-001artifactId>
        <packaging>warpackaging>
        <version>1.0-SNAPSHOTversion>
    
        
        <properties>
            <maven.compiler.source>17maven.compiler.source>
            <maven.compiler.target>17maven.compiler.target>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        properties>
    
        <dependencies>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.13.2version>
                <scope>testscope>
            dependency>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>5.3.23version>
            dependency>
            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>4.0.1version>
                <scope>providedscope>
            dependency>
    
        dependencies>
        <build>
            
            <finalName>springmvc-001finalName>
            
            <resources>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.xmlinclude>
                        <include>**/*.propertiesinclude>
                    includes>
                resource>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*.xmlinclude>
                        <include>**/*.propertiesinclude>
                    includes>
                resource>
            resources>
        build>
    project>
    
    
    • 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
    1. 添加springmvc.xml配置文件,指定包扫描,添加视图解析器
    
    
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://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.example"/>
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            
            <property name="prefix" value="/admin/"/>
            
            <property name="suffix" value=".jsp"/>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 删除wen.xml文件(版本过低),新建web.xml
    web.xml
    1. web.xml文件中注册springmvc框架(所有的web请求都是基于servlet的)
    
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             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:springmvc.xmlparam-value>
            init-param>
            <load-on-startup>1load-on-startup>
        servlet>
        
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            
            <url-pattern>*.actionurl-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
    1. 删除index.jsp,并新建,发送请求给服务器
    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
    访问 main 
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. webapp目录下新建admin目录(根据配置文件中的需要添加),在admin/admin.jsp,删除index.jsp,并新建
    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Admin-Page
    
    
    

    show admin.............................

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 开发控制器(Servlet),它是一个普通的类
    package com.example.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class MainController {
        @RequestMapping("/main.action")
        public String main() {
            return "admin";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. Tomcat启动测试
    测试请求
    • 注意点

      在这些步骤的前提下,如果访问出现404,那么进行IDEA缓存清除,重新构建,再次启动测试

    • 项目目录结构

    当前目录结构
    五种数据提交方式的优化
    • 单个提交数据
    <%-- 单个提交数据 --%>
    <form action="/user.do" method="post">
        <input type="text" name="myname">
        <input type="number" name="age">
        <input type="submit" value="提交">
    form>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    @RequestMapping("/user.do")
    public String user(String myname, int age) {
        System.out.println(myname + " " + (age + 10));
        return "admin";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 对象封装提交数据
    // 实体类
    package com.example.entity;
    
    import lombok.Data;
    
    @Data
    public class User {
        private String username;
        private Integer age;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    @RequestMapping("/pojo.do")
    public String user(User user) {
        System.out.println(user.getUsername() + " " + (user.getAge() + 10));
        return "admin";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    <%-- 2. 对象封装提交 --%>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 动态占位符
    占位符
    
    • 1
    @RequestMapping("/test/{username}/{age}.do")
    public String TestDo(@PathVariable("username") String username, @PathVariable("age") int age) {
        System.out.println(username + (age));
        return "admin";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 映射名称不一致提交
    • 1
    • 2
    • 3
    • 4
    • 5
    // 通过 @RequestParam 将 username 注给 uname
    @RequestMapping("/nomapping.do")
    public String NoMapping(
        @RequestParam("username")
        String uname,
        @RequestParam("age")
        int ahe
    ) {
        System.out.println(uname + " " + (ahe));
        return "admin";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    中文编码配置
    • web.xml中配置过滤器,建议写在顶部
    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        
        <init-param>
            <param-name>forceRequestEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
        
        <init-param>
            <param-name>forceResponseEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    
    • 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
    方法的返回值
    1. String: 客户端资源的地址,自动拼接前缀和后缀,还可以屏蔽自动拼接字符串,可以指定返回的路径
    2. Object: 返回json格式的对象。自动将对象或集合转为json,使用jackson工具进行转换,必须要添加jackson依赖。一般用于ajax请求
    3. void: 无返回值,一般用于ajax请求
    4. 基本数据类型,用于ajax请求
    5. ModelAndView: 返回数据和视图对象,现在用的很少
    完成 Ajax 请求访问服务器,返回学生集合
    1. 添加jackson依赖
    <dependency>
        <groupId>com.fasterxml.jackson.coregroupId>
        <artifactId>jackson-databindartifactId>
        <version>2.13.4.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. webapp目录下新建js目录,添加axios函数库
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js">script>
    
    • 1
    1. index.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
    1. action上添加注解@ResponseBody,用来处理ajax请求
    package com.example.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    @Controller
    public class UserController {
        @ResponseBody
        @RequestMapping("/list.do")
        public List<String> list() {
            List<String> list = new ArrayList<>();
            Collections.addAll(list, "aa", "bb", "cc", "dd");
            return list;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    1. springmvc.xml文件中添加注解驱动,用它来解析@RespinseBody注解
    
    <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" xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://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 https://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
        <context:component-scan base-package="com.example"/>
        
    
        
        <mvc:annotation-driven/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. 访问测试
    控制台输出
    日期处理
    • 单个日期处理

    要使用注解@DateTimeFormat,此注解必须搭配springmvc.xml中的

    • 1
    • 2
    • 3
    • 4
    package com.example.controller;
    
    import org.springframework.format.annotation.DateTimeFormat;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    @Controller
    public class DateController {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        @RequestMapping("/mydate.do")
        public String myDate(
                @DateTimeFormat(pattern = "yyyy-MM-dd")
                Date mydate) {
            System.out.println(sdf.format(mydate));
            return "admin";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 类中全局日期处理
    package com.example.controller;
    
    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.annotation.InitBinder;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    @Controller
    public class DateController {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
        // 注册一个全局的日期处理注解
        @InitBinder
        public void initBinder(WebDataBinder dataBinder) {
            dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
        }
    
        @RequestMapping("/mydate.do")
        public String myDate(Date mydate) {
            System.out.println(sdf.format(mydate));
            return "admin";
        }
    }
    
    
    • 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
    • 如果一个类的成员属性是日期格式,可以在类的成员 setXX添加注解 @DateTimeFormat(pattern = "yyyy-MM-dd")

    • 如果是json类型,需要在类中的成员变量getXXX方法上添加注解@JsonFormat(pattern="yyyy-MM-dd")

    WEB-INF 资源
    • 此目录下的动态资源,不可以直接访问,只能通过请求转发的方式进行访问

    • 修改springmvc.xml配置

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/"/>
      <property name="suffix" value=".jsp"/>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    WEB-INF/main
    
    • 1
    package com.example.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class WebInfController {
      @RequestMapping("/showMain.do")
      public String showMain() {
          return "main";
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    拦截器
    • 什么是拦截器

    针对请求和响应进行的额外处理。在请求和响应的过程中添加预处理,后处理和最终处理

    • 拦截器的执行时机

    • preHandle: 在请求被处理之前进行操作

    • postHandle: 在请求被处理之后,但结果还没有渲染前进行操作,可以改变响应结果

    • afterCompletion: 所有的请求响应结束后执行善后工作,清理对象,关闭资源

    • 拦截器实现的两种方式

    • 集成HandlerIntercepterAdpater的父类

    • 实现HandlerInterceptor接口【推荐使用实现接口的方式】

    • 登录拦截器实现的步骤

    1. 改造登录方法,在session中存储用户信息,用于进行权限验证

      package com.example.controller;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
      
      import javax.servlet.http.HttpServletRequest;
      
      @Controller
      public class WebInfController {
          @RequestMapping("/showLogin")
          public String showLogin() {
              return "login";
          }
          @RequestMapping("/main.do")
          public String showMain(String username, String password, HttpServletRequest request) {
              if ("coder-itl".equalsIgnoreCase(username) && "coder-itl".equalsIgnoreCase(password)) {
                  request.getSession().setAttribute("username", username);
                  return "main";
              } else {
                  request.setAttribute("msg", "用户名和密码错误");
                  return "login";
              }
          }
      
      }
      
      • 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
    2. 开发拦截器的功能,实现HandlerInterceptor接口,重写preHandle方法

      package com.example.intercepter;
      
      import org.springframework.web.servlet.HandlerInterceptor;
      
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      // TODO: 无法实现跳转
      public class LoginInterceptor implements HandlerInterceptor {
          @Override
          public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
              if (request.getSession().getAttribute("username") == null) {
                  // 没有登陆
                  request.setAttribute("msg", "您还未登录,请先登录!");
                  request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
                  return false;
              }
              // 登录成功放行
              return true;
          }
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
    3. springmvc.xml文件中注册拦截器

      
      <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" xmlns="http://www.springframework.org/schema/beans"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://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 https://www.springframework.org/schema/mvc/spring-mvc.xsd">
          
          <context:component-scan base-package="com.example"/>
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/jsp/"/>
              <property name="suffix" value=".jsp"/>
          bean>
          
          <mvc:annotation-driven/>
          <mvc:interceptors>
              <mvc:interceptor>
                  
                  <mvc:mapping path="/**"/>
                  
                  <mvc:exclude-mapping path="/showLogin"/>
                  <mvc:exclude-mapping path="/main.do"/>
                  
                  <bean class="com.example.intercepter.LoginInterceptor"/>
              mvc:interceptor>
          mvc:interceptors>
      
      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
    SSM 整合
    1. 创建项目,添加依赖
    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0modelVersion>
        <groupId>org.examplegroupId>
        <artifactId>spring-ssmartifactId>
        <packaging>warpackaging>
        <version>1.0-SNAPSHOTversion>
    
        
        <properties>
            
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <maven.compiler.encoding>UTF-8maven.compiler.encoding>
            <java.version>1.8java.version>
            <maven.compiler.source>1.8maven.compiler.source>
            <maven.compiler.target>1.8maven.compiler.target>
    
            
            <junit.version>4.12junit.version>
            
            <spring.version>5.1.2.RELEASEspring.version>
            
            <mybatis.version>3.2.8mybatis.version>
            
            <mybatis.spring.version>1.2.2mybatis.spring.version>
            
            <mybatis.paginator.version>1.2.15mybatis.paginator.version>
            
            <mysql.version>8.0.22mysql.version>
            
            <slf4j.version>1.6.4slf4j.version>
            
            <druid.version>1.0.9druid.version>
            
            <pagehelper.version>5.1.2pagehelper.version>
            
            <jstl.version>1.2jstl.version>
            
            <servlet-api.version>3.0.1servlet-api.version>
            
            <jsp-api.version>2.0jsp-api.version>
            
            <jackson.version>2.9.6jackson.version>
            
            <loback.version>1.2.11loback.version>
            
            <lombok.version>1.18.24lombok.version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.aspectjgroupId>
                <artifactId>aspectjweaverartifactId>
                <version>1.6.11version>
            dependency>
            <dependency>
                <groupId>org.jsongroupId>
                <artifactId>jsonartifactId>
                <version>20140107version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-beansartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aspectsartifactId>
                <version>${spring.version}version>
            dependency><project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0modelVersion>
        <groupId>org.examplegroupId>
        <artifactId>spring-ssmartifactId>
        <packaging>warpackaging>
        <version>1.0-SNAPSHOTversion>
    
        
        <properties>
            
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <maven.compiler.encoding>UTF-8maven.compiler.encoding>
            <java.version>1.8java.version>
            <maven.compiler.source>1.8maven.compiler.source>
            <maven.compiler.target>1.8maven.compiler.target>
    
            
            <junit.version>4.12junit.version>
            
            <spring.version>5.1.2.RELEASEspring.version>
            
            <mybatis.version>3.2.8mybatis.version>
            
            <mybatis.spring.version>1.2.2mybatis.spring.version>
            
            <mybatis.paginator.version>1.2.15mybatis.paginator.version>
            
            <mysql.version>8.0.22mysql.version>
            
            <slf4j.version>1.6.4slf4j.version>
            
            <druid.version>1.0.9druid.version>
            
            <pagehelper.version>5.1.2pagehelper.version>
            
            <jstl.version>1.2jstl.version>
            
            <servlet-api.version>3.0.1servlet-api.version>
            
            <jsp-api.version>2.0jsp-api.version>
            
            <jackson.version>2.9.6jackson.version>
            
            <loback.version>1.2.11loback.version>
            
            <lombok.version>1.18.24lombok.version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.aspectjgroupId>
                <artifactId>aspectjweaverartifactId>
                <version>1.6.11version>
            dependency>
            <dependency>
                <groupId>org.jsongroupId>
                <artifactId>jsonartifactId>
                <version>20140107version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-beansartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aspectsartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jmsartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-context-supportartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>${spring.version}version>
            dependency>
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>${mybatis.version}version>
            dependency>
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatis-springartifactId>
                <version>${mybatis.spring.version}version>
            dependency>
            <dependency>
                <groupId>com.github.miemiedevgroupId>
                <artifactId>mybatis-paginatorartifactId>
                <version>${mybatis.paginator.version}version>
            dependency>
            <dependency>
                <groupId>com.github.pagehelpergroupId>
                <artifactId>pagehelperartifactId>
                <version>${pagehelper.version}version>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql.version}version>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>${druid.version}version>
            dependency>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.13.2version>
                <scope>testscope>
            dependency>
            
            <dependency>
                <groupId>jstlgroupId>
                <artifactId>jstlartifactId>
                <version>${jstl.version}version>
            dependency>
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>3.0.1version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>jsp-apiartifactId>
                <scope>providedscope>
                <version>${jsp-api.version}version>
            dependency>
            
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>${jackson.version}version>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>fastjsonartifactId>
                <version>1.2.28version>
            dependency>
            
            <dependency>
                <groupId>commons-iogroupId>
                <artifactId>commons-ioartifactId>
                <version>2.4version>
            dependency>
            <dependency>
                <groupId>commons-fileuploadgroupId>
                <artifactId>commons-fileuploadartifactId>
                <version>1.3.1version>
            dependency>
            
            <dependency>
                <groupId>ch.qos.logbackgroupId>
                <artifactId>logback-classicartifactId>
                <version>1.2.11version>
            dependency>
            <dependency>
                <groupId>ch.qos.logbackgroupId>
                <artifactId>logback-coreartifactId>
                <version>1.2.11version>
            dependency>
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-apiartifactId>
                <version>1.7.25version>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-txartifactId>
                <version>5.3.23version>
            dependency>
    
        dependencies>
    
        
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                plugin>
            plugins>
            
            <resources>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>falsefiltering>
                resource>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>falsefiltering>
                resource>
            resources>
        build>
    project>
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jmsartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-context-supportartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>${spring.version}version>
            dependency>
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>${mybatis.version}version>
            dependency>
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatis-springartifactId>
                <version>${mybatis.spring.version}version>
            dependency>
            <dependency>
                <groupId>com.github.miemiedevgroupId>
                <artifactId>mybatis-paginatorartifactId>
                <version>${mybatis.paginator.version}version>
            dependency>
            <dependency>
                <groupId>com.github.pagehelpergroupId>
                <artifactId>pagehelperartifactId>
                <version>${pagehelper.version}version>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql.version}version>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>${druid.version}version>
            dependency>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.13.2version>
                <scope>testscope>
            dependency>
            
            <dependency>
                <groupId>jstlgroupId>
                <artifactId>jstlartifactId>
                <version>${jstl.version}version>
            dependency>
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>3.0.1version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>jsp-apiartifactId>
                <scope>providedscope>
                <version>${jsp-api.version}version>
            dependency>
            
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>${jackson.version}version>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>fastjsonartifactId>
                <version>1.2.28version>
            dependency>
            
            <dependency>
                <groupId>commons-iogroupId>
                <artifactId>commons-ioartifactId>
                <version>2.4version>
            dependency>
            <dependency>
                <groupId>commons-fileuploadgroupId>
                <artifactId>commons-fileuploadartifactId>
                <version>1.3.1version>
            dependency>
    
            <dependency>
                <groupId>ch.qos.logbackgroupId>
                <artifactId>logback-classicartifactId>
                <version>${loback.version}version>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-txartifactId>
                <version>5.3.23version>
            dependency>
        dependencies>
    
        
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                plugin>
            plugins>
            
            <resources>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>falsefiltering>
                resource>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>falsefiltering>
                resource>
            resources>
        build>
    project>
    
    
    • 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
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    1. 添加缺失目录java,resources

    2. 更换高版本的web.xml

    3. resources目录下创建jsbc.properties

    # 5.7
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssmuser?useUnicode=true&characterEncoding=utf8
    jdbc.username=root
    jdbc.password=root
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # 8.0
    jdbc.driverClassName=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssmuser?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf-8&autoReconnect=true
    jdbc.username=root
    jdbc.password=root
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 创建mybatis-config.xml
    
    DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "https://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        
        <settings>
            <setting name="logImpl" value="SLF4J"/>
        settings>
        <typeAliases>
            <package name="com.example.entity"/>
        typeAliases>
        <mappers>
            <package name="com.example.mapper"/>
        mappers>
    configuration>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1. 添加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
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
        
        
        
        
        
       
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
        
        <context:component-scan base-package="com.example"/>
        
        <context:property-placeholder location="classpath:jdbc.properties"/>
        
        <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        bean>
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            
            <property name="dataSource" ref="myDataSource"/>
            
            <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.example.mapper"/>
        bean>
    
        
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="myDataSource"/>
        bean>
        
        <tx:advice id="myAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="*select*" read-only="true"/>
                <tx:method name="*find*" read-only="true"/>
                <tx:method name="*get*" read-only="true"/>
                <tx:method name="*search*" read-only="true"/>
                <tx:method name="*insert*" propagation="REQUIRED"/>
                <tx:method name="*save*" propagation="REQUIRED"/>
                <tx:method name="*add*" propagation="REQUIRED"/>
                <tx:method name="*delete*" propagation="REQUIRED"/>
                <tx:method name="*remove*" propagation="REQUIRED"/>
                <tx:method name="*clear*" propagation="REQUIRED"/>
                <tx:method name="*update*" propagation="REQUIRED"/>
                <tx:method name="*modify*" propagation="REQUIRED"/>
                <tx:method name="*change*" propagation="REQUIRED"/>
                <tx:method name="*set*" propagation="REQUIRED"/>
                <tx:method name="*" propagation="SUPPORTS"/>
            tx:attributes>
        tx:advice>
        
        <aop:config>
            
            <aop:pointcut id="myPointCut" expression="execution(* com.example.service.impl.*.*(..))"/>
            <aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut"/>
        aop:config>
    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
    • 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
    1. 配置spring-mvc.xml
    
    <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" xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://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.example"/>
        
        
        <mvc:annotation-driven/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 配置web.xml
    
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        
        <filter>
            <filter-name>characterEncodingFilterfilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            
            <init-param>
                <param-name>encodingparam-name>
                <param-value>UTF-8param-value>
            init-param>
            <init-param>
                <param-name>forceRequestEncodingparam-name>
                <param-value>trueparam-value>
            init-param>
            <init-param>
                <param-name>forceResponseEncodingparam-name>
                <param-value>trueparam-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>characterEncodingFilterfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
        
        <servlet>
            <servlet-name>dispatcherServletservlet-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>dispatcherServletservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
        
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
        listener>
        <context-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath*:spring-service.xmlparam-value>
        context-param>
    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
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    1. 添加实体类
    package com.example.entity;
    
    import lombok.Data;
    
    import java.io.Serializable;
    
    @Data
    public class User implements Serializable {
        private String userId;
        private String cardType;
        private String cardNo;
        private String userName;
        private String userSex;
        private String userAge;
        private String userRole;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1. 添加service,impl
    // service
    package com.example.service;
    
    import com.example.entity.User;
    
    import java.util.List;
    
    public interface UserService {
        List<User> selectUserPage(String userName, String userSex, int startRow);
        int createUser(User user);
        int deleteUserById(String userId);
        int getRowCount(String userName, String userSex); 
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    // serviceImpl
    package com.example.service.impl;
    
    import com.example.entity.User;
    import com.example.mapper.UserMapper;
    import com.example.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service("userService")
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserMapper userMapper;
    
        @Override
        public List<User> selectUserPage(String userName, String userSex, int startRow) {
            return userMapper.selectUserPage(userName, userSex, startRow);
        }
    
        @Override
        public int createUser(User user) {
            return userMapper.createUser(user);
        }
    
        @Override
        public int deleteUserById(String userId) {
            return userMapper.deleteUserById(userId);
        }
    
        @Override
        public int getRowCount(String userName, String userSex) {
            return userMapper.getRowCount(userName, userSex);
        }
    }
    
    
    • 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
    1. 创建mapper映射文件
    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.mapper.UserMapper">
    
        <resultMap id="userMap" type="user">
            <id property="userId" column="user_id"/>
            <result property="cardType" column="card_type"/>
            <result property="cardNo" column="card_no"/>
            <result property="userName" column="user_name"/>
            <result property="userSex" column="user_sex"/>
            <result property="userAge" column="user_age"/>
            <result property="userRole" column="user_role"/>
        resultMap>
        
        <sql id="allColumns">
            user_id
            ,
            card_type,
            card_no,
            user_name,
            user_sex,
            user_age,
            user_role
        sql>
        
        <select id="selectUserPage" resultMap="userMap">
            select
            <include refid="allColumns"/>
            from user
            <where>
                <if test="userName!=null and userName!=''">
                    and user_name like concat('%',#{userName},'%')
                if>
                <if test="userSex!=null and userSex!=''">
                    and user_sex=#{userSex}
                if>
            where>
            limit #{startRow},5
        select>
        
        <insert id="createUser" parameterType="user">
            insert into user
            values (#{userId}, #{cardType}, #{cardNo}, #{userSex}, #{userAge}, #{userRole})
        insert>
        
        <delete id="deleteUserById" parameterType="string">
            delete
            from user
            where userId = #{userId}
        delete>
        
        <select id="getRowCount" resultType="integer">
            select count(*)
            from user
            <where>
                <if test="userName!=null and userName!=''">
                    and user_name like concat('%',#{userName},'%')
                if>
                <if test="userSex!=null and userSex!=''">
                    and user_sex=#{userSex}
                if>
            where>
        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
    • 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
    1. 测试
    // spring-service.xml 是 spring 管理 service,dao 相关的对象
    public class UserServiceTest {
        @Test
        public void selectUserPageTest() {
            ApplicationContext ac = new ClassPathXmlApplicationContext("spring-service.xml");
            UserService userService = ac.getBean("userService", UserService.class);
            int rowCount = userService.getRowCount(null, "男");
            System.out.println(rowCount);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    测试整合是否有效
  • 相关阅读:
    计算机网络运输层
    【PAT甲级 - C++题解】1149 Dangerous Goods Packaging
    【PHP快速入门】详细笔记---精简版
    链表、栈、队列
    互融云人行二代征信系统对接服务
    空调外机清洁机器人设计
    SynthText流程解读 - 不看代码不知道的那些事
    有没有免费的云渲染平台?哪家云渲染平台收费更合理?
    免费开源库CDN加速对比
    HR坦言:不敢招聘5年开发经验的程序员?怎么回事?
  • 原文地址:https://blog.csdn.net/weixin_43340420/article/details/127940116