javascript
SpringBoot(一)——快速入门
文章目錄
- 1. 了解SpringBoot
- 2. 第一個SpringBoot程序
- 3. 理解SpringBoot原理
- 4. SpringBoot 的配置
- 5. SpringBoot 集成 MyBatis
- 6. SpringBoot 開發 Web 應用
1. 了解SpringBoot
原生開發:Servlet + jsp ,十分麻煩,web.xml 或者代碼中都會存在大量重復內容;
簡化開發 :Spring 里面所有東西都是配置文件,集成很多框架或者做一些大型項目,會導致整個程序和項目十分的臃腫,通篇的配置文件;
簡化配置文件 :SpringBoot 就好像Spring的升級版,原來很多東西需要手動配置,現在只需要自動配置!
梳理SSM框架中麻煩之處 :
- 很多配置文件
- web.xml、 tomcat 都要配置
- lib依賴 也需要管理
那么在SpringBoot以上的一切,都不需要配置即可運行!
2. 第一個SpringBoot程序
1、使用IDEA構建一個SpringBoot程序
2、填寫Maven項目基本信息
3、勾選啟動器(Spring Web) 如果你勾選了這個,相當于幫你自動配置好了Spring和SpringMVC,包括Tomcat!
4、完成之后,等待Maven自動下載所有的依賴,再刪除不需要的幾個文件夾
最終的目錄如下:
5、編寫第一個程序
注意:一定要在 SpringBoot 主啟動類的同級或者子級目錄下,新建包!否則是識別不了的
6、啟動主啟動類,訪問測試:http://localhost:8080/hello
拓展:我們可以自定義 banner!
自定義的啟動Logo,默認是Spring,可以根據自己喜好進行修改。
(1)我們在 resource 目錄下新建一個 banner.txt文件,在這里面寫入自己的banner即可
(2)在線網站生成:https://www.bootschool.net/ascii
(3)啟動測試看效果
3. 理解SpringBoot原理
1、怎么啟動的 ?
一個注解:@SpringBootApplication
一個類:SpringApplication
可以將自動生成的這個主啟動類刪除,自己手動寫一個!
(1)在類上面增加一個@SpringBootApplication注解
(2) 使用SpringApplication的run方法啟動
如寫一個名為 ZzApplication 的主啟動類:
package com.zz;//自己手寫一個主啟動類 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class ZzApplication {public static void main(String[] args) {//run方法的啟動SpringApplication.run(ZzApplication.class,args);} }2、依賴怎么配置的
思考:我們沒有配置tomcat、 沒有配置servlet、沒有配置spring,這個怎么生效的?
SpringBoot默認有一個 pom.xml ,里面有父依賴以及場景啟動器
父依賴:
父依賴作用分析
(1)自動幫你管理依賴,里面包含了幾乎常用的所有依賴,如果你需要的依賴在這里面有,你就不要配置了,如果沒有再配置
(2)插件和資源過濾的自動管理
啟動器:
spring-boot-starter-xx 導入對應場景所需要的類,會自動幫你導入封裝了這個場景所需要需要的依賴
官網有所有的啟動器:https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/html/using-spring-boot.html#using-boot
4. SpringBoot 的配置
所有的配置都可以在配置文件中配置:
- application.yml / application.yaml
- application.properties
properties 是我們傳統的配置文件 key = value
yaml 是SpringBoot 推薦的配置文件,功能更加強大
yaml 所有語法:(空格嚴格要求,縮進嚴格要求)
# 普通鍵值對 key: value name: qinjiang# map/對象 key:k1: v1k2: v3person:name: qinjiangage: 3# 行內寫法 {} person: {name: qinjiang,age: 3}# list/數組 key:- v1- v2pets:- dog- pig- cat# 行內寫法 pets: [dog,pig,cat]5. SpringBoot 集成 MyBatis
注意:MyBatis 所有的包都是自己的,所以要導入自己的依賴
整個項目目錄:
1、在pom.xml中添加驅動和依賴
<!-- 這是自定義的包 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.1</version></dependency><!-- mysql 驅動 在這不寫版本號,默認使用mysql8的版本 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--在這里寫的依賴都不需要版本號,因為在父依賴中有!--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>2、編寫配置文件 application.yml
spring:datasource:username: rootpassword: 123456url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8driver-class-name: com.mysql.cj.jdbc.Driver mybatis:type-aliases-package: com.kuang.pojomapper-locations: classpath:com/kuang/mapper/*.xml3、測試數據源有沒有,在測試類中測試即可
package com.zz;import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException;@SpringBootTest class HelloSpringbootApplicationTests {// 自動導入數據源@Autowiredprivate DataSource dataSource;// SpringBoot 2.X 默認集成的是 Hikari@Testvoid contextLoads() throws SQLException {// 查看默認數據源 class com.zaxxer.hikari.HikariDataSourceSystem.out.println(dataSource.getClass());// connectionConnection connection = dataSource.getConnection();System.out.println(connection);// 關閉連接connection.close();} }SpringBoot 目前默認的數據源是 class com.zaxxer.hikari.HikariDataSource
4、編寫實體類 User
package com.zz.pojo;import lombok.Data;@Data public class User {private int id;private String name;private String pwd; }5、編寫接口 UserMapper(使用注解和 XML 都可以)
package com.zz.mapper;import com.zz.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository;import java.util.List;@Mapper // @Mapper 標注這是一個Mapper接口 @Repository // 代表持久層 public interface UserMapper {// @Select("select * from user") 注解方式List<User> getUserLists(); }6、編寫接口的配置文件 UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.zz.mapper.UserMapper"><select id="getUserLists" resultType="User">select * from user;</select> </mapper>7、編寫控制層 MyBatisController
package com.zz.controller;import com.zz.mapper.UserMapper; import com.zz.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController public class MyBatisController {@Autowiredprivate UserMapper userMapper;@RequestMapping("/list")public List<User> getUserList(){List<User> userLists = userMapper.getUserLists();return userLists;} }8、配置 .xml 文件的資源過濾
由于 .xml 的配置文件沒有導出,在pom.xml中配置資源過濾即可
9、啟動主啟動類,進行測試
在地址欄輸入 http://localhost:8080/list ,頁面就顯示了數據庫中user表的數據
6. SpringBoot 開發 Web 應用
1、資源存放目錄說明
static 靜態資源
templates 頁面,templates只能通過 controller來訪問資源文件
resources 也可以存放資源文件
public SpringBoot 源碼中找到的,靜態資源公共的可以放在這里
測試:
2、Thymeleaf 使用,導入靜態資源模板 使用html 編寫頁面
Thymeleaf是一個流行的模板引擎,該模板引擎采用Java語言開發,模板引擎是一個技術名詞,是跨領域跨平臺的概念。
Thymeleaf的主要目標在于提供一種可被瀏覽器正確顯示的、格式良好的模板創建方式,因此也可以用作靜態建模。你可以使用它創建經過驗證的XML與HTML模板。使用thymeleaf創建的html模板可以在瀏覽器里面直接打開(展示靜態數據),這有利于前后端分離。
(1)導入對應maven依賴
<!-- thymeleaf依賴,如果要編寫頁面一定需要這個依賴 --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>(2)編寫 html 頁面放到 templates 目錄下
(3)使用controller 進行跳轉
package com.zz.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;@Controller //可以被視圖解析器解析 public class IndexController {//只要訪問 / 就會跳轉到templates目錄下的index頁面@RequestMapping("/")public String index(){return "index";}@RequestMapping("/user/index")public String userIndex(){return "user/index";}@RequestMapping("/user/list")public String userList(){return "user/list";} }(4)啟動測試
3、頁面傳遞值
(1)在后端方法中使用 Model 傳遞值
(2)在前端使用 th:xxx 去接收后端傳遞值,注意:一定要導入頭文件約束
普通取值 和 循環遍歷
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>首頁</h1> <!-- 普通取值 --> <p th:text="${msg}"></p><!-- 循環遍歷:接收后端傳遞的 users,遍歷每個的結果就是user,可以在這個標簽內使用! th:each="item:items" --> <!--寫法一--> <h2 th:each="user:${users}" th:text="${user}"></h2> <!-- 寫法二:行內寫法 --> <h2 th:each="user:${users}">[[${user}]]</h2> <!--寫法三--> <div th:each="user:${users}"><p th:text="${user}"></p> </div></body> </html>啟動測試:
總結
以上是生活随笔為你收集整理的SpringBoot(一)——快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue——快速入门
- 下一篇: SpringBoot(二)——实现一个基