第一章 SpringBoot系列之从0搭建项目

系列文章目录

第一章 SpringBoot系列之从0搭建项目
第二章 SpringBoot系列返回json数据
第三章 SpringBoot系列GlobalException全局异常捕获
第四章 SpringBoot系列整合Mybatis做增删改查
第五章 SpringBoot系列配置JPA访问数据
第六章 SpringBoot系列使用JdbcTemplate操作数据
第七章 SpringBoot系列静态资源处理,访问磁盘文件


前言

使用SpringBoot已经也有两年多了,从一开始对SpringBoot的零认知到现在日常开发必接触的框架,说实话受益良多,其实SpringBoot就是Spring的扩展,以前我们做框架整合以及开发过程中会有大量的配置文件需要配置,而SpringBoot的出现就是把我们从大量配置文件xml中解救出来,不再需要做过多bean配置、DI配置,使用SpringBoot之后只需要集中在application配置文件中做简单属性配置即可,由于SpringBoot内嵌了Tomcat这样还免去了我们安装Tomcat的麻烦,我们只需要运行项目根目录下启动类的main方法即可启动项目,是不是对比以往的项目有没有感觉牛逼plus,今天先说到这,接下来我们学习如何从零搭建SpringBoot项目。


提示:以下是本篇文章正文内容,下面案例可供参考

一、开发工具安装,环境安装准备工作

  1. 开发工具:Eclipse/IntelliJ IDEA(我用的IDEA)
    开发工具可自行去官网下载
  2. JAVA环境:JDK(我用的1.8版本)
    JDK自行去官网下载, window环境变量配置教程.
  3. Jar管理:Maven(我用的IDEA插件maven3)
    也可自行安装maven在开发工具setting中进行配置。

二、开发工具安装Spring帮助插件

1.Eclipse安装Spring Tools4插件

由于我使用的是IntelliJ IDEA开发工具,这里我就不做详细介绍了。

2.IntelliJ IDEA安装Spring Assistant插件

  1. 打开IDEA,单击菜单栏中的“File->Setting->plugins”打开插件窗口。

  2. 在插件窗口搜索“spring”或“Spring Assistant”回车,找到如下图Install安装。(我已经安装过了,所以是按钮是Uninstall)
    在这里插入图片描述

  3. 重启IDEA生效。

三、搭建SpringBoot项目工程

1.使用IDEA中的插件“Spring Assistant”创建项目。

①菜单栏“file->new->project”打开创建项目窗口。
在这里插入图片描述
②默认next
在这里插入图片描述
③根据自己需要更改,next。
在这里插入图片描述
④本次就创建一个web项目所以我们勾选spring web ,然后next。
在这里插入图片描述

⑤Finish。
在这里插入图片描述

2.项目结构。

①下图是使用插件“Spring Assistant”帮我们生成的项目。
在这里插入图片描述

②从①图中可以看出来创建SpringBoot项目还是很简单的。

  1. maven项目pom.xml加入springboot依赖
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.3.4.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>.
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 创建启动类DemoApplication.java,注意启动类一定要创建在代码根目录哦,不然后续加入其他代码会启动报错。
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}
  1. application配置文件,此文件可以放项目使用的一些属性。
    更改项目启动端口,项目全局路径,等等都可以。
  2. 我们可以创建一个Controller控制器,加上@RestController注解,在控制器中加一个test方法,方法加上@GetMapping("/test")
package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @GetMapping("/test")
    public String test() {
        return "Hello World";
    }
}
  1. 项目都创建好了,启动看看实际效果,DemoApplication.java类右键然后Run 'DemoApplication’或Debug 'DemoApplication’启动。
    在这里插入图片描述
    项目默认启动端口是8080,也可在application配置文件自定义,启动日志:
  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _    
( ( )\___ | '_ | '_| | '_ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.4.RELEASE)

2020-10-24 14:19:35.103  INFO 109976 --- [  restartedMain] com.example.demo.DemoApplication         : Starting DemoApplication on wb_lichao001 with PID 109976 
2020-10-24 14:19:35.108  INFO 109976 --- [  restartedMain] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-10-24 14:19:35.236  INFO 109976 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-10-24 14:19:35.236  INFO 109976 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-10-24 14:19:37.408  INFO 109976 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-10-24 14:19:37.426  INFO 109976 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-10-24 14:19:37.427  INFO 109976 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-10-24 14:19:37.579  INFO 109976 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-10-24 14:19:37.580  INFO 109976 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2342 ms
2020-10-24 14:19:37.907  INFO 109976 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-24 14:19:38.088  INFO 109976 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-10-24 14:19:38.145  INFO 109976 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-10-24 14:19:38.164  INFO 109976 --- [  restartedMain] com.example.demo.DemoApplication         : Started DemoApplication in 4.024 seconds (JVM running for 6.96)
  1. 打开浏览器,访问http://localhost:8080/test,成功返回Hello World。
    在这里插入图片描述

总结

今天创建是使用插件的方式来帮我们创建SpringBoot项目,其实我们可以先创建一个maven项目,然后pom.xml加入依赖,创建Application.java启动类(加@SpringBootApplication注解,main方法),新增application配置文件,最终也能创建出一个最简单的springboot项目,今天分享就到这里,希望能够帮助到大家,记得收藏点赞哦!!!有补充/需要都可评论留言,你们的支持就是博主的动力!!!

原文地址:https://www.cnblogs.com/javakfz/p/13938212.html