SSM常用注解

1.标识启动类@SpringBootApplication

这是springboot提供的注解

SpringBoot使用了全新的运行方式,两句话,直接执行main方法,默认启动tomcat中间件,端口为8080。

注意:类必须放在其他类的父级目录,它底层使用了包扫描机制,扫描的要求只扫描本类所在的目录极其子目录。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication  //标识为springboot项目
public class RunApp {
    public static void main(String[] args) {
        //全新的启动方式,执行run方法会自动启动集成的tomcat中间件
        SpringApplication.run(RunApp.class, args);
    }
}

 2.controller层的常用注解

##2.1@RestController  用于接受浏览器的请求 并返回json数据,只能用于类上

查看注解源码,该注解由@Controller @ResponseBody注解构成

 @Controller 由于接受浏览器请求

@ResponseBody 将对象转换成json数据返回(前后端的数据传输采用json形式)

@RestController
public class HelloController {

}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    @AliasFor(
        annotation = Controller.class
    )
    String value() default "";
}

##2.2@RequestMapping 匹配请求路径,可用于类和方法上

@RequestMapping("hello")//只匹配一个路径
@RequestMapping({"hello","hello/hi"}) 匹配两种路径/hi或者/hello/hi

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
//@RequestMapping("hello")
@RequestMapping({"hello","hello/hi"})

public class HelloController {
    @RequestMapping("/show")
    public String show(){
        return "hello你好";
    }
}

注解源码

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";

    @AliasFor("path")
    String[] value() default {};

    @AliasFor("value")
    String[] path() default {};

    RequestMethod[] method() default {};

    String[] params() default {};

    String[] headers() default {};

    String[] consumes() default {};

    String[] produces() default {};
}

##2.3@RequestParam

如果页面的名称和后台形参的名称不一致,可以使用@RequestParam(“页面名称”),就必须指定值.

@RestController
@RequestMapping("hello")

public class HelloController {
    @RequestMapping("/show")
    //前端请求使用的参数名为id,后台使用id2作为形参
    public String show(@RequestParam("id") Integer id2){   
        return "hello你好"+id2;
    }
}

##@DateTimeFormat(pattern="yyyy-MM-dd")  

public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
    private String[] hobby;
    private String edu;
    @DateTimeFormat(pattern="yyyy-MM-dd")
    //网页上的日期是string,注解用来转换格式,不然400错误
    private Date intime;
}


原文链接:https://blog.csdn.net/u012932876/article/details/117658765

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值