1,IDEA中Lombok作用

資料庫: 庫 表 欄位 對應的值 user表(id,name,age)
物體物件pojo: 用來封裝資料庫中的資料 User類(id,name,age)
物體物件方法: Get/Set/toString/無參構造/有參構造/equals/hashcode
lombok作用: 自動生成上述的方法.
- 創建專案

依賴的版本 2.4.1、
- 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>
3.1 pom.xml標簽說明
說明: 該坐標在本地倉庫中是唯一識別符號.是當前專案打包/被依賴的唯一 路徑.
根據坐標查找jar包
擴展: 如果專案中依賴第三方jar包檔案報錯! 如何處理?
問題說明: 有時根據坐標下載jar包檔案時,可能由于網路問題,導致jar包 下載不完整.
解決方案: 根據第三方的坐標,查找到本地倉庫的位置,之后洗掉 重新下 載.
· 默認條件下 jar包
· web專案可以打成 war包
· 如果該專案是父級專案 則寫 pom
通過dependencyManagement標簽統一定義父級工程,在其中定義了 springBoot專案所有兼容的版本資訊.
所以依賴項中不需要添加版本號,也可以正常依賴jar包檔案
3.2 依賴的相關說明
思想: “開箱即用” 是springBoot設計的核心 越來越簡單!!!
什么是啟動項: SpringBoot為整合第三方框架,寫了啟動項的jar包檔案, 其中官方已經將所有的配置資訊/需要依賴的jar包檔案提前測驗并且定義.
說明: maven中的jar包是有依賴的傳遞性
例如: A專案依賴B.jar包, B.jar依賴C.jar. 在專案中,只需要添加B.jar. 則B/C.jar都會自動添加.
實際應用: 如圖web.jar包中依賴了很多其它的第三方jar包檔案.
mavenjar包查詢網址: https://mvnrepository.com/
本地倉庫檔案說明:
1. 當maven開始決議專案的POM.xml檔案時,根據依賴的坐標,找到指定的 jar包檔案.之后添加該依賴.
2. 之后掃描當前檔案中的 xxx.pom檔案.
3. 掃描pom.xml檔案中的依賴資訊dependency
4. 根據dependency的坐標 重復執行上述的操作.直到所有的依賴都添加完 成.
需求: 網路資料傳輸,一般都需要進行加密處理.maven中采用SHA1數字簽 名的加密演算法,保證資料傳遞的有效性!!!
說明: maven資料傳遞有效性原理圖.

3.3 SHA1介紹
SHA-1(英語:Secure Hash Algorithm 1,中文名:安全散列演算法1)是一種密碼散列函 數,美國國家安全域設計,并由美國國家標準技術研究所(NIST)發布為聯邦資料處理 標準(FIPS),SHA-1可以生成一個被稱為訊息摘要的160位(20位元組)散列值,散列 值通常的呈現形式為**40個十六進制數**,
關鍵字: 數字證書.
問題1: 常見hashcode值 有多少位16進制陣列成??? 8位
問題2: 8位16進制數,有多少種排列組合? 2^32種
00000000-FFFFFFFF
問題3: 相同資料進行hash(演算法相同),問題: 值是否相同? 必定相同
問題4: 不同資料進行hash(演算法相同),問題: 值是否相同? 可能相同 hash碰撞
問題5: 一個資料1kb, 一個資料ITB 問: hash計算的速度誰快? “一樣快” hash本質
- SpringBoot高級用法
Pro檔案說明
# 1.pro檔案語法
# 資料結構型別: key=value 特別注意不要有空格.
# 字符集編碼: 程式讀取檔案時,默認采用ISO-8859-1編碼
# 弊端: 所有的key都必須寫完整,不能縮進
YML檔案說明
# YML檔案的語法
# 1.資料結構 key-value結構
# 2.寫法: key:(空格)value
# 3.層級代碼結構,注意縮進
# 4.字符集 檔案讀取時,默認采用UTF-8編碼 可以寫中文
server:
port: 8080
4.1需求說明
說明: 有時將資料寫死,不方便后續擴展,需要為屬性動態賦值.
解決方案: 有些資料是后臺特有的.一般可以將資料寫到組態檔中,之后 為屬性動態賦值
編輯YML
#YML檔案的語法
#1,資料結構 key-value結構
#2,寫法: key:(空格)value
#3,層及代碼結構,注意縮進
#4,字符集 檔案讀取時,默認采取UTF-8編碼 可以寫中文
# 規則: 命名時最好指定前綴.
server:
port: 8080
# 指定業務的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);
}
}
動態為屬性賦值
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.將業務處理之后的結果,為頁面回傳JSON資料.
* @ResponseBody作用: 將資料轉化為JSON串
*/
@RestController
public class JDBCController {
//${key} Spring提供的springel運算式 簡稱為:spel運算式
//語法: 從spring容器內部獲取key,動態為屬性賦值.
@Value("${mysql.username}")
String username; // = "root|";
@Value("${mysql.password}")
String password; // = "root";
@RequestMapping("/getMsg")
public String getMsg() {
return "你好資料庫:" + username + password;
}
}
4.2利用properties檔案為屬性賦值
YML檔案是SpringBoot的核心組態檔,一般主要用來整合其它第三方框架.屬于系統組態檔.如果將大量的業務資料寫到系統組態檔中. 耦合性高. 所以將業務資料最好放到pro檔案中.
配置 pro 檔案
#默認ISO-8859-1 中文必定亂碼
mysql.username2=mysql資料庫
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.將業務處理之后的結果,為頁面回傳JSON資料.
* @ResponseBody作用: 將資料轉化為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 "你好資料庫:"+ username2 +password2;
}
}
結構

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/302500.html
標籤:java
上一篇:Java階段二:陣列和方法
下一篇:漫畫 | 我在阿里云“淘金”
