springboot(一)入门

文章封面

一、maven构建springboot

1、需求:

搭建SpringBoot工程,定义HelloController.hello()方法,返回”Hello SpringBoot!”。

2、实现步骤:

  1. 创建Maven项目 springboot-helloworld

  2. 导入SpringBoot起步依赖

<!--springboot工程需要继承的父工程-->
    <parent>
      <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>

    <dependencies>
        <!--web开发的起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  1. 定义Controller编写引导类

    @SpringBootApplication//表示这个类 是springboot主启动类。
    public class HelloApplication {
        public static void main(String[] args) {
            SpringApplication.run(HelloApplication.class,args);
        }
    }
  2. 定义Controller

    @RestController
    @RequestMapping("/hello")
    public class HelloController {
    
        @GetMapping("/test")
        public String test1(){
            return "Hello SpringBoot!";
        }
    }
  3. 启动测试 访问: http://localhost:8080/hello/test

3、总结

  1. 启动springboot一个web工程

  2. pom 规定父工程,导入web的起步依赖

  3. 主启动类 @SpringBootApplication、main

  4. 业务逻辑 controller,service,dao

    • SpringBoot在创建项目时,使用jar的打包方式。 java -jar xxx.jar

    • SpringBoot的引导类,是项目入口,运行main方法就可以启动项目。

    • 使用SpringBoot和Spring构建的项目,业务代码编写方式完全一样。

二、快速构建springboot

image-20220616174853885

image-20220616175045675

image-20220616175453873

image-20220616175515019

三、SpringBoot起步依赖原理分析-理解

  • 在spring-boot-starter-parent中定义了各种技术的版本信息,组合了一套最优搭配的技术版本。
  • 在各种starter中,定义了完成该功能需要的坐标合集,其中大部分版本信息来自于父工程。
  • 我们的工程继承parent,引入starter后,通过依赖传递,就可以简单方便获得需要的jar包,并且不会存在版本冲突等问题。

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0