【Spring】整合【SpringMVC】

导入依赖


      org.springframework
      spring-core
      ${spring.version}


      org.springframework
      spring-context
      ${spring.version}
    
    
      org.springframework
      spring-web
      ${spring.version}
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
 

      javax.servlet
      javax.servlet-api
      4.0.1
      provided



      com.fasterxml.jackson.core
      jackson-databind
      2.13.4
 

配置SpringMVC



  
  
    org.springframework.web.context.ContextLoaderListener
  
  
    contextConfigLocation
    classpath:spring.xml
  
  
  
  
  
    dispatcher
    org.springframework.web.servlet.DispatcherServlet
      
      contextConfigLocation
      classpath:spring-mvc.xml
    
  
  
    dispatcher
    /
  
  
  
    characterFilter
    org.springframework.web.filter.CharacterEncodingFilter
  
  
    characterFilter
    /*
  

【注意】配置DispatcherServlet时,如果不指定配置文件,SpringMVC会在WEB-INF目录下寻找以servlet-name中配置的名称加-servlet寻找配置文件,例如:servlet-name中配置了名称为dispatcher,SpringMVC会寻找名称为dispatcher-servlet.xml的配置文件,通常情况下,需要指定固定位置的固定配置文件,如上例中所示。

SpringMVC.XML



    
    
    
        
        
    
    
    
        
            
                
                    
                        text/plain;charset=UTF-8
                        text/html;charset=utf-8
                        application/json
                    
                
            
        
    
    
    

SpringMVC常用注解

@Controller

该注解标注在控制器上,表明该类可以接收请求,具体的请求由类中的方法处理。

@RequestMapping

package org.springframework.web.bind.annotation;
​
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
​
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";
    //该方法或者该类的请求路径
    @AliasFor("path")
    String[] value() default {};
    //等价于value
    @AliasFor("value")
    String[] path() default {};
    //设置接收的请求方式,如果不配置该属性,则可以接收所有类型的请求, 如果配置,则只能接收指定类型的请求
    RequestMethod[] method() default {};
    //必须携带固定的参数才可以处理请求例如:name=admin
    String[] params() default {};
    //响应头中必须包含固定参数才可以处理请求
    String[] headers() default {};
    //请求数据类型是指定类型的请求可以处理
    String[] consumes() default {};
    //
    String[] produces() default {};
}

通常,可以使用该注解的替代注解,常用的有:

  • @GetMapping

  • @PostMapping

属性 类型 是否必要 说明
value String[] 用于将指定请求的地址映射到方法
name String 给映射地址指定一个别名
method RequestMethod[] 映射指定请求的方法,包括GET、POST、HEAD、OPTIONS、PUT、PATCH、DELETE、TRACE
consumes String[] 指定处理请求的提交内容(Content-Type),例如:application/json
produces String[] 指定返回的内容类型,返回的内容类型必须是request请求头中所包含的类型
params String[] 指定request中必须包含某些参数值时,才让该方法处理请求
headers String[] 指定request中必须包含某些指定的header值,才能让该方法处理请求
path String[] 用于将指定请求的地址映射到方法

@RequestMapping 不但支持标准的 URL,还支持 Ant 风格(?、*和**字符)的和带{xxx}占位符的URL。以下 URL 都是合法的。


  • /user/*/createUser∶ 匹配/user/aa/createUser、/user/bbb/createUser等 URL。

  • /user/**/createUser∶匹配/user/createUser、/user/aaa/bbb/createUser 等URL。

  • /user/createUser??∶匹配/user/createUseraa、/user/createUserbb等 URL。

  • /user/{userld}∶匹配 user/123、user/456 等URL。


  • /user/**/{userId}∶匹配 user/aa/bbb/123、user/aaa/456等 URL。


  • company/{companyld}/user/{userId}/detail∶匹配 company/123/user/456/detail 等
URL。

@RequestParam

该注解的作用是将请求参数和Controller方法参数进行绑定

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
​
   /**
    * 将前端请求参数的名称(即请求参数的name或者key)与方法参数绑定
    * Alias for {@link #name}.
    */
   @AliasFor("name")
   String value() default "";
   
   /**
    * The name of the request parameter to bind to.
    * @since 4.2
    */
   @AliasFor("value")
   String name() default "";
​
   /**
    * Whether the parameter is required.
    * 

Defaults to {@code true}, leading to an exception being thrown    * if the parameter is missing in the request. Switch this to    * {@code false} if you prefer a {@code null} value if the parameter is    * not present in the request.    *

Alternatively, provide a {@link #defaultValue}, which implicitly    * sets this flag to {@code false}.    * 如果不传该参数则抛出异常    */   boolean required() default true; ​   /**    * The default value to use as a fallback when the request parameter is    * not provided or has an empty value.    *

Supplying a default value implicitly sets {@link #required} to    * {@code false}.    */   String defaultValue() default ValueConstants.DEFAULT_NONE; ​ }

@PathVariable

该注解的作用时将请求路径中的参数绑定到Controller方法参数

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {
​
   /**
    * 请求路径中参数的名称
    * Alias for {@link #name}.
    */
   @AliasFor("name")
   String value() default "";
​
   /**
    * The name of the path variable to bind to.
    * @since 4.3.3
    * 作用同value
    */
   @AliasFor("value")
   String name() default "";
​
   /**
    * Whether the path variable is required.
    * 

Defaults to {@code true}, leading to an exception being thrown if the path    * variable is missing in the incoming request. Switch this to {@code false} if    * you prefer a {@code null} or Java 8 {@code java.util.Optional} in this case.    * e.g. on a {@code ModelAttribute} method which serves for different requests.    * @since 4.3.3    * 是否必须传递该参数,如果为null则抛出异常    */   boolean required() default true; ​ }

@ResonseBody

将方法的返回值转成Json格式的字符串或者Json对象

@RequestBody

将前端发送的JSON格式的数据绑定到方法参数的对象中

@SessionAttribute

获取使用value或者name指定名称的在Session中保存的数据

@CookieValue

获取使用value或者name指定名称的在Cookie中保存的数据

Spring整合MyBatis

导入依赖



  org.mybatis
  mybatis-spring
  2.0.7



  org.mybatis
  mybatis
  3.5.9



  mysql
  mysql-connector-java
  8.0.28



  com.alibaba
  druid
  1.2.11


      org.springframework
      spring-jdbc
      ${spring.version}

配置Mybatis



    
    
    
    
    
    
        
        
        
​
​
        
        
        
        
​
        
        
​
        
        
​
        
        
        
​
        
        
        
        
​
        
        
​
        
        
    
    
    
        
        
        
        
        
        
    
    
    
        
    

你可能感兴趣的:(java,spring)