搭建 springboot 應用
一、采用 Spring Initializr 搭建springboot應用
步驟:
1、只需要打開網址: https://start.spring.io/;
2、選擇使用 Maven 構建,語言 java,Spring Boot 版本 2.0.4,group 為 com.shiyanlou,artifact 為 springboot,依賴我們選擇 web。
點擊 Generate Project,我們會得到一個 springboot.zip 的壓縮包。
3、使用 Spirng Boot 的 maven 插件啟動(運行 mvn spring-boot:run)
二、手動構建 Spring Boot 項目
步驟:
1、運行:mvn archetype:generate
mvn archetype:generate \
-DgroupId=com.shiyanlou \
-DartifactId = springboot \
-DarchetypeArtifactId = maven-archetype-quickstart
創建過程中會提示輸入版本號等,直接使用默認即可。接著輸入 Y 確定創建。
2、添加 maven 依賴,修改 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>
<groupId>com.shiyanlou</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<!--設置父模塊 這樣就可以繼承父模塊中的配置信息-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--添加web依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--spirng Boot maven插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、在包 com.shiyanlou.springboot 中建立 SpringbootApplication.java,代碼如下:
package com.shiyanlou.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// @SpringBootApplication 注解相當于同時使用 @EnableAutoConfiguration、@ComponentScan、@Configurations 三個注解
// @EnableAutoConfiguration 用于打開 Spring Boot 自動配置,而其余注解為 Spring 注解,這里不再贅述
@SpringBootApplication
public class SpringbootApplication{
public static void main(String[] args){
SpringApplication.run(SpringbootApplication.class, args);
}
}
4、打開 terminal,輸入:mvn spring-boot:run;
三、先命令行創建mvn項目,在加入spring-boot依賴。
1. 命令行 mvn archetype:generate -DgroupId=com.shiyanlou -DartifactId=redis -DarchetypeArtifactId=maven-archetype-quickstart
一直回車就好了。
2. porm文件添加依賴:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.shiyanlou</groupId>
<artifactId>redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis</name>
<description></description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. 完成 o(n_n)o;

浙公網安備 33010602011771號