Build Anything with Spring Boot:Spring Boot is the starting point for building all Spring-based applications. Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring.css
上面是引自官网的一段话,大概是说: Spring Boot 是全部基于 Spring 开发的项目的起点。Spring Boot 的设计是为了让你尽量快的跑起来 Spring 应用程序而且尽量减小你的配置文件。html
回顾咱们以前的 SSM 项目,搭建过程仍是比较繁琐的,须要:java
而使用 Spring Boot 来开发项目则只须要很是少的几个配置就能够搭建起来一个 Web 项目,而且利用 IDEA 能够自动生成生成,这简直是太爽了...mysql
选择 Spring Initializr ,而后选择默认的 url 点击【Next】:web
而后修改一下项目的信息:spring
勾选上 Web 模板:sql
选择好项目的位置,点击【Finish】:typescript
若是是第一次配置 Spring Boot 的话可能须要等待一下子 IDEA 下载相应的 依赖包,默认建立好的项目结构以下:数据库
项目结构仍是看上去挺清爽的,少了不少配置文件,咱们来了解一下默认生成的有什么:apache
在【cn.wmyskxz.springboot】包下新建一个【HelloController】:
package cn.wmyskxz.springboot; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 测试控制器 * * @author: @我没有三颗心脏 * @create: 2018-05-08-下午 16:46 */ @RestController public class HelloController { @RequestMapping("/hello") public String hello() { return "Hello Spring Boot!"; } }
咱们回到 SpringbootApplication 这个类中,而后右键点击运行:
等待一下子就会看到下方的成功运行的提示信息:
能够看到咱们的 Tomcat 运行在 8080 端口,咱们来访问 “/hello
” 地址试一下:
能够看到页面成功显示出咱们返回的信息。
让咱们来看看默认生成的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.wmyskxz</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
咱们能够看到一个比较陌生一些的标签 <parent>
,这个标签是在配置 Spring Boot 的父级依赖:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
有了这个,当前的项目才是 Spring Boot 项目,spring-boot-starter-parent 是一个特殊的 starter ,它用来提供相关的 Maven 默认依赖,使用它以后,经常使用的包依赖就能够省去 version 标签。
关于具体 Spring Boot 提供了哪些 jar 包的依赖,咱们能够查看本地 Maven 仓库下:\repository\org\springframework\boot\spring-boot-dependencies\2.0.1.RELEASE\spring-boot-dependencies-2.0.1.RELEASE.pom 文件来查看,挺长的...
Spring Boot 项目一般有一个名为 *Application 的入口类,入口类里有一个 main 方法, 这个 main 方法其实就是一个标准的 Javay 应用的入口方法。
@SpringBootApplication 是 Spring Boot 的核心注解,它是一个组合注解,该注解组合了:@Configuration、@EnableAutoConfiguration、@ComponentScan; 若不是用 @SpringBootApplication 注解也可使用这三个注解代替。
Spring Boot 使用一个全局的配置文件 application.properties 或 application.yml,放置在【src/main/resources】目录或者类路径的 /config 下。
Spring Boot 不只支持常规的 properties 配置文件,还支持 yaml 语言的配置文件。yaml 是以数据为中心的语言,在配置数据的时候具备面向对象的特征。
Spring Boot 的全局配置文件的做用是对一些默认配置的配置值进行修改。
- 简单实例一下
咱们一样的将 Tomcat 默认端口设置为 8080 ,并将默认的访问路径从 “/
” 修改成 “/hello
” 时,使用 properties 文件和 yml 文件的区别如上图。
:
” 后加一个空格,幸亏 IDEA 很好地支持了 yml 文件的格式有良好的代码提示;
- 咱们能够本身配置多个属性
咱们直接把 .properties 后缀的文件删掉,使用 .yml 文件来进行简单的配置,而后使用 @Value 来获取配置属性:
重启 Spring Boot ,输入地址:localhost:8080/hello 能看到正确的结果:
你也能够在配置文件中使用当前配置:
仍然能够获得正确的结果:
- 封装配置信息
咱们能够把配置信息封装成一个类,首先在咱们的 name 和 age 前加一个 student 前缀,而后新建一个 StudentProperties 的类用来封装这些信息,并用上两个注解:
这样咱们就能够在控制器中使用,重启获得正确信息:
在目前的 Spring Boot 项目中,当发生了任何修改以后咱们都须要从新启动才可以正确的获得效果,这样会略显麻烦,Spring Boot 提供了热部署的方式,当发现任何类发生了改变,就会经过 JVM 类加载的方式,加载最新的类到虚拟机中,这样就不须要从新启动也能看到修改后的效果了。
- 作法也很简单,修改 pom.xml 便可!
咱们往 pom.xml 中添加一个依赖就能够了:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 这个须要为 true 热部署才有效 --> </dependency>
从新启动 Spring Boot ,而后修改任意代码,就能观察到控制台的自动重启现象:
关于如何在 IDEA 中配置热部署:传送门
上面已经完成了 Spring Boot 项目的简单搭建,咱们仅仅须要进行一些简单的设置,写一个 HelloController 就可以直接运行了,不要太简单...接下来咱们再深刻了解一下 Spring Boot 的使用。
Spring Boot 的默认视图支持是 Thymeleaf 模板引擎,可是这个咱们不熟悉啊,咱们仍是想要使用 JSP 怎么办呢?
- 第一步:修改 pom.xml 增长对 JSP 文件的支持
<!-- servlet依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat的支持.--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
- 第二步:配置试图重定向 JSP 文件的位置
修改 application.yml 文件,将咱们的 JSP 文件重定向到 /WEB-INF/views/ 目录下:
- 第三步:修改 HelloController
修改 @RestController 注解为 @Controller ,而后将 hello 方法修改成:
- 第四步:新建 hello.jsp 文件
在【src/main】目录下依次建立 webapp、WEB-INF、views 目录,并建立一个 hello.jsp 文件:
- 第五步:刷新网页
由于咱们部署了热部署功能,因此只须要等待控制台重启信息完成以后再刷新网页就能够看到正确效果了:
- 第一步:修改 pom.xml 增长对 MySql和 MyBatis 的支持
<!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency>
- 第二步:新增数据库连接参数
这里咱们就直接使用以前建立好的 student 表了吧:
- 第三步:建立 Student 实体类和 StudentMapper 映射类
在【cn.wmyskxz.springboot】下新建一个【pojo】包,而后在其下建立一个 Student 类:
public class Student { private Integer id; private Integer student_id; private String name; private Integer age; private String sex; private Date birthday; /* getter and setter */ }
在【cn.wmyskxz.springboot】下新建一个【mapper】包,而后在其下建立一个 StudentMapper 映射类:
package cn.wmyskxz.springboot.mapper; import cn.wmyskxz.springboot.pojo.Student; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface StudentMapper { @Select("SELECT * FROM student") List<Student> findAll(); }
- 第四步:编写 StudentController
在【cn.wmyskxz.springboot】下新建一个【controller】包,而后在其下建立一个 StudentController :
package cn.wmyskxz.springboot.controller;
import cn.wmyskxz.springboot.mapper.StudentMapper; import cn.wmyskxz.springboot.pojo.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; /** * Student 控制器 * * @author: @我没有三颗心脏 * @create: 2018-05-08-下午 20:25 */ @Controller public class StudentController { @Autowired StudentMapper studentMapper; @RequestMapping("/listStudent") public String listStudent(Model model) { List<Student> students = studentMapper.findAll(); model.addAttribute("students", students); return "listStudent"; } }
第五步:编写 listStudent.jsp 文件
咱们简化一下 JSP 的文件,仅显示两个字段的数据:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <table align='center' border='1' cellspacing='0'> <tr> <td>id</td> <td>name</td> </tr> <c:forEach items="${students}" var="s" varStatus="st"> <tr> <td>${s.id}</td> <td>${s.name}</td> </tr> </c:forEach> </table>
- 第六步:重启服务器运行
由于往 pom.xml 中新增长了依赖的包,因此自动重启服务器没有做用,咱们须要手动重启一次,而后在地址输入:localhost:8080/listStudent 查看效果:
以上。