#SpringBoot-新建工程

搭建SpringBoot工程

一、方法一/便捷版

      通过start.spring.io在spring官网上勾选相应的jar输入名称即可生成一个SpringBoot的工程项目,下载导入即可使用

二、方法二/原理版

以IDEA编译器为例
      1、File->New-Project->Maven->(点亮Create from archetype)->①maven-archetype-quickstart(生成zip版本,推荐使用) / ②maven-archetype-webapp(生成war版本)->(以IDEA 2020为例) Artifact Coordinates(GroupId:报名 ,Artifact:工程名,version:版本号)
      2、等待运行环境自动安装即可生成目录结构
      3、配置文件夹属性
            ①src/main/java->右键->Mark Directory as->Sources Root《java的class文件》
            ②在java同级目录下新建文件目录resources,重复上述操作,如果发现没有Resources Root,可在工作空间右上角找到一个类似文件夹的图标(带有积木),点击->Project Settings->Modules->右侧选择对应的文件夹给其赋予Resources的功能即可
            ③test的和上述类似
      4、从零开始继承SpringBoot项目
            a、添加依赖pom.xml
            ①SpringBoot的父级依赖

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

            ②web依赖

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

            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.miaoshaproject</groupId>
  <artifactId>miaosha</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>miaosha</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

            b、修改启动类
            ①将App.class这个原始的启动类修改成SpringBoot Web的启动类,在类上增加@EnableAutoCongfiguration注解@EnableAutoCongfiguration将这个类变成可自动化配置的Bean,并且能够开启基于SpringBoot工程类的自动化配置,实现各种配置文件(jdbc、mybatis等配置文件)自动注入的效果)
            ②实现启动,在main方法中,增加SpringApplication.run(App.class, args),实现web项目的启动

5、验证

      SpringBoot解决SpringMVC的Controller的功能,就是在这个类上增加@RestController注解即可实现,在具体执行类上增加@RequestMapping这个注解,即可实现URL的功能,如下:

package com.miaoshaproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Hello world!
 *
 */
@EnableAutoConfiguration
@RestController
public class App 
{
    @RequestMapping("/")
    public String home(){
        return "Hello world!";
    }

    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class,args);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值