SpringBoot2入门学习


SpringBoot2基础入门

环境设置

  • Java8以上
  • Maven3.3以上
  • Idea 2020.1

Maven 3.6下载

二、使用步骤

Maven配置阿里云镜像和JDK1.8

配置阿里云镜像,项目下载jar包等速度会更快,效率更高。
<mirrors></mirrors>标签中加入以下代码:

<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>

第一个SpringBoot程序

pom.xml中添加以下代码

<!--导入父工程-->
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
 </parent>

    <dependencies>
    <!-- web场景启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

MainApplication.java


/**
 * ResponseBody代码该类的每一个方法的返回数据直接写给浏览器的,而不是跳转到页面
 */
//@ResponseBody
//@Controller

/**
 * @RestController中包含了@ResponseBody和@Controller
 */
@RestController
public class HelloController {
    /**
     * @RequestMapping 映射请求,希望浏览器中发射hello请求,处理
     * @return给浏览器返回hello,springboot
     */
    @RequestMapping("/hello")
    public String hand01(){
        return "Hello,SpringBoot";
    }
}

HelloController.java

/**
 * ResponseBody代码该类的每一个方法的返回数据直接写给浏览器的,而不是跳转到页面
 */
//@ResponseBody
//@Controller

/**
 * @RestController中包含了@ResponseBody和@Controller
 */
@RestController
public class HelloController {
    /**
     * @RequestMapping 映射请求,希望浏览器中发射hello请求,处理
     * @return给浏览器返回hello,springboot
     */
    @RequestMapping("/hello")
    public String hand01(){
        return "Hello,SpringBoot";
    }
}

run MainApplication,运行成功,显示Tomcat端口号8080
run MainApplication

修改Tomcat端口号

在resources中新建application.properties配置文件,写入
server.port=8888
在这里插入图片描述
run MainApplication,显示Tomcat端口号已修改成8888了

run MainApplication
在这里插入图片描述

创建可执行Jar包

在pom.xml文件中添加以下插件代码

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

clean清理—>package打包
在这里插入图片描述
打包成功
右击target—>show in Explorer在文件夹中显示
在这里插入图片描述
进入Target目录,在上面输入cmd
在这里插入图片描述
在打开的cmd窗口中输入

java -jar boot[按Tab键]

springboot应用正在启动…
在这里插入图片描述

依赖管理

依赖管理    
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
</parent>
鼠标放在spring-boot-starter-parent上面,按ctrl再点击,可进入下面的页面

他的父项目
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.4.RELEASE</version>
  </parent>

几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值