idea中两种创建Spring Boot项目的方法

文章介绍了两种创建SpringBoot项目的方法:一种是手动创建maven项目,配置pom.xml,编写启动类和Controller;另一种是通过SpringInitializr自动化创建,选择所需模块。两种方法都会生成包含启动器、依赖和基础结构的项目,可实现相同的功能。
摘要由CSDN通过智能技术生成

创建Spring Boot项目的两种方法

第一种方法:

创建maven项目

1.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.atguigu</groupId>
    <artifactId>spring-boot-01-helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

2.写一个HelloWorldApplication的启动类

package com.atguigu;
 /**
  *SpringBoot启动类
  */
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * SpringBootApplication来标记一个主程序类,说明是一个Spring Boot应用
 */
@SpringBootApplication
public class HelloWorldApplication {
    public static void main(String args[]){
        SpringApplication.run(HelloWorldApplication.class);//让主程序跑起来
    }

}

3.controller下创建一个controller类

package com.atguigu.controller;

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

@Controller
/**
 *这是一个controller类
 */
public class HelloController {
    @RequestMapping("/hello")
    /**
     * RequestMapping接收浏览器hello请求
     */
    @ResponseBody
    /**
     *RequestBody是把Hello World写给浏览器
     **/
    public String hello(){
        return "Hello World";
    }
}

第二种:

直接创建Spring Initializr项目,勾选需要的模块

idea会自动创建好pom.xml文件

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.5</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

注意<version>,如果版本不兼容,会报错,

配置一个启动类  。

package com.atguigu;
 /**
  *SpringBoot启动类
  */
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * SpringBootApplication来标记一个主程序类,说明是一个Spring Boot应用
 */
@SpringBootApplication
public class HelloWorldApplication {
    public static void main(String args[]){
        SpringApplication.run(HelloWorldApplication.class);//让主程序跑起来
    }

}

系统默认在Rerource目录下生成的文件如下:

static:保存所有静态资源:js css images

templates:保存所有模板页面(Spring Boot默认使用嵌入式的Tomcat 默认不支持JSP页面,可以使用模板引擎)

application.properties:Spring Boot的配置文件,可以更改一些默认配置

两个项目都可以达到相同的输出结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值