一,SpringBoot
–1,概述
用來整合maven項目,可以和Spring框架無縫銜接。
–2,用法
–1,創建SpringBoot工程:File-New-Project-選擇Spring Init…-next-輸入groupId、項目id、選成jdk8-next-選擇SpringWeb-ok
–2,配置Maven:File-Settings-選擇Build…-Maven-修改三處(解壓的位置、settings.xml位置-本地倉庫位置)-ok
–3,找到自動生成的一個類,直接運行 ( 啟動服務器 )
–4,創建類,讓瀏覽器訪問
–5,測試
啟動服務器
打開瀏覽器訪問指定的地址::http://localhost:8080/hi
二,SpringMVC
–1,概述
主要的職責:接受瀏覽器發來的請求,給瀏覽器發送響應的數據
遵循了MVC的設計模式:好處是可以把代碼松耦合
MVC的全稱:M是Model模型,用來封裝數據
V是View視圖,用來展示數據
C是Controller控制器,用來寫業務代碼
–2,原理
當瀏覽器發起請求,就會訪問服務器----前端控制器DispatcherServlet—處理器映射器HandlerMapping—處理器適配器
HandlerAdaptor—視圖解析器ViewResolver—視圖渲染—響應數據。
–前端控制器DispatcherServlet
:: 把請求進行分發,找到對應的類里的方法開始干活
–處理器映射器HandlerMapping
::根據url來找到對應的類并找到對應的方法
http://localhost:8080/hello/hi 即將訪問 HelloBoot類里的 hi()
–處理器適配器HandlerAdaptor
::拿到要執行的類名和方法名,開始干活
–視圖解析器ViewResolver
::解析要在瀏覽器上展示的數據
–視圖渲染:::真正的把數據在瀏覽器上展示
–3,入門案例
需求:訪問url地址,服務器返回汽車的相關數據
–1,創建Maven的模塊:選中工程-右鍵-New-Maven-next-輸入module的名字-finish
–2,創建啟動類RunApp
–3,創建汽車類
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
|
package cn.tedu; //充當MVC模式里的M層model:封裝數據 public class Car{ //提供屬性 + get/set/toString private int id; private String name; private String type; private String color; private double price; // get/set /toString public int getId() { return id; } public void setId( int id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getType() { return type; } public void setType(String type) { this .type = type; } public String getColor() { return color; } public void setColor(String color) { this .color = color; } public double getPrice() { return price; } public void setPrice( double price) { this .price = price; } //如果沒重寫,就是用Object的toString()返回的是地址值。 //沒重了,就是返回屬性值。 @Override public String toString() { return "Car{" + "id=" + id + ", name='" + name + '\ '' + ", type='" + type + '\ '' + ", color='" + color + '\ '' + ", price=" + price + '}' ; } } |
–4,創建類,接受瀏覽器的請求,并返回數據
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
|
package cn.tedu; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; //職責:接受請求+做出響應 @RestController //接受瀏覽器發來的請求 @RequestMapping ( "car" ) //規定了url的寫法 public class CarController { //訪問http://localhost:8080/car/find, //在瀏覽器展示了{"id":718,"name":"保時捷","type":"Cayman T","color":"紅色","price":641000.0} @RequestMapping ( "find" ) public Car find(){ Car c = new Car(); c.setId( 718 ); c.setName( "保時捷" ); c.setType( "Cayman T" ); c.setColor( "紅色" ); c.setPrice( 641000 ); return c; //把結果返回給了瀏覽器 } //訪問http://localhost:8080/car/save ,在瀏覽器展示abc @RequestMapping ( "save" ) public String save(){ //接受請求,并返回數據 return "abc" ; } //訪問http://localhost:8080/car/get ,在控制臺打印123 @RequestMapping ( "get" ) //規定了url的寫法 public void get(){ System.out.println( 123 ); } } |
–5,測試
總結
SpringMVC的原理?DispatcherServlet->HandlerMapping->HandlerAdaptor->ViewResolver->View
SpringMVC里用的注解?@RestController 接受請求 + 負責響應 (把數據變成JSON串)
@RequestMapping 跟url匹配規定了url的寫法
@RestController 只能出現在類上
@RequestMapping 可以出現在類上或方法上
SpringBoot的注解?@SpringBootApplication 用來作為springboot的啟動類
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關注腳本之家更多內容!
原文鏈接:https://blog.csdn.net/u012932876/article/details/117810556