使用 Spring Boot

使用 IEDA 创建一个 Spring Boot 项目

第一步:

image.png

第二步:

image.png

第三步,填写 Group 和 Artifact,一直下一步直到完成创建:

image.png

第四步,删除 src 文件夹,修改 pom 文件:

image.png

pom 文件如下:



    4.0.0

    com.etrols
    spring-boot-study-plan
    0.0.1-SNAPSHOT
    pom

    spring-boot-study-plan
    spring-boot-study-plan project for Spring Boot

    
        UTF-8
        UTF-8
        1.8
    

    
        
            
                io.spring.platform
                platform-bom
                Cairo-SR2
                pom
                import
            

            
                
                org.springframework.boot
                spring-boot-dependencies
                2.0.3.RELEASE
                pom
                import
            
        
    

    

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



第五步,新建一个子模块:

选中项目,右键 --》New --》 Module

image.png
image.png

项目结构如下:


image.png

spring-boot-study-helloword 的pom 文件如下:



    4.0.0

    spring-boot-study-helloword
    jar

    spring-boot-study-helloword
    spring-boot-study-helloword project for Spring Boot

    
        com.etrols
        spring-boot-study-plan
        0.0.1-SNAPSHOT
        ../
    

    

    


在父 pom 文件(spring-boot-study-plan)加入spring-boot-study-helloword



    4.0.0

    com.etrols
    spring-boot-study-plan
    0.0.1-SNAPSHOT
    pom

    spring-boot-study-plan
    spring-boot-study-plan project for Spring Boot

    
        UTF-8
        UTF-8
        1.8
    

    
        spring-boot-study-helloword
    

    
        
            
                io.spring.platform
                platform-bom
                Cairo-SR2
                pom
                import
            

            
                
                org.springframework.boot
                spring-boot-dependencies
                2.0.3.RELEASE
                pom
                import
            
        
    

    

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



Hello Word 程序

Spring Boot 入口 SpringBootStudyHellowordApplication:

package com.etrols;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootStudyHellowordApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootStudyHellowordApplication.class, args);
    }
}

在包 com.etrols.helloword.controller 下新建 HelloController

package com.etrols.helloword.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Hello控制器
 *
 * @author Etrols
 */
@RestController
@RequestMapping("hello")
public class HelloController {

    /**
     * hello
     *
     * @return String
     */
    @GetMapping
    public String hello() {
        return "hello spring boot";
    }

}

启动项目并访问:

image.png

如上图所示,控制台输出 hello spring boot,一个 spring boot 程序就此完成。

项目地址:https://gitee.com/Etrols/spring-boot-study-plan

你可能感兴趣的:(使用 Spring Boot)