• 02.Spring Boot入门案例


    在这里插入图片描述
    📝 个人主页:程序员阿红🔥

    🎉 支持我:点赞👍收藏⭐️留言📝

    📣 推荐文章:都什么时候了,你还不会框架SSM整合🍁

    一、Spring Boot入门案例

    案例需求:请求Controller中的方法,并将返回值响应到页面

    (1)依赖管理

    <!--
        所用的springBoot项目都会直接或者间接的继承spring-boot-starter-parent
        1.指定项目的编码格式为UTF-8
        2.指定JDK版本为1.8
        3.对项目依赖的版本进行管理,当前项目再引入其他常用的依赖时就需要再指定版本号,避免版本
    冲突的问题
        4.默认的资源过滤和插件管理
        -->
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
      </parent>
      <dependencies>
        <!--引入Spring Web及Spring MVC相关的依赖-->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      </dependencies>
      <!--可以将project打包为一个可以执行的jar-->
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    (2)启动类

    /**
    * SpringBoot的启动类通常放在二级包中,比如:com.lagou.SpringBootDemo1Application
    * 因为SpringBoot项目在做包扫描,会扫描启动类所在的包及其子包下的所有内容。
    */
    //标识当前类为SpringBoot项目的启动类
    @SpringBootApplication
    public class SpringBootDemo1Application {
      public static void main(String[] args) {
        //样板代码
        SpringApplication.run(SpringBootDemo1Application.class,args);
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    (3)Controller

    package com.lagou.controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    @RestController
    @RequestMapping("/hello")
    public class HelloController {
      @RequestMapping("/boot")
      public String helloBoot(){
        return "Hello Spring Boot";
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    二、 SpringBoot 快速构建

    案例需求:请求Controller中的方法,并将返回值响应到页面

    (1)使用Spring Initializr方式构建Spring Boot项目

    本质上说,Spring Initializr是一个Web应用,它提供了一个基本的项目结构,能够帮助我们快速构建一个基础的Spring Boot项目

    image-20220621235427137

    image-20220621235439005

    Project SDK”用于设置创建项目使用的JDK版本,这里,使用之前初始化设置好的JDK版本即可;在“Choose Initializr Service URL(选择初始化服务地址)”下使用默认的初始化服务地址“https://start.spring.io”进行Spring Boot项目创建(注意使用快速方式创建Spring Boot项目时,所在主机须在联网状态下)

    image-20220621235515803

    image-20220621235523809

    Spring Boot项目就创建好了。创建好的Spring Boot项目结构如图:

    image-20220621235553994

    使用Spring Initializr方式构建的Spring Boot项目会默认生成项目启动类、存放前端静态资源和页面的文件夹、编写项目配置的配置文件以及进行项目单元测试的测试类

    (2) 创建一个用于Web访问的Controller

    com.lagou包下创建名称为controller的包,在该包下创建一个请求处理控制类HelloController,并编写一个请求处理方法 (注意:将项目启动类SpringBootDemoApplication移动到com.lagou包下)

    @RestController // 该注解为组合注解,等同于Spring中@Controller+@ResponseBody注解
    public class DemoController {
     @RequestMapping("/demo")
      public String demo(){
       return "hello spring Boot";
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    (3)运行项目

    运行主程序启动类SpringbootDemoApplication,项目启动成功后,在控制台上会发现SpringBoot项目默认启动的端口号为8080,此时,可以在浏览器上访问“http://localhost:8080/hello”

    image-20220621235758745

    💖💖💖 完结撒花

    💖💖💖 路漫漫其修远兮,吾将上下而求索

    💖💖💖 写作不易,如果您觉得写的不错,欢迎给博主点赞、收藏、评论来一波~让博主更有动力吧

  • 相关阅读:
    node.js学习笔记 10包管理器
    Python之第八章 模块和包 --- 模块
    软考信息系统项目管理师_历年真题_2019上半年错题集_上午综合知识题---软考高级之信息系统项目管理师054
    R语言向前或者向后移动时间序列数据(自定义滞后或者超前的期数):使用lag函数将时间序列数据向后移动一天(设置参数k为负值)
    栈和队列 OJ题
    Workfine新手入门:分级预览
    搭建和mybatis-plus官网一样主题的网站(cos+宝塔+vercel)
    select的测试demo
    qsort 函数的使用
    LeetCode: 2. 两数相加
  • 原文地址:https://blog.csdn.net/qq_41239465/article/details/125401077