1. 程式人生 > 其它 >有關SpringBoot的介紹

有關SpringBoot的介紹

一、springboot的介紹

  • ssm框架搭建太麻煩

  • 很多開發者已經達成的共識,在ssm中依然要配置,springboot給所有開發者做了一個統一的規範

  • 固定型別的檔案就放在固定的位置上(約定大於配置)

二、springboot專案搭建

  1. 建立普通的maven專案,在pom.xml中引入Springboot父工程

<parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.0.0.RELEASE</version>
</parent>
  1. 引入我們需要的模組

<dependencies>
    <!-- 引入springmvc的模組-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 引入mybatis的模組-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version>
    </dependency>
    <!-- 引入mybatis需要的資料庫驅動包-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.24</version>
    </dependency>
</dependencies>
  1. 引入springboot的外掛

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  1. 建立SpringBoot的啟動類,在根路徑下

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
@SpringBootApplication
public class Day07SpringBootApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(Day07SpringBootApplication.class,args);
    }
​
}
  1. 在resource下建立兩個基礎資料夾

  • static:存放可以被外部直接訪問的靜態檔案,例如:js,css,圖片等

  • templates:存檔模板,暫時我們可以理解為存放 頁面

  1. 在resources下建立springboot的配置檔案

  • properties型別的配置檔案

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb2
spring.datasource.username=root
spring.datasource.password=hanchun123
  • yml/ymal型別的配置檔案

優點:更加清晰

缺點:必須嚴格的縮排 , 每個名稱的後面的必須有冒號+空格

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mydb2
    username: 使用者名稱
    password:密碼

三、Springidea工具自動搭建Springboot專案

  1. file --> new --> project --> 選擇springboot專案 --> 填寫專案名,包名

  2. 勾選需要引入的模組, 例如springweb, mybatis等等, 然後結束

  3. 這時, 工具會給我建立完善的專案結構, 必要的檔案

  4. 我們只需要在配置檔案中做一些基本配置就可以使用了, 例如配置資料庫連線資訊等

四、建立頁面

  1. 如果想要直接訪問的檔案,可以放在static資料夾中

  2. 如果想要按照規範, 將頁面放到了templates中, 那麼需要引入靜態模板模組,再由controller轉發

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
@RequestMapping("/toHome")
public String toHome() {
    System.out.println("........");
    return "home";
}

五、接收和返回引數

  1. 客戶端傳遞引數到Contrller, 如果引數過多, 會導致我們使用起來非常麻煩

Contoller上的方法需要寫很多的引數, Service上的方法也需要寫很多的引數, Dao上的方法也需要寫很多的引數

所以應該啟用實體類

  1. 返回給客戶端的資料,至少包含三部分內容 : 狀態碼, 解釋說明, 資料

  • 狀態碼 :留給客戶端快速判斷“成功”還是“失敗”

  • 解釋說明 :告訴客戶端人員,本次請求的結果是什麼意思

  • 資料 :如果是查詢,除了要告訴客戶端成員成功還是失敗以外,還要有資料

所以,我們需要定一個類,將返回的內容包裹在一起,一次性返回出去

六、查詢商品並展示的功能

  1. 資料庫建表

  2. 定義和資料庫表相對應的JavaBean

  3. 在mybatis的介面中定義方法 getGoods, 在對應的對映檔案中,寫上sql語句

  4. mybatis幫助我們查詢商品資訊, 並且將每條商品資訊變成一個GoodsBean物件, 並且把這些物件放到一個集合中返回給service層

  5. service再把資料返回給controller, contoller將資料和狀態碼,解釋資訊一起返回給客戶端'

  6. 客戶端展示資訊

七、打包

  1. war

  2. jar

  • 引入外掛

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
  • 找到maven控制檯,雙擊package按鈕打包

  • 完成之後的包住在目錄下

  • 直接執行 java -jar xxx.jar / javaw -jar xxx.jar