springboot與springmvc基礎(chǔ)入門講解
一,SpringBoot
–1,概述
用來整合maven項目,可以和Spring框架無縫銜接。
–2,用法
–1,創(chuàng)建SpringBoot工程:File-New-Project-選擇Spring Init…-next-輸入groupId、項目id、選成jdk8-next-選擇SpringWeb-ok
–2,配置Maven:File-Settings-選擇Build…-Maven-修改三處(解壓的位置、settings.xml位置-本地倉庫位置)-ok
–3,找到自動生成的一個類,直接運行 ( 啟動服務(wù)器 )

–4,創(chuàng)建類,讓瀏覽器訪問

–5,測試
啟動服務(wù)器

打開瀏覽器訪問指定的地址::http://localhost:8080/hi

二,SpringMVC
–1,概述
主要的職責(zé):接受瀏覽器發(fā)來的請求,給瀏覽器發(fā)送響應(yīng)的數(shù)據(jù)
遵循了MVC的設(shè)計模式:好處是可以把代碼松耦合
MVC的全稱:M是Model模型,用來封裝數(shù)據(jù)
V是View視圖,用來展示數(shù)據(jù)
C是Controller控制器,用來寫業(yè)務(wù)代碼
–2,原理
當(dāng)瀏覽器發(fā)起請求,就會訪問服務(wù)器----前端控制器DispatcherServlet—處理器映射器HandlerMapping—處理器適配器
HandlerAdaptor—視圖解析器ViewResolver—視圖渲染—響應(yīng)數(shù)據(jù)。
–前端控制器DispatcherServlet:: 把請求進(jìn)行分發(fā),找到對應(yīng)的類里的方法開始干活
–處理器映射器HandlerMapping::根據(jù)url來找到對應(yīng)的類并找到對應(yīng)的方法
http://localhost:8080/hello/hi 即將訪問 HelloBoot類里的 hi()
–處理器適配器HandlerAdaptor::拿到要執(zhí)行的類名和方法名,開始干活
–視圖解析器ViewResolver::解析要在瀏覽器上展示的數(shù)據(jù)
–視圖渲染:::真正的把數(shù)據(jù)在瀏覽器上展示
–3,入門案例
需求:訪問url地址,服務(wù)器返回汽車的相關(guān)數(shù)據(jù)
–1,創(chuàng)建Maven的模塊:選中工程-右鍵-New-Maven-next-輸入module的名字-finish

–2,創(chuàng)建啟動類RunApp

–3,創(chuàng)建汽車類
package cn.tedu;
//充當(dāng)MVC模式里的M層model:封裝數(shù)據(jù)
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,創(chuàng)建類,接受瀏覽器的請求,并返回數(shù)據(jù)

package cn.tedu;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//職責(zé):接受請求+做出響應(yīng)
@RestController //接受瀏覽器發(fā)來的請求
@RequestMapping("car")//規(guī)定了url的寫法
public class CarController {
//訪問http://localhost:8080/car/find,
//在瀏覽器展示了{(lán)"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;//把結(jié)果返回給了瀏覽器
}
//訪問http://localhost:8080/car/save ,在瀏覽器展示abc
@RequestMapping("save")
public String save(){
//接受請求,并返回數(shù)據(jù)
return "abc";
}
//訪問http://localhost:8080/car/get ,在控制臺打印123
@RequestMapping("get")//規(guī)定了url的寫法
public void get(){
System.out.println(123);
}
}
–5,測試

總結(jié)
SpringMVC的原理?DispatcherServlet->HandlerMapping->HandlerAdaptor->ViewResolver->View
SpringMVC里用的注解?@RestController 接受請求 + 負(fù)責(zé)響應(yīng) (把數(shù)據(jù)變成JSON串)
@RequestMapping 跟url匹配規(guī)定了url的寫法
@RestController 只能出現(xiàn)在類上
@RequestMapping 可以出現(xiàn)在類上或方法上
SpringBoot的注解?@SpringBootApplication 用來作為springboot的啟動類
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家更多內(nèi)容!
相關(guān)文章
java實現(xiàn)列表、集合與數(shù)組之間轉(zhuǎn)化的方法
這篇文章主要介紹了java實現(xiàn)列表、集合與數(shù)組之間轉(zhuǎn)化的方法,涉及java中列表、集合與數(shù)組相互轉(zhuǎn)換的技巧,非常具有實用價值,需要的朋友可以參考下2015-03-03
SpringMVC中使用@PathVariable綁定路由中的數(shù)組的方法
這篇文章主要介紹了SpringMVC中使用@PathVariable綁定路由中的數(shù)組的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
在MyBatis中實現(xiàn)一對多查詢和多對一查詢的方式詳解(各兩種方式)
今天通過兩種方法分別給大家介紹在MyBatis中實現(xiàn)一對多查詢和多對一查詢的方式,每種方式通過實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-01-01
spring通過構(gòu)造函數(shù)注入實現(xiàn)方法分析
這篇文章主要介紹了spring通過構(gòu)造函數(shù)注入實現(xiàn)方法,結(jié)合實例形式分析了spring通過構(gòu)造函數(shù)注入的原理、實現(xiàn)步驟及相關(guān)操作注意事項,需要的朋友可以參考下2019-10-10

