第二篇:SpringBoot入门案例(IDEA联网版本)

简介: 第二篇:SpringBoot入门案例(IDEA联网版本)

starter


SpringBoot中常见的项目名称,定义当前项目使用的所有依赖坐标,以达到减少依赖配置的目的。


就比如说web的依赖,只需要引入下面的一个依赖就可以了,这个依赖里面有很多小依赖。


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


点进去之后会看到有很多的小依赖。


<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <!-- This module was also published with a richer model, Gradle metadata,  -->
  <!-- which should be used instead. Do not delete the following line which  -->
  <!-- is to indicate to Gradle or any Gradle module metadata file consumer  -->
  <!-- that they should prefer consuming it instead. -->
  <!-- do_not_remove: published-with-gradle-metadata -->
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.6.3</version>
  <name>spring-boot-starter-web</name>
  <description>Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container</description>
  <url>https://spring.io/projects/spring-boot</url>
  <organization>
    <name>Pivotal Software, Inc.</name>
    <url>https://spring.io</url>
  </organization>
  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>https://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>
  <developers>
    <developer>
      <name>Pivotal</name>
      <email>info@pivotal.io</email>
      <organization>Pivotal Software, Inc.</organization>
      <organizationUrl>https://www.spring.io</organizationUrl>
    </developer>
  </developers>
  <scm>
    <connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
    <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
    <url>https://github.com/spring-projects/spring-boot</url>
  </scm>
  <issueManagement>
    <system>GitHub</system>
    <url>https://github.com/spring-projects/spring-boot/issues</url>
  </issueManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.6.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.6.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.6.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.3.15</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.15</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>


parent


所有的SpringBoot项目都要继承的项目,定义了若干个坐标的版本号(依赖管理,而非依赖),已达到减少依赖冲突的目的。就是在下面协写依赖的时候,有时候可能没有版本号,其实本质上是SpringBoot已经收录了。


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


引导类


0149fc9a93b3402e9fdb0c422d8784ef.png

SpringBoot工程提供引导类来用于启动成程序,就是run这个引导类就可以启动SpringBoot项目了,本质上这个东西就是一个大的Spring容器,然后之后写的bean都会放到这个spring里面的。


tomcat服务器


内嵌Tomcat服务器是SpringBoot辅助功能之一。


创建好springboot项目之后,就可以开始写第一个SpringBoot项目了。


package com.itheima;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
    @GetMapping("/news")
    public String getId() {
        System.out.println("SpringBoot is running");
        return "SpringBoot is running";
    }
}


@RestController


@RestController = @ResponseBody + @Controller合在一起的作用


如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。


例如:本来应该到success.jsp页面的,则其显示success.


如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。


如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。(@Controller + @ResponseBody)


@RequestMapping


cfc9d3300dc247eeac7736c9d65ce1a7.png

@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
@PathVariable

2.png

package com.itheima;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
    @GetMapping("/news")
    public String getId() {
        System.out.println("SpringBoot is running");
        return "SpringBoot is running";
    }
    @GetMapping("{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println(id);
        return id + "";
    }
}


相关文章
|
14天前
|
开发工具 数据中心 git
详解IDEA git 版本回滚
详解IDEA git 版本回滚
20 0
|
15天前
|
SQL Java Maven
idea如何建立一个springboot项目
idea如何建立一个springboot项目
|
2天前
|
缓存 NoSQL Java
案例 采用Springboot默认的缓存方案Simple在三层架构中完成一个手机验证码生成校验的程序
案例 采用Springboot默认的缓存方案Simple在三层架构中完成一个手机验证码生成校验的程序
25 5
|
2天前
|
存储 JSON 自然语言处理
SSMP整合案例交互之在idea中利用vue和axios发送异步请求进行前后端调用
SSMP整合案例交互之在idea中利用vue和axios发送异步请求进行前后端调用
7 2
|
2天前
|
JSON 前端开发 Java
Springboot mvc开发之Rest风格及RESTful简化开发案例
Springboot mvc开发之Rest风格及RESTful简化开发案例
13 2
|
2天前
|
SQL Java 数据库连接
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
12 2
|
19天前
|
IDE Java 项目管理
Java入门——Intellij IDEA简介、使用IDEA开发程序、IDEA常用快捷键、IDEA其他操作
Java入门——Intellij IDEA简介、使用IDEA开发程序、IDEA常用快捷键、IDEA其他操作
20 3
|
19天前
|
Java
springboot和elasticsearch以及springboot data elasticsearch对应的版本
springboot和elasticsearch以及springboot data elasticsearch对应的版本
|
20小时前
|
SpringCloudAlibaba Cloud Native Dubbo
SpringBoot和SpringCloud,SpringCloudAlibaba版本依赖关系
由于 Spring Boot 3.0,Spring Boot 2.7~2.4 和 2.4 以下版本之间变化较大,目前企业级客户老项目相关 Spring Boot 版本仍停留在 Spring Boot 2.4 以下,为了同时满足存量用户和新用户不同需求,社区以 Spring Boot 3.0 和 2.4 分别为分界线,同时维护 2022.x、2021.x、2.2.x 三个分支迭代。如果不想跨分支升级,如需使用新特性,请升级为对应分支的新版本。 为了规避相关构建过程中的依赖冲突问题,我们建议可以通过 云原生应用脚手架 进行项目创建。
13 0
SpringBoot和SpringCloud,SpringCloudAlibaba版本依赖关系
|
1天前
|
Java 测试技术 数据库
【单文件版本】java SpringBoot 切换不同的运行环境(生产环境、开发环境、测试环境)SpringBoot配置多个不同运营环境
【单文件版本】java SpringBoot 切换不同的运行环境(生产环境、开发环境、测试环境)SpringBoot配置多个不同运营环境
8 0