springboot與springmvc基礎入門講解

一,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,創建汽車類

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,創建類,接受瀏覽器的請求,並返回數據

在這裡插入圖片描述

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的啟動類

本篇文章就到這裡瞭,希望能給你帶來幫助,也希望您能夠多多關註WalkonNet更多內容!

推薦閱讀: