第一个SpringBoot程序的创建
-
BootSchool这个网站里面有好多有趣的页面和注释大家有时间了可以去玩一玩
-
第一种呢就是去官网直接下载后导入idea开发(这种呢一般不常用)
-
也可以直接去idea中去创建一个SpringBoot项目(这中方式呢更加的方便)
项目结构(虽然你自己创建一个SpringBoot会生成但是我还是给大家拿过来)
- 项目创建步骤
- 上面的一些选项呢是你项目的名字和JDK的一个版本最后一个呢留意一下是里面包的一个结构所以一般都要去改动这个结构去选则你自己的结构自动生成的结构可能会有点长。
- controller这个包呢是我们创建的还创建了HelloController类
package com.cloud.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/hello")
public class HelloController {
//接口:http://localhost:8080/hello/hello
@GetMapping("/hello")
@ResponseBody
public String hello(){
return "hello";
}
}
//这个代码呢就不做过多的解释了这个呢就想当于一个重定向就是一个接口会跳转页面并返回一个hello
- application.properties这个呢是人家生成的用来写一些SpringBoot的核心配置文件
#SpringBoot的核心配置文件
#更改项目的端口号
server.port=8081
- banner.txt这个也是我们自己写的也给大家拿过来这个其实是个小趣味就是把人家的一些东西干掉了留给大家去发现
$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$
$$$$$$$$$$$$$
$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$ $$$$$$$$$$$$$$
$$$$ $$$$$$$$$$$
$$$$ $$$$$$$$$$
$$$$ $$$$$$$$$$
$$$$ $$$$$$$$$$
$$$ $$$$$$$$$
$$$ $$$$$$$$$$
$$$$$$$$$$$$$$
$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$ $$$$$$$$$$$$$
$$$$$$$$ $$$$$$$$$$$
$$$$$$$$ $$$$$$$$$$$
$$$$$$$ $$$$$$$$$$
$$$$$$$ $$$$$$$$$$
$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$
$$$$$$
$$$$$$
$$$$$$
$$$$$$
$$$$$
$$$$$
$$$$
$$$$
$$$$
$$$$$
$$$$$
$$$$
$$$$$
$$$$
- Springboot01HelloworldApplicationTests这是一个自动生成的测试类
package com.cloud;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
//单元测试
@SpringBootTest
class Springboot01HelloworldApplicationTests {
@Test
void contextLoads() {
}
}
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--它呢是有一个父项目的-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cloud</groupId>
<artifactId>springboot-01-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-01-helloworld</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--相当于一个启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--web依赖:tomcat,dispatcherServlet,xml....-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--spring-boot-starter所有的springboot依赖都是以这个开头的-->
<build>
<!--打jar包插件-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这个呢给大家重点讲一下这几个依赖第一个呢就相当于一个依赖启动器,第二个依赖呢就是web依赖它做了一个上面操作呢它把一些tomcat还有web.xml里面的配置都做了比ssm更加的简化了大大减少了大家书写代码的行数最后呢一个是jar包创建和单元测试依赖,还有呢项目结构呢一定要根据我的项目结构创建因为虽然它虽然是更加方便了但自由度却降低了里面存在一些规则。