Springboot創(chuàng)建時(shí)常用的依賴詳解
新建SpringBoot Maven項(xiàng)目中pom常用依賴配置及常用的依賴
1.springboot項(xiàng)目的總(父)依賴大全
<parent>
<artifactId>spring-boot-dependencies</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.3.RELEASE</version>
</parent>當(dāng)我們使用 spring 或 spring-boot 開發(fā)項(xiàng)目時(shí),需要引入很多依賴,包括 spring 本身的組件、各種 spring-boot-starter、以及其它第三方依賴(如:slf4j、redis)。依賴多了,版本的選擇是個(gè)問題,就怕哪個(gè)版本選擇的不對(duì)導(dǎo)致出現(xiàn)一些意想不到的 BUG。
spring-boot-dependencies的作用主要是起到約束版本的作用,在這個(gè)包里面聲明了各種版本號(hào),供子項(xiàng)目去引用。類似spring-cloud-dependencies和spring-cloud-alibaba-dependencies則是去聲明cloud和cloud-alibaba組件的版本。具體有些什么可以點(diǎn)進(jìn)去看看就知道了。如果當(dāng)下面的< dependency >中用到就可以不用配置版本號(hào)< version >
2.可執(zhí)行的 Web 應(yīng)用且內(nèi)含SpringBoot核心啟動(dòng)器
包含各種springboot的配置日志等,創(chuàng)建項(xiàng)目時(shí)會(huì)自動(dòng)引入該依賴
- 支持注解:@controller、@Service、@Component、@Resource 是spring的,所以spring boot創(chuàng)建完成后就可以使用(由spring-boot-starter支持)
- 支持注解:@RestController、@RequestMapping、@ResponseBody、@JsonFormat(由spring-boot-starter-web支持)
<!--Spring Boot Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>spring-boot-starter-web 是什么?
spring-boot-starter-web是一個(gè)依賴庫(kù),Spring Boot 是在 Spring 的基礎(chǔ)上創(chuàng)建的一個(gè)開原框架,它提供了 spring-boot-starter-web (web場(chǎng)景啟動(dòng)器)來為web開發(fā)予以支持。spring-boot-starter-web 提供了嵌入的Servlet容器以及SpringMVC提供了大量自動(dòng)配置,可以適用于大多數(shù)web開發(fā)場(chǎng)景。
只要我們?cè)赟pring Boot 項(xiàng)目中的 pom.xml 中引入了spring-boot-starter-web依賴,即使不進(jìn)行任何配置,也可以使用Spring MVC 進(jìn)行 Web 開發(fā)。Spring Web的啟動(dòng)程序使用Spring MVC, REST和Tomcat作為默認(rèn)的嵌入式服務(wù)器。單個(gè)spring-boot-starter-web依賴關(guān)系可傳遞地獲取與Web開發(fā)相關(guān)的所有依賴關(guān)系。它還減少了構(gòu)建依賴項(xiàng)計(jì)數(shù)。
配置了該依賴就不用再配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>因?yàn)閟pring-boot-starter-web包含了spring-boot-starter等,可以點(diǎn)進(jìn)去看看
3.junit測(cè)試,創(chuàng)建項(xiàng)目時(shí)會(huì)自動(dòng)引入該依賴
用于編寫springboot Test測(cè)試類
SpringBoot Test測(cè)試類的使用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>4.mysql數(shù)據(jù)配置
配置mysql依賴時(shí),不寫版本號(hào)xx.xx.xx的話,就會(huì)引入mysql依賴的默認(rèn)版本
- SpringBoot2.1.x以后默認(rèn)使用的是mysql 8版本,
- SpringBoot2.1.x之前默認(rèn)使用的是mysql 5.x版本
在配置數(shù)據(jù)源的時(shí)候,就有差異了:
- 配置低版本 5.xx.xx:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456
- 配置高版本 8.xx.xx:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
<!--MySQL 連接組件-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>5.mybatis
數(shù)據(jù)處理層持久層框架,連接數(shù)據(jù)庫(kù)
著重點(diǎn)放在了編寫sql上,而不是通過jdbc傳統(tǒng)方式來不斷操作Connection、Statment、ResultSet
注解@Mapper 指定映射接口
application.yaml配置文件中配置自動(dòng)識(shí)別的xml:
mybatis:
mapper-locations: classpath:mapper/**/*.xml type-aliases-package: run.leave.mapper
<!--MyBaits-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>8.Druid連接池
<!--Druid-->
<!--可以不配這個(gè)因?yàn)閐ruid-spring-boot-starter里面已經(jīng)有了,隨便帶著一下這個(gè)依賴,代碼可讀性高一點(diǎn),反正對(duì)其他啥也沒影響-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<!-- Druid Spring Boot 組件-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>在yaml文件中配置使用:
spring:
datasource:
# 數(shù)據(jù)源基本配置
url: jdbc:mysql://localhost:3306/hotel?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# 數(shù)據(jù)源其他配置
druid:
# 配置初始化大小、最小、最大線程數(shù)
initialSize: 5
minIdle: 5
# CPU核數(shù)+1,也可以大些但不要超過20,數(shù)據(jù)庫(kù)加鎖時(shí)連接過多性能下降
maxActive: 20
# 最大等待時(shí)間,內(nèi)網(wǎng):800,外網(wǎng):1200(三次握手1s)
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
# 配置一個(gè)連接在池中最大空間時(shí)間,單位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
# 設(shè)置從連接池獲取連接時(shí)是否檢查連接有效性,true檢查,false不檢查
testOnBorrow: true
# 設(shè)置從連接池歸還連接時(shí)是否檢查連接有效性,true檢查,false不檢查
testOnReturn: true
# 可以支持PSCache(提升寫入、查詢效率)
poolPreparedStatements: true
# 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),'wall'用于防火墻
filters: stat,wall,log4j
# 保持長(zhǎng)連接
keepAlive: true
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5007.Json格式轉(zhuǎn)換工具Fastjson
Fastjson 是一個(gè) Java 庫(kù),可以將 Java 對(duì)象轉(zhuǎn)換為 JSON 格式,當(dāng)然它也可以將 JSON 字符串轉(zhuǎn)換為 Java 對(duì)象。
Java中 Json、String、jsonObject、jsonArray格式之間互相轉(zhuǎn)換
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>8.lombook
lombok最優(yōu)秀的就是注解了,一個(gè)注解就干掉了很多代碼
實(shí)體類中的注解.
- @Data :直接可以省略了Get、Set方法
- @Slf4j :不需要單獨(dú)引入日志依賴和配置日志,直接 log.info( ) 打印日志
如何在IDE編譯器 中使用lombok插件?
- idea中可以直接在編譯器中搜索下載,就不多闡述了
- eclipse則需要從官網(wǎng)下載lombok.jar包,然后雙擊啟動(dòng)jar包,逐步操作,指向eclisp.exe,重啟eclipse即可
<!--LomBok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>9.面向切面編程AOP
支持的注解:@AspectJ、@Pointcut、通知注解(如:@Before、@After等)、@Aspect和自定義注解
- spring-boot-starter-aop及其使用場(chǎng)景說明
- SpringBoot 中的 Aop 注解使用+ 自定義注解
<!--Spring Boot Aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>10.Validation校驗(yàn)參數(shù)的實(shí)現(xiàn)
支持的注解:@Max,@Min等
常用注解和demo
<!--Spring Validation-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>11.Actuator 監(jiān)控
主要是服務(wù)器運(yùn)維使用,開發(fā)過程不常用
springboot 監(jiān)控 Actuator 的設(shè)置
<!--Spring Boot Actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>12.hutool工具包
提供了很多封裝方法供開發(fā)者使用
<!--Hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.7</version>
</dependency>13.jupiter
其依賴包含了junit-jupiter-api、junit-jupiter-engine、junit-vintage-engine
<!--Junit-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>14.打包配置
用于生成部署到服務(wù)器的包
JAVA項(xiàng)目在服務(wù)器部署過程
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>15.多yaml文件配置
指定其使用那個(gè)文件,不配置下面的profiles,但創(chuàng)建的文件格式形如這樣也是可用的
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profilesActive>dev</profilesActive>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<profilesActive>pro</profilesActive>
</properties>
</profile>
</profiles>16.使用properties標(biāo)簽統(tǒng)一編碼和JAVA版本
<!--統(tǒng)一編碼和JAVA版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>17.mybatis-plus
在mybatis基礎(chǔ)上的升級(jí)版工具,避免了使用mybatis時(shí)需要編寫大量的xml文件
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>18.springboot熱部署
修改java代碼后,不用重啟項(xiàng)目就能直接最新測(cè)試,省略了不斷修改代碼不斷重啟項(xiàng)目的麻煩
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot項(xiàng)目中使用?TrueLicense?生成和驗(yàn)證License的詳細(xì)步驟
這篇文章主要介紹了Spring?Boot項(xiàng)目中使用?TrueLicense?生成和驗(yàn)證License,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10
Hibernate雙向多對(duì)多映射關(guān)系配置代碼實(shí)例
這篇文章主要介紹了Hibernate雙向多對(duì)多映射關(guān)系配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
mybatis-plus與mybatis共存的實(shí)現(xiàn)
本文主要介紹了mybatis-plus與mybatis共存的實(shí)現(xiàn),文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

