1.IDEA歡迎頁面:Create New Project
2.選擇Maven選項,右側是選擇當前項目的JDK,單擊next進入下一步
3.分別填寫Groupid(項目組織唯一標識符),Artifactidt(項目唯一標識符)和Version(項目版本號)
4.指定項定名稱,默認會使用ArtifactID的內容,設置項目存儲路徑project location
5.項目創建完成後,會默認打開創建Maven項目生成的pom.xml依賴文件,同時在右下角會彈出“Maven projects need to be imported”(需要導入maven依賴)的提示框
說明:import changes:表示導入版本變化,只會導入本次變化的依賴
Enable Auto-Import:表示開啓自動導入,後期會持續監測變化的依賴。這裏選擇此項,一旦pom.xml文件發生更改,依賴會自動導入。至此,完成了一個空的Maven項目。
添加Spring Boot相關依賴
6.在pom.xml中添加Spring Boot相關依賴,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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 引入Spring Boot依賴 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<groupId>com.itheima</groupId>
<artifactId>manual_chapter01</artifactId>
<version>1.0-SNAPSHOT</version>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 選擇的Web模塊依賴啓動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
7.編寫主程序啓動類
在項目的SRC-MAIN-JAVA目錄下創建一個名稱爲com.itheima的包,在該包下新建一個主程序啓動類ManualChapter01Application,內容如下:
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication //是Spring Boot框架的核心註解,表明ManualChapter01Application爲爲主程序啓動類
public class Manualchapter01Application {
public static void main(String[] args) {
SpringApplication.run(Manualchapter01Application.class, args);//調用SpringApplication.run()方法啓動主程序類
}
8.創建一個用於Web訪問的Controller
-在com.itheima包下創建名稱爲Controller的包,在該包下創建一個名稱爲HelloController的請求處理控制類,並編寫一個請求處理方法,內容如下:
package com.itheima.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController //該註解爲組合註解,等同於spring中的@Controller+@ResponseBody註解
public class HelloController {
@GetMapping("/hello") //該註解等現於spring中的@RequestMapping(RequestMethod.GET)
public String hello(){
return "Hello Spring Boot!";
}
9.運行項目(需要完成相關插件下載)後運行
在主程序啓動類點運行按鈕
在瀏覽器上訪問:http://localhost:8080/hello, 效果如下圖
擴展:IntelliJ IDEA可以自動優化導入包,但是有多個同名的類位於不同的包時,需要自己手動使用Alt + Enter進行導入。方法如下:
Settings→Editor→General→Auto Import
選中Optimize imports on the fly(自動去掉一些沒有用到的包)和Add unambiguous imports on the fly(自動幫我們優化導入的包)
————————————————
版權聲明:本文爲CSDN博主「qq_26665293」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_26665293/article/details/104391095