从零开始-使用IntelliJ IDEA创建SpringBoot项目

栏目: Java · 发布时间: 5年前

内容简介:刚刚开始了解SpringBoot框架,觉得很好用,觉得很有必要深入学习一下该框架,现在就来创建一个SpringBoot项目:​ 在IDEA上新建一个Project,选择Spring Initializr,​ Project SDK 选择安装的JDK;

*注:此文章谨以记录学习过程,分享学习心得!

刚刚开始了解SpringBoot框架,觉得很好用,觉得很有必要深入学习一下该框架,现在就来创建一个SpringBoot项目:

新建项目

1、在IDEA上新建一个Project

​ 在IDEA上新建一个Project,选择Spring Initializr,

​ Project SDK 选择安装的JDK;

​ Choose Initializr Service URL 选择默认(Default: https://start.spring.io)

从零开始-使用IntelliJ IDEA创建SpringBoot项目

点击Next

2、进行项目配置

设置项目数组(group),项目标识(Artifact),Type选择一个Maven Project 表示是一个maven项目

Version:项目版本号

Name:项目名称

Description:项目描述

Package:项目包名

从零开始-使用IntelliJ IDEA创建SpringBoot项目

点击Next 下一步

3、选择项目模板

我们来选择创建一个Web项目

选择Spring Boot版本

从零开始-使用IntelliJ IDEA创建SpringBoot项目

4、设置项目名称和项目路径

从零开始-使用IntelliJ IDEA创建SpringBoot项目 设置完项目路径,和项目名称后,点击FInish,创建项目完成,需要进行项目构建,等一小会即可完成。

5、创建完成,我们删除.mvn文件夹,mvnw文件和 mvnw.cmd文件

从零开始-使用IntelliJ IDEA创建SpringBoot项目

6、maven配置的pom.xml文件

我们来看一下maven配置的pom.xml文件,里面包含了SpringBoot项目运行所需的版本库

从零开始-使用IntelliJ IDEA创建SpringBoot项目

SpringBoot运行所需库为:

<!-- SpringBoot项目的基础库文件-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
<!-- SpringBoot项目的基础库文件-->
    <dependencies>
<!-- web项目库-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!-- 测试所需库-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

7、创建一个HelloService

package com.example.springbootdemo.service;

import org.springframework.stereotype.Service;

@Service
public interface HelloService {
    String sayHello();
}

8、创建HelloService的实现类HelloServiceImpl,实现sayHello()方法,返回”Hello World!”

package com.example.springbootdemo.service.impl;

import com.example.springbootdemo.service.HelloService;
import org.springframework.stereotype.Component;

@Component
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello() {
        return "Hello World!";
    }
}

9、创建HelloController,调用HelloService实现类,打印”Hello World!”到浏览器

package com.example.springbootdemo.controller;

import com.example.springbootdemo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/")
public class HelloController {
    @Autowired
    private HelloService helloService;

    @RequestMapping("/hello")
    @ResponseBody
    public String helloWorld(){
        return helloService.sayHello();
    }
}

10、见证奇迹的时刻,我们来运行一下所建项目,看能不能跟我们预期一样,在浏览器输入访问地址 http://localhost:8080/hello

就可以看到Hello World!

至此,学习创建一个SpringBoot项目就完成了。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C++ Primer 中文版(第 4 版)

C++ Primer 中文版(第 4 版)

Stanley B.Lippman、Josée LaJoie、Barbara E.Moo / 李师贤、蒋爱军、梅晓勇、林瑛 / 人民邮电出版社 / 2006 / 99.00元

本书是久负盛名的C++经典教程,其内容是C++大师Stanley B. Lippman丰富的实践经验和C++标准委员会原负责人Josée Lajoie对C++标准深入理解的完美结合,已经帮助全球无数程序员学会了C++。本版对前一版进行了彻底的修订,内容经过了重新组织,更加入了C++ 先驱Barbara E. Moo在C++教学方面的真知灼见。既显著改善了可读性,又充分体现了C++语言的最新进展和当......一起来看看 《C++ Primer 中文版(第 4 版)》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试