最近公司要新開一個項目, 我目前首先考慮用 spring boot
之前項目用到過感覺很方便, 這次從最基礎(chǔ)的搭建開始系統(tǒng)的學(xué)習(xí)
配置java開發(fā)環(huán)境要求
Spring Boot要求JDK1.7以上,Maven3.2以上。
我用的IDEA, JDK1.8, Maven3.5
大家最好不要用eclipse來學(xué)習(xí),intelliJ idea目前很友好的支持SpringBoot
為什么要使用Spring Boot ?
很多公司都是用的SSM框架, 當(dāng)集成了很多功能后 配置變得很繁瑣
可以快速的構(gòu)建項目, 不需要繁瑣的xml配置文件, 內(nèi)置tomcat容器,無需生成war包.
一 .自動構(gòu)建項目
有兩種創(chuàng)建方式 簡單說一下自動構(gòu)建
1. 打開https://start.spring.io/
2. group 是包名 , Artifact 是項目名 , 右邊有個文本框, 可以選擇你需要的依賴
這里我選了3個: Web, mysql 和 mybatis

構(gòu)建項目.png
3. 點擊構(gòu)建項目, 瀏覽器會下載一個項目壓縮包
之前選擇的 Web, mysql 和 mybatis 依賴關(guān)系 會自動添加到pom中
4. 解壓后導(dǎo)入項目到開發(fā)工具IDEA或eclipse中
二 .手動構(gòu)建項目
我比較喜歡手動構(gòu)建
1. 在idea新建maven項目


工程名與選擇保存路徑
2. pom添加以下
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hw</groupId>
<artifactId>SpringBootDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<!--依賴性 從Spring Boot繼承默認(rèn)值 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
</parent>
<!-- 添加典型的依賴于Web應(yīng)用程序 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- 部署為可執(zhí)行的jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- 如果您使用.RELEASE版本,則不需要此操作-->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
3. 接下來打開maven圖形控制面板

下載jar包.png
點擊install就會從配置的遠(yuǎn)程倉庫下載jar包, 放在后臺慢慢下載,需要30分鐘
可以參考我另一篇文章配置 國內(nèi)阿里云加速地址
4. 新建一個類 com.demo.app.Application 添加主方法, 作為springboot開啟的入口

因為是自定義的包 所以要配置 @ComponentScan("com.demo.controller") 來掃描控制器
@ComponentScan 可以用來掃描@Controller、@Service、@Repository、@Component 的類

5. 創(chuàng)建Controller

6. 等jar包全部下載完成后,由于內(nèi)置了tomcat容器, 鼠標(biāo)右鍵運行main方法,打開瀏覽器就能訪問了

本章節(jié)項目github地址:
https://github.com/hweeeeeei/springbootDemo
參考官方文檔地址 :
詳細(xì) https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/
入門 https://spring.io/guides/gs/spring-boot/