springBoot2---基础入门

一、配置

1、maven

conf/setting.xml:选择阿里云快一点

<mirrors>
      <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror>
  </mirrors>

  <profiles>
         <profile>
              <id>jdk-1.8</id>
              <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
              </activation>
              <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
              </properties>
         </profile>
  </profiles>

2、系统要求

在这里插入图片描述在这里插入图片描述

二、helloWorld

官方文档:https://spring.io/projects/spring-boot#learn

1、创建maven项目

1.1、引入依赖
<!--用于指定其父工程-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.4</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2、创建主程序

package com.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/*
* @SpringBootApplication:表示这是一个springboot应用
* 也可以有下面这三个注解代替:
* @SpringBootConfiguration
* @EnableAutoConfiguration
* @ComponentScan("com.boot"):括号里面写的是对应的包名
* */
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan("com.boot")
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
    }
}

3、编写业务

package com.boot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/*
* @RequestMapping注释提供“路由”信息。它告诉Spring,任何带有/path的HTTP请求都应该映射到home方法。
* @RestController注释告诉Spring将结果字符串直接呈现给调用者。*/
@RestController
public class helloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello,world.";
    }
}

4、测试

直接运行main方法,
在这里插入图片描述

然后在浏览器中输入:http://localhost:8080/hello

在这里插入图片描述

5、题外话

在springboot中咱们再也不用配置海量的配置文件,仅仅只需要创建一个application.properties文件就够了

需要改什么都可以在里面改

在这里插入图片描述

例如如果要修改端口号的话:

server.port=8888

三、创建可执行的jar

1、添加插件

要创建可执行jar,我们需要将SpringBootMaven插件添加到pom.xml中。要执行此操作,请在dependencies部分下方插入以下行:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2、打包

在这里插入图片描述

也可以在输入命令:mvn package

成功结果:

在这里插入图片描述

打包的文件会放在target目录下

在这里插入图片描述

在这里插入图片描述

然后再cmd中运行,必须在同级目录下

在这里插入图片描述

3、注意点

取消掉cmd的快速编辑模式:因为程序没运行好时,你点击窗口,程序可能会终止

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值