Java SpringBoot高級用法詳解
1,IDEA中Lombok作用
數(shù)據(jù)庫: 庫 表 字段 對應的值 user表(id,name,age)
實體對象pojo: 用來封裝數(shù)據(jù)庫中的數(shù)據(jù) User類(id,name,age)
實體對象方法: Get/Set/toString/無參構造/有參構造/equals/hashcode
lombok作用: 自動生成上述的方法.
創(chuàng)建項目
依賴的版本 2.4.1、
2.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> <!-- 該配置表示Maven坐標 --> <!-- 項目的組id --> <groupId>com.jt</groupId> <!-- 項目名稱 --> <artifactId>springboot_demo1</artifactId> <!-- 項目版本號 --> <version>0.0.1-SNAPSHOT</version> <name>springboot_demo1</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.4.1</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <!-- SpringBoot的啟動項 wep 相當于引入MVC框架 思想:"開箱即用"?。?! 說明: 只需要引入jar包,簡單的配置即可以使用該功能 --> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <!--相當于繼承了一個父級--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <!--通過pom標識 是一個父級 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.4.1</version> <configuration> <mainClass>com.jt.SpringbootDemo1Application</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
2.1 pom.xml標簽說明
說明: 該坐標在本地倉庫中是唯一標識符.是當前項目打包/被依賴的唯一 路徑.
根據(jù)坐標查找jar包
擴展: 如果項目中依賴第三方jar包文件報錯! 如何處理?
問題說明: 有時根據(jù)坐標下載jar包文件時,可能由于網(wǎng)絡問題,導致jar包下載不完整.
解決方案: 根據(jù)第三方的坐標,查找到本地倉庫的位置,之后刪除 重新下載.
- 默認條件下 jar包
- web項目可以打成 war包
- 如果該項目是父級項目 則寫 pom
通過dependencyManagement標簽統(tǒng)一定義父級工程,在其中定義了 springBoot項目所有兼容的版本信息.
所以依賴項中不需要添加版本號,也可以正常依賴jar包文件
2.2 依賴的相關說明
思想: “開箱即用” 是springBoot設計的核心 越來越簡單!!!
什么是啟動項: SpringBoot為整合第三方框架,寫了啟動項的jar包文件, 其中官方已經將所有的配置信息/需要依賴的jar包文件提前測試并且定義.
說明: maven中的jar包是有依賴的傳遞性
例如: A項目依賴B.jar包, B.jar依賴C.jar. 在項目中,只需要添加B.jar. 則B/C.jar都會自動添加.
實際應用: 如圖web.jar包中依賴了很多其它的第三方jar包文件.
mavenjar包查詢網(wǎng)址: https://mvnrepository.com/
本地倉庫文件說明:
1. 當maven開始解析項目的POM.xml文件時,根據(jù)依賴的坐標,找到指定的 jar包文件.之后添加該依賴.
2. 之后掃描當前文件中的 xxx.pom文件.
3. 掃描pom.xml文件中的依賴信息dependency
4. 根據(jù)dependency的坐標 重復執(zhí)行上述的操作.直到所有的依賴都添加完 成.
需求: 網(wǎng)絡數(shù)據(jù)傳輸,一般都需要進行加密處理.maven中采用SHA1數(shù)字簽 名的加密算法,保證數(shù)據(jù)傳遞的有效性!!!
說明: maven數(shù)據(jù)傳遞有效性原理圖.
2.3 SHA1介紹
SHA-1(英語:Secure Hash Algorithm 1,中文名:安全散列算法1)是一種密碼散列函 數(shù),美國國家安全局設計,并由美國國家標準技術研究所(NIST)發(fā)布為聯(lián)邦數(shù)據(jù)處理 標準(FIPS)。SHA-1可以生成一個被稱為消息摘要的160位(20字節(jié))散列值,散列 值通常的呈現(xiàn)形式為**40個十六進制數(shù)**。
關鍵字: 數(shù)字證書.
問題1: 常見hashcode值 有多少位16進制數(shù)組成??? 8位
問題2: 8位16進制數(shù),有多少種排列組合? 2^32種
00000000-FFFFFFFF
問題3: 相同數(shù)據(jù)進行hash(算法相同),問題: 值是否相同? 必定相同
問題4: 不同數(shù)據(jù)進行hash(算法相同),問題: 值是否相同? 可能相同 hash碰撞
問題5: 一個數(shù)據(jù)1kb, 一個數(shù)據(jù)ITB 問: hash計算的速度誰快? “一樣快” hash本質
SpringBoot高級用法
Pro文件說明
- pro文件語法
- 數(shù)據(jù)結構類型: key=value 特別注意不要有空格.
- 字符集編碼: 程序讀取文件時,默認采用ISO-8859-1編碼
- 弊端: 所有的key都必須寫完整,不能縮進
YML文件說明
YML文件的語法
- 數(shù)據(jù)結構 key-value結構
- 寫法: key:(空格)value
- 層級代碼結構,注意縮進
- 字符集 文件讀取時,默認采用UTF-8編碼 可以寫中文
server:
port: 8080
3.需求說明
說明: 有時將數(shù)據(jù)寫死,不方便后續(xù)擴展,需要為屬性動態(tài)賦值.
解決方案: 有些數(shù)據(jù)是后臺特有的.一般可以將數(shù)據(jù)寫到配置文件中,之后 為屬性動態(tài)賦值
編輯YML
#YML文件的語法 #1,數(shù)據(jù)結構 key-value結構 #2,寫法: key:(空格)value #3,層及代碼結構,注意縮進 #4,字符集 文件讀取時,默認采取UTF-8編碼 可以寫中文 # 規(guī)則: 命名時最好指定前綴. server: port: 8080 # 指定業(yè)務的key mysql: Username: root| Password: root||
啟動類
package com.jt; //import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //import org.springframework.web.bind.annotation.RequestMapping; @SpringBootApplication public class SpringbootDemo1Application { public static void main(String[] args) { SpringApplication.run(SpringbootDemo1Application.class, args); } }
動態(tài)為屬性賦值
package com.jt.contraller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 說明: * 1.將該類交給Spring容器管理 * 2.SpringMVC負責調用該對象接收用戶的請求. * 3.將業(yè)務處理之后的結果,為頁面返回JSON數(shù)據(jù). * @ResponseBody作用: 將數(shù)據(jù)轉化為JSON串 */ @RestController public class JDBCController { //${key} Spring提供的springel表達式 簡稱為:spel表達式 //語法: 從spring容器內部獲取key,動態(tài)為屬性賦值. @Value("${mysql.username}") String username; // = "root|"; @Value("${mysql.password}") String password; // = "root"; @RequestMapping("/getMsg") public String getMsg() { return "你好數(shù)據(jù)庫:" + username + password; } }
3.2利用properties文件為屬性賦值
YML文件是SpringBoot的核心配置文件,一般主要用來整合其它第三方框架.屬于系統(tǒng)配置文件.如果將大量的業(yè)務數(shù)據(jù)寫到系統(tǒng)配置文件中. 耦合性高. 所以將業(yè)務數(shù)據(jù)最好放到pro文件中.
配置 pro 文件
#默認ISO-8859-1 中文必定亂碼 mysql.username2=mysql數(shù)據(jù)庫 mysql.password2=你猜猜
為屬性賦值
package com.jt.contraller; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 說明: * 1.將該類交給Spring容器管理 * 2.SpringMVC負責調用該對象接收用戶的請求. * 3.將業(yè)務處理之后的結果,為頁面返回JSON數(shù)據(jù). * @ResponseBody作用: 將數(shù)據(jù)轉化為JSON串 * * propertySource: value屬性指定路徑 * encoding屬性指定配置文件編碼格式 */ @RestController @PropertySource(value="classpath:/mysql.properties",encoding = "UTF-8") public class JDBCContraller { /** * 難點: 如何將pro文件交給Spring容器管理???? * 解決方案: @PropertySource("xxxxxx/xxx.properties") 指定配置文件交給Spring * 容器管理 */ @Value("${mysql.username2}") private String username2; @Value("${mysql.password2") private String password2; @RequestMapping("/getMsg2") public String getMsg2(){ return "你好數(shù)據(jù)庫:"+ username2 +password2; } }
結構
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
jcl與jul?log4j1?log4j2?logback日志系統(tǒng)機制及集成原理
這篇文章主要介紹了jcl與jul?log4j1?log4j2?logback的集成原理,Apache?Commons-logging?通用日志框架與日志系統(tǒng)的機制,有需要的朋友可以借鑒參考下2022-03-03mybatis查詢返回Map<String,Object>類型的講解
這篇文章主要介紹了mybatis查詢返回Map<String,Object>類型的講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06SpringBoot使用AOP實現(xiàn)日志記錄功能詳解
這篇文章主要為大家介紹了SpringBoot使用AOP實現(xiàn)日志記錄功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07SpringSecurity之SecurityContextHolder使用解讀
這篇文章主要介紹了SpringSecurity之SecurityContextHolder使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03