Springboot
一.SpringBoot簡介:
Spring Boot可以讓我們的spring應用變得更輕量化。例如:你可以緊靠一個java類來運行你的spring應用。你也可以把你的應用打包成jar,並通過java -jar來運行你的Spring Web應用。
Spring Boot的主要優點:
爲Spring開發者更快入門;開箱即用,提供各種默認配置來簡化項目配置;內嵌式容器簡化Web項目;沒有冗餘代碼生成和XML配置要求等。
二.寫第一個SpringBoot項目“Hello World”
1.選擇File---New---Project。
2.選擇Spring初始化器-Spring Initializr,其他如果沒有修改,選擇默認即可,點擊next。
3.這裏我只修改了Group名稱,其他根據實際情況修改,點擊next。
4.這裏選擇Web,勾選Spring Web,點擊next。
5.這裏給項目命名,修改存儲路徑,點擊Finish。
6.生成文件簡介
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- 這個特殊的依賴包含了應用運行需要的所有信息,它包含了Spring Boot應用所必須的類似於Spring FrameWork(spring-core)、
Spring Test(spring-test)等基礎依賴的依賴描述。你只需要使用這個parent pom就能完成所有的依賴描述添加工作。-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.springboot</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 添加這個依賴之後就可以創建一個web應用程序。starter poms部分可以引入所有需要在實際項目中使用的依賴。
spring-boot-starter-web依賴包含所有的spring-core, spring-web, spring-webmvc,嵌入的Tomcat server和其他web應用相關的庫。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
7.這裏我將DemoApplication.java作了簡單修改實現我想要的功能。
運行結果如下:
從控制檯可以看到Springboot有內嵌式Tomcat。
運行地址:http://localhost:8080/admin/home
註解說明:
@EnableAutoConfiguration:作用在於讓 Spring Boot 根據應用所聲明的依賴來對 Spring 框架進行自動配置
這個註解告訴Spring Boot根據添加的jar依賴猜測你想如何配置Spring。由於spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration將假定你正在開發一個web應用並相應地對Spring進行設置。
@RestController:在上加上RestController 表示修飾該Controller所有的方法返回JSON格式,直接可以編寫Restful接口。@RequestMapping:此註解提供的是路由信息。它告訴Spring任何來自“/”路徑的請求都會被映射到home方法。
至此,一個簡單的Springboot就已經創建好了。
三.Web開發。
我們在開發Web應用時,需要引用大量的js、css、圖片等靜態資源。
默認配置:
Springboot默認靜態資源放在classpath下,目錄名符合以下規則:
/static
/public
/resources
/META-INF/resources
例:我們可以在src/main/resources/static目錄下放一個圖片文件。啓動程序後訪問:http://localhost:8080/A.jpg。如果能顯示圖片,則配置成功。
四.全局捕獲異常。
代碼如下:
異常處理類:
注:@ControllerAdvice 定義全局異常處理類,
@ExceptionHandler 聲明異常處理的方法。
Controller:
運行結果:
返回結果是json,沒有報錯,說明異常捕獲成功。
五.渲染Web頁面。
Springboot提供的默認配置的模板引擎有以下幾種:
Thymeleaf、FreeMarker、Velocity、Groovy、Mustache。
避免使用JSP,因爲無法實現SpringBoot多種特性,默認模板配置路徑:src/main/resources/templates,也可以修改此路徑。
使用FreeMarker模板引擎渲染web視圖:
pom文件:
<!--引入freemarker依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
application.properties:
########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates/
#comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved
Controller:
index.ftl:
啓動項目,運行結果如下:
使用JSP渲染Web頁面。
pom:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!-- SpringBoot 核心組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
application.properties:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
六.數據訪問。
SpringBoot整合mybatis:
引入依賴:
配置mysql:
這裏使用註解實現簡單的查詢:
controller:
mapper:
啓動類:
數據庫:
查詢顯示結果:
七.Springboot整合jpa。
定義:JPA 即Java Persistence API。
JPA 是一個基於O/R映射的標準規範(目前最新版本是JPA 2.1 )。所謂規範即只定義標準規則(如註解、接口),不提供實現,軟件提供商可以按照標準規範來實現,而使用者只需按照規範中定義的方式來使用,而不用和軟件提供商的實現打交道。
JPA的出現有兩個原因:
簡化現有Java EE和Java SE應用的對象持久化的開發工作;
Sun希望整合對ORM技術,實現持久化領域的統一。
JPA 的主要實現有Hibernate、EclipseLink 和OpenJPA 等,這也意味着我們只要使用JPA 來開發,無論是哪一個開發方式都是一樣的。
JPA通過JDK 5.0註解或XML描述對象-關係表的映射關係,並將運行期的實體對象持久化到數據庫中。
引入依賴:
配置數據源文件:
代碼:
注:jpa的版本高,使用findById(id).get(),不過這個如果值不存在會拋異常,所以要先做判斷,值存在再get(),或者就是寫在try-catch裏。也可以用它的findById(id).orElse(null); 如果不存在會返回null不會拋異常 。
部分實體類:
啓動類:
啓動後運行結果: