初识SpringBoot
现在到了大三,学业压力不是很大,想为以后就业增强点核心竞争力,所以抽出了点空闲时间来学习学习SpringBoot。鉴于之前也是小白,所以有什么不对的还请大家多指正啦。
关于SpringBoot
SpringBoot是Pivotal公司开发的新框架,设计目的是用来简化新Spring应用的初始搭建以及开发过程,方便工作人员开发业务代码。简单来说,SpringBoot相当于是模板,使用SpringBoot能够快速创建应用。SpringBoot的可复用性特别高,稍作修改就能完全变成另一个服务。
SpringBoot特点
(1)快速构建Spring应用。
(2)无须单独安装tomcat、jetty等容器(内嵌容器)
(3)简化Maven跟Gradle的配置
(4)通过注解自动化配置Spring
(5)可以和其他主流框架无缝集成
(6)可以直接运行jar包
新建SpringBoot项目(使用SpringToolSuite4或Eclipse)
(1)使用Eclipse
使用Eclipse创建SpringBoot项目需要先下载STS插件,打开Eclipse,选择Help->EclipseMarketspace,进入插件下载界面,输入SpringBoot,显示如下
点击install下载红框中的插件,安装完成后重启Eclipse即可开始创建SpringBoot项目了。
创建项目
在Eclipse中,点击File->New->Other,输入Spring Starter,即可看到新建SpringBoot项目,
输入项目名,以及相关信息
点击Next,
选中Web跟MySQL,点击Finish,一个SpringBoot项目就创建好了。
创建完成的项目目录如图所示。
(2)使用SpringToolSuite
Spring官网提供了Spring Tools的IDE,基于Eclipse,https://spring.io/tools可以直接下载。
在SpringToolsSuite4里创建项目基本与在Eclipse中相同,New->File->Spring Starter Project。
SpringBoot项目目录结构
(1)src/main/java 存放java源文件
(2)com.example.demo目录下,Bootdemo2Application.java Spring启动文件,点击运行即可启动项目
(3)src/main/resources目录下存放资源文件
static文件夹存放静态资源,例如图片,css文件,js文件等
template存放模板资源,例如HTML文件等
application.properties,SpringBoot核心配置文件
(4)src跟target文件夹,存放编译后的文件等等
(5)pom.xml,Maven的核心配置文件
pom.xml文件中的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
有三个依赖,其中MySQL跟spring-boot-starter-web依赖是在创建项目时手动添加的,spring-boot-starter-test是创建项目默认添加的单元测试依赖。
关于thymeleaf
SpringBoot不推荐使用JSP,默认使用thymeleaf作为模板。所以要添加thymeleaf依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
新建Animal.java实体类
package com.example.demo.entity;
public class Animal {
private String name;
private String kind;
private String eat;
public Animal() {
super();
// TODO Auto-generated constructor stub
}
public Animal(String name, String kind, String eat) {
super();
this.name = name;
this.kind = kind;
this.eat = eat;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getEat() {
return eat;
}
public void setEat(String eat) {
this.eat = eat;
}
}
新建AnimalController.java
package com.example.demo.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.entity.Animal;
@Controller
public class AnimalController {
@RequestMapping(value="/animal")
public String index(Model model) {
List<Animal> animals = new ArrayList<Animal>();
animals.add(new Animal("喵喵","猫","老鼠"));
animals.add(new Animal("旺财","狗","骨头"));
animals.add(new Animal("泡泡","鱼","水藻"));
model.addAttribute("animals", animals);
return "animal";
}
}
使用@Controller注解注入spring成为控制器,Spring4.3以及以上版本有@GetMapping注解,等价于@RequestMapping(method=RequestMethod.GET),用于简化开发。同理还有@PostMapping,@PutMapping,@DeleteMapping,@PatchMapping。
SpringBoot默认返回thymeleaf视图,所以方法返回animal会自动到template目录下找到animal.html。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Animals</title>
</head>
<body>
<table>
<tr>
<td>名字</td>
<td>种类</td>
<td>食物</td>
</tr>
<tr th:each="animal:${animals}" th:object="${animal}">
<td th:text="${animal.name}"></td>
<td th:text="${animal.kind}"></td>
<td th:text="${animal.eat}"></td>
</tr>
</table>
</body>
</html>
注意要在<html xmlns:th="http://www.thymeleaf.org">中添加xmlns:th="http://www.thymeleaf.org",即thymeleaf的命名空间,有点类似于EL表达式。
然后运行Bootdemo2Application.java。
在浏览器中输入http://localhost:8080/animal,即可访问。
显示如下:
这样一个最简单的SpringBoot项目就完成啦!
(这个项目暂时没有用到MySQL的相关操作),后续再更。