今天小玉去面试被面试官反复鞭尸了 T . T .......回来赶紧写个博客,今天是2023_10_24日,祝码友们码到成功,offer拿到手软!!!!
目录
1.什么是Spring Boot MVC?
(之前学的servlet没有白学,在servlet的基础上,用更加方便的框架实现servlet的功能)
1.1什么是MVC?
2.SpringBoot MVC如何建立连接?
1.首先我们在新建项目的时候就可以导入MVC框架,只需要选中web中的spring web即可.spring web就是SpringMVC.
2.新建一个TestController类,在里面实现功能:
package com.example.demo;
import org.springframework.web.bind.annotation.*;
/**
* Created with IntelliJ IDEA.
* Description:
* User: yyx
* Date: 2023-10-24
* Time: 16:48
*/
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "/sayhi")
public String SayHi(){
return "hello Spring Boot MVC!";
}
}
a) 其中主要用到了一个注解: @RequestMapping ("/路由") 这个注解可以用来修饰类或者方法,都可以的,修饰类的话可以视为一级路由,一个类里可能会有多种方法,此时需要方法的二级路由来定位具体的方法.
点进这个注解的源码我们可以看到里面还有一些方法:value/path/method等,下面我们一起来谈谈这几个方法的作用:
i) value
用来给路由起名字,如果后面还有参数,需要指定如("/sayhi")这个参数是路由映射就要使用 (value = "/getname") 这样的方式来为参数命名
ii) path
同value;
iii) method
用来指定请求的方法,@RequestMapping ("/路由")这个注解可以用GET/POST方法触发,当指定只能用一个特定方法时,需要使用(method = RequestMethod.POST)指定
我们需要牢记一点就是: @RequestMapping ("/路由")这个注解既可以用GET触发也可以用POST方法触发
b)另两个注解@GetMapping和@PostMapping:同样用来指定特定的请求:
package com.example.demo;
import org.springframework.web.bind.annotation.*;
/**
* Created with IntelliJ IDEA.
* Description:
* User: yyx
* Date: 2023-10-24
* Time: 16:48
*/
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "/sayhi",method = RequestMethod.POST)
public String SayHi(){
return "hello Spring Boot MVC!";
}
@GetMapping(value = "/sayhi2")
public String SayHi2(){
return "hello Spring Boot MVC2!";
}
@PostMapping(value = "/sayhi3")
public String SayHi3(){
return "hello Spring Boot MVC3!";
}
}
此时我们需要拿出postman来构造一个post和get请求来模拟实现:此时是验证@RequestMapping:
接下来验证@GetMapping和@PostMapping:
(GetMapping只能支持get,能成功所以我只验证了post请求;PostMapping同理:)
在@RequestMapping中指定method为get和post同理,码友们可以自行验证
3.SpringBoot MVC 如何获取参数?
既然是通过HTTP请求访问,那么就需要拿到前端传来的数据,那前端都会传入哪些数据呢?我们如何获取呢?
3.1获取单个参数:
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA.
* Description:
* User: yyx
* Date: 2023-10-24
* Time: 17:05
*/
@RestController
@RequestMapping("/test")
public class TestController2 {
@RequestMapping(value = "/getname")
public String gatName(String name){
return "name +" + name;
}
}
我们希望前端传来一个键值对:name = xxx这样的参数(在键入的URL里后面加上?即可设置参数),那么后端可以直接拿,但是需要注意的是参数的name必须和前端传入的key值是一样的不然无法匹配. 我们通过postman或者前端键入URL来访问:
3.2获得传递过来的对象:
在前端中我们没有对象这样的概念,那么就是我们希望在前端传入的参数,与后端的对象中的属性相匹配,这样可以视作是,给对象的属性赋值,这样的好处是,我们不需要在参数中添加好多不同类型或者个数的参数,直接用对象封装一下,这样也可以降低代码的耦合度.
在com.example.demo下建一个包用来存放数据,就叫model:在model中写一个User类,可以使用Lombok的@Data注解来生成对应的getter/setter/toString方法:
package com.example.demo.model;
import lombok.Data;
/**
* Created with IntelliJ IDEA.
* Description:
* User: yyx
* Date: 2023-10-24
* Time: 22:04
*/
@Data
public class User {
private int id;
private String name;
private String password;
}
再新建一个UserController类用来当做User的启动类:
package com.example.demo;
import com.example.demo.model.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA.
* Description:
* User: yyx
* Date: 2023-10-24
* Time: 22:06
*/
@RestController
@RequestMapping("/user")
public class UserContorller {
@RequestMapping("/usertest")
public User getAll(User user){
System.out.println("name+" + user.getName());
System.out.println("id+" + user.getId());
System.out.println("password+" + user.getPassword());
return user;
}
}
此时我们希望前端传来的数据一定要和User类中的成员变量相吻合,在传参的时候我们直接在UserController中将User 传入即可: 还可以打印其中的name/id/password来验证是否可以得到对象:(其中我们要注意URL键入的时候参数使用?开头,参数和参数之间使用&隔开,参数的key值要和User的成员变量相契合)
假如传递的参数key与User成员变量不一致或者少传入参数会怎样?
少传入的参数按照null处理,多传入的参数匹配不到就不做处理:
3.3获取多个参数
假如传递过来多个参数,不利用面向对象的思想的话,就可能有点麻烦(小玉个人感觉)......感觉不足之处有二:
第一需要在后端代码中传参的时候,将参数一一写出来.
第二: 代码的耦合性较高.我们的目的是高内聚,低耦合.若是参数变动后端也需要及时更新参数列表...
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA.
* Description:传递多个参数:
* User: yyx
* Date: 2023-10-26
* Time: 10:26
*/
@RestController
@RequestMapping("/test255")
public class TestContorller255 {
@RequestMapping("/getparams")
public Object method_3(String name, String id) {
return " name "+name+" id"+id;
}
}
键入URL照样可以拿到:
今天先讲这么多,小玉很累,混个1024头衔算了.明日在做补充...............