靜態資源導入
在WebMvcAutoConfiguration自動配置類中
有一個添加資源的方法:
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//如果靜態資源已經被自定義了,則直接生效if (!this.resourceProperties.isAddMappings()) {logger.debug("Default resource handling disabled");} else {
//注冊一個/webjars/**,添加靜態資源到該路徑下this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
//獲得靜態資源的路徑this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {registration.addResourceLocations(this.resourceProperties.getStaticLocations());if (this.servletContext != null) {ServletContextResource resource = new ServletContextResource(this.servletContext, "/");registration.addResourceLocations(new Resource[]{resource});}});}}
自定義靜態資源:就是找到WebMvcProperties內部關于靜態資源的配置,然后在配置文件中配置即可。
?webjars引入jquery
<dependency>
??? <groupId>org.webjars</groupId>
??? <artifactId>jquery</artifactId>
??? <version>3.4.1</version>
</dependency>
導入依賴后即可訪問webjars下的信息。
靜態資源路徑:
public static class Resources {private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
綜上:有4個路徑下的靜態資源可被訪問。public為1,resources為2,static為3
訪問結果:?
說明resources優先級最高。
刪掉resources。
?總結:優先級為:resources>static>public
常用于:public為公共資源(都會訪問的頁面),static為靜態資源,如圖片等,resources放上傳的文件。
首頁定制
從WebMvcAutoConfiguration中找到首頁相關的配置。
從getWelcomePage()方法中找到自定義的路徑。
String[] var1 = this.resourceProperties.getStaticLocations();
也可以通過靜態資源目錄找index.html的文件
return this.getIndexHtml(this.resourceLoader.getResource(location));
Resource resource = location.createRelative("index.html");
所以可以在靜態資源目錄下添加一個index.html文件。
注意:templates非靜態資源目錄,該目錄下的頁面只能通過controller來跳轉。并且需要模板引擎的支持(需要導入thymeleaf依賴)。
模板引擎
SpringBoot使用的是嵌入式的Tomcat,不支持jsp頁面,需要用html,然后用模板引擎來渲染前端。
模板引擎的作用:處理前端一些動態的值。
導入依賴:
<dependency>
??? <groupId>org.springframework.boot</groupId>
??? <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在templates下寫前端頁面:
在controller中編寫:
測試:
使用Thymeleaf時,在html上導入約束。
<html?lang="en"?xmlns:th="http://www.thymeleaf.org">
Thymeleaf 模板引擎支持多種表達式:
- 變量表達式:${...}
- 選擇變量表達式:*{...}
- 鏈接表達式:@{...}
- 國際化表達式:#{...}
- 片段引用表達式:~{...}
測試:
th:each使用
遍歷,支持 Iterable、Map、數組等
在controller中編寫一個集合傳遞給前端:
前端通過th:eath來遍歷數組:
測試:
整合Mybatis框架
SpringBoot整合Mybatis框架需要一個整合包:mybatis-spring-boot-starter。項目創建成功后,需要引入該依賴。
<dependency>
? ? <groupId>org.mybatis.spring.boot</groupId>
? ? <artifactId>mybatis-spring-boot-starter</artifactId>
? ? <version>2.2.2</version>
</dependency>
在創建SpringBooot項目時,需要勾選:
在properties文件中配置數據庫信息進行連接:此處連接的是mybatis數據庫。
#整合mybatis
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
#配置數據源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
在測試類中注入DataSource,進行數據庫連接成功測試:
測試時,無錯誤提示即連接成功
編寫實體類:事先導入lombok依賴節省時間。
編寫mapper實現類:
/*該注解表示這是一個mapper類,也可在啟動類上添加@MapperScan("com.example.mapper")
* 表示掃描該包下的所有接口
* @Repository,該類被Spring整合,接口類dao統一用該注解*/
@Mapper
@Repository
public interface UserMapper {List<User> queryUserList();User queryUserById(int id);
}
編寫mapper.xml文件:頭文件可從MyBatis中文文檔中找入門_MyBatis中文網
<?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.example.mapper.UserMapper"><select id="queryUserList" resultType="User">select * from user </select><select id="queryUserById" resultType="User">select * from user where id=#{id}</select>
</mapper>
在properties中整合mybatis:
#整合Mybatis
#設置別名
mybatis.type-aliases-package=com.example.springboot02mybatis.pojo
#設置mapper的地址
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
編寫Controller類:
@RestController
public class UserControlller {@Autowiredprivate UserMapper userMapper;@GetMapping("/queryUserList")public List<User> queryUserList(){List<User> users = userMapper.queryUserList();for (User user : users) {System.out.println(user);}return users;}
}
注意:所有路徑都需要對應上,不可出錯
進行測試,可正常輸出user列表信息:
編寫通過id查詢用戶:
@GetMapping("/queryUserById")public String queryUserById(){User user = userMapper.queryUserById(3);System.out.println(user);return "ok";}