SpringBoot项目的创建

(git已经之前学习过)
springboot官方网站的简介如下:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.

SpringBoot相比Spring免去了复杂的xml文件配置,特点是:

  • 创建独立的Spring应用
  • 直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)
  • 提供独立的“入门”依赖项,以简化构建配置
  • 尽可能自动配置Spring和第三方库
  • 提供可用于生产的功能,例如指标,运行状况检查和外部化配置
  • 完全没有代码生成,也不需要XML配置

下面建立一个SpringBoot项目:

1 打开idea,新建项目:

选择Spring Initializer,选next
在这里插入图片描述
输入组织名和项目名,java version 一般为8
在这里插入图片描述
接下来在依赖中搜索要用到的依赖。或者在分类中选择,我们选择这四个依赖。
在这里插入图片描述
点击next,finish,项目自动生成

编写配置文件

这里主要先是数据库的配置信息。

spring:
  datasource:
    driver-class-name:
      url: jdbc:mysql://localhost:3306/sqltest?useSSL=true&characterEncoding=utf-8&ServerTimezone=Asia/Shanghai
      username: root
      password: 123456
    thymeleaf:
      mode: HTML
    profiles:
      active: dev

到这里直接启动会出现数据库连接错误,因为SpringBoot用了内置的数据库驱动,在Application中添加一行注释取消自动配置数据库

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class NewsApplication {
    public static void main(String[] args) {
        SpringApplication.run(NewsApplication.class, args);
    }
}

前端显示

在templates包中新建first.html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>第一页</title>
</head>
<body>
欢迎
</body>
</html>

编写web层

在首页地址返回first.html页面

@Controller
public class IndexController {
    @GetMapping("/")
    //@RequestMapping(method = RequestMethod.GET,value = "/")
    public String index(){
        return "first";
    }
}

运行

在这里插入图片描述
浏览器进入localhost:8080
在这里插入图片描述

这样一个简单的SpringBoot项目就创建完成了

猜你喜欢

转载自blog.csdn.net/weixin_42189888/article/details/107603826