springboot與springmvc基礎入門講解
一,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,找到自動生成的一個類,直接運行 ( 啟動服務器 )

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

–5,測試
啟動服務器

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

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

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

–3,創(chuàng)建汽車類
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,創(chuàng)建類,接受瀏覽器的請求,并返回數據

package cn.tedu;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//職責:接受請求+做出響應
@RestController //接受瀏覽器發(fā)來的請求
@RequestMapping("car")//規(guī)定了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")//規(guī)定了url的寫法
public void get(){
System.out.println(123);
}
}
–5,測試

總結
SpringMVC的原理?DispatcherServlet->HandlerMapping->HandlerAdaptor->ViewResolver->View
SpringMVC里用的注解?@RestController 接受請求 + 負責響應 (把數據變成JSON串)
@RequestMapping 跟url匹配規(guī)定了url的寫法
@RestController 只能出現在類上
@RequestMapping 可以出現在類上或方法上
SpringBoot的注解?@SpringBootApplication 用來作為springboot的啟動類
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關注腳本之家更多內容!
相關文章
SpringBoot使用AOP+注解實現簡單的權限驗證的方法
這篇文章主要介紹了SpringBoot使用AOP+注解實現簡單的權限驗證的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
Java JDBC批量執(zhí)行executeBatch方法詳解
這篇文章主要介紹了Java JDBC批量執(zhí)行executeBatch方法詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08

