Intellij IDEA創建Springboot項目-01

1、File -New-Project,彈出新建工程對話框;
在這裏插入圖片描述
2、選擇maven項目,不勾選Create from archetype,
在這裏插入圖片描述
3、下一步,填寫組織名稱、項目名稱以及版本號,版本號可以默認;
在這裏插入圖片描述
4、下一步,默認選擇,即可創建完成;
5、創建Maven成功後,在Pom.xml文件中配置父級依賴:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
</parent>

6、如果要使用spring mvc和spring的功能,就需要在子工程中引入:spring-boot-starter依賴;

<!--引入Spring mvc 和Spring的依賴-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

由於父工程中已經存在版本號,在子工程中使用的時候,不需要加版本;
7、引入jdk版本:

<!--引入jdk版本-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

8、創建一個controller,類的內容如下:

package com.fu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class IndexController {
    @RequestMapping("/")
    @ResponseBody
    public String getData(){
        return "Hello World!";
    }
}

9、創建一個Application類,增加一個main方法,實現Springboot的入口函數;

import org.springframework.boot.SpringApplication;
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

10、開啓自動配置註解:@EnableAutoConfiguration,啓動類和Controler類在同一包下,如果想在不同包下,可以使用如下代碼:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableCaching
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

11、啓動main方法,查看運行結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章