创建springboot工程模板

433 阅读1分钟

目录
1.使用Maven-archetype来创建项目
2.设置工程包路径和工程名 3.创建java目录和resources目录
4.pom中添加spring依赖
5.创建包和启动类
6.编写controller层测试类
7.运行启动类
8.启动成功,测试验证


1.使用Maven-archetype来创建项目
新建maven项目。File>New>Project>Maven>Create from archetype>Maven-archetype-webapp>Next 20200207164916249.jpg 完成后的效果 image.png 建立Maven项目时,有两种常用的原型,这两种分别是: 1、maven-archetype-quickstart: 创建一个纯后台程序,没有前端时使用。最后编译生成jar包。 2、maven-archetype-webapp: 创建一个带前端代码的web应用时使用。最后编译生成war包

2.设置工程包路径和工程名

image.png 点击下一步后,完成项目创建

image.png

3.创建java目录和resources目录
使用IDEA在src新建目录时,下方会自动出现提示来创建maven的source目录,我们只需要选中创建即可。

image.png

4.pom中添加spring依赖

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

<dependencies>
    <!-- 包含mvc aop等jar包 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

5.创建包和启动类

package com.flyfish;

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

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

6.编写controller层测试类

package com.flyfish.controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/my")
public class MyController {
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index(Model model, HttpServletRequest httpServletRequest) {
        return "hello,this is index";
    }
}

7.运行启动类
image.png

8.启动成功,测试验证

image.png