一,概述
springboot 的本质还是spring,他们之间的区别就在与简化了一些配置文件的操作和依赖的管理;
举个例子:
比如你从商家买了一个结构复杂的桌子,按照spring的模式,它会把零件发给你,组装是你自己的事情。而这张桌子是在你组装好后才能使用它多种多样的功能。
而按照springboot的模式,他相当于直接把这个桌子发给你,你只需要知道你想要桌子的型号和功能,买回来就能很快速的使用。
二,重点介绍;
(1)起步依赖;
(2)自动配置;
问题来了,什么是起步依赖呢?依赖我们知道,他在我们利用Maven开发时具有很重要的作用,从概念中我们或许可以知道他的意义:告诉Spring Boot需要什么功能,它就能引入需要的库;我的理解是,在头疼的众多依赖中,springboot设计了一种方法,他可以有效的提供给你想要的依赖而不会出现版本冲突,或依赖缺失等令我们抓耳挠腮的问题。
自动配置设计是springboot的高度自动化的特点,至于有多高,下面的学习中我们将慢慢领悟。
三,开始第一个程序
(1)开发环境:安装有sts插件的eclipse
(2)Thymeleaf来定义Web视图;
(3)数据持久化:Spring Data JPA;
(4)数据库:嵌入式的H2数据库;
(5)功能:构建以个简单的阅读列表应用程序。
四,具体步骤(很具体,具体到令人发指)
1,打开eclipse,安装sts插件:
(1)Help--->Eclipse MarkePlace--->Populer
找到Spring Tool Suite (STS) for Eclipse点击安装
(呀!竟然找不到,没关系,没有什么是一个链接解决不了的:https://blog.youkuaiyun.com/zhen_6137/article/details/79383941)
(好吧!插件安装失败,咱们改用IDEA吧!)
打开IDEA:new -->project--->Spring Initializer
Project SDK :Java版本
Service URL选择默认。
不知不觉中我们就涉及到了起步依赖的操作,没错,简单到令人发指,你只需要点几下就够了。当然也有复杂的。
来来来,热腾腾,新鲜的组织架构程上来了喂!
ReadinglistApplication:应用程序的启动引导类(bootstrap class)也是主要的启动配置类。
来来来,咱们细细的琢磨琢磨,这个类存在的意义:
虽然大佬想让你少些配置文件但不是让你不写配置文件,即使让写也不让你写那么难的,到底说是好人啊,膜拜之!
所以在这里你需要少量的配置来启动配置,毕竟自动性极强的声控灯你也得插上插头吧!于是乎,该类就应运而生,主要作用就是配置和引导
具体代码:
画面清晰,一个注解一个main方法,我就大胆的包产到户,注解用于加载配置,main方法用于启动引导。
一查资料,嘿,大致是点意思
@SpringBootApplication 开启了Spring的组件扫描和Spring Boot的自动配置功能。实际
上, @SpringBootApplication 将三个有用的注解组合在了一起。
(1)Spring的 @Configuration :标明该类使用Spring基于Java的配置。虽然本书不会写太多
配置,但我们会更倾向于使用基于Java而不是XML的配置。
(2)Spring的 @ComponentScan :启用组件扫描,这样你写的Web控制器类和其他组件才能被
自动发现并注册为Spring应用程序上下文里的Bean。本章稍后会写一个简单的Spring MVC
控制器,使用 @Controller 进行注解,这样组件扫描才能找到它。
(3)Spring Boot 的 @EnableAutoConfiguration : 这 个 不 起 眼 的 小 注 解 也 可 以 称 为
@Abracadabra,就是这一行配置开启了Spring Boot自动配置的魔力,让你不用再写成
篇的配置了。
在Spring Boot的早期版本中,你需要在 ReadingListApplication 类上同时标上这三个注
解,但从Spring Boot 1.2.0开始,有 @SpringBootApplication 就行了
测试方法日后祥述。
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>com.manning</groupId>
<artifactId>readinglist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>readinglist</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
<!-- 从 spring-boot-starter-parent继承版本号 -->
</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--运用Spring Boot插件 -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
总结:
(1)依赖是不需要写版本号的,但不是说没有版本号,他的版本号是从 spring-boot-starter-parent继承来的,并可进行重写和过滤操作,相当方便。
(2)以spring-boot-starter开头的依赖都是起步依赖,这是他和其他依赖区分的标识。
(3)构建插件的主要功能是把项目打包成一个可执行的超级JAR,包括把应用程序的所有依赖打入JAR文件内,并为JAR添加一个描述文件,其中的内容能让你用java -jar来运行。
2,创建实体(定义领域模型)
package com.manning.readinglist;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity //@Entity 注解表明它是一个JPA实体
public class Book {
// id 属性加了 @Id 和 @GeneratedValue 注解,说明这个字段是实体的唯一标识,并且这个字段的值是自动生成的.
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private String reader;
private String isbn;
private String title;
private String author;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
3,定义仓库接口,(与数据库实现交互)
package com.manning.readinglist;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface ReadingListRepository extends JpaRepository<Book, Long> {
List<Book> findByReader(String reader);
}
通过扩展 JpaRepository , ReadingListRepository 直接继承了18个执行常用持久化操作的方法。 JpaRepository 是个泛型接口,有两个参数:仓库操作的领域对象类型,及其ID属性的类型。此外,我还增加了一个 findByReader() 方法,可以根据读者的用户名来查找阅读列表。如果你好奇谁来实现这个 ReadingListRepository 及其继承的18个方法,请不用担心,Spring Data提供了很神奇的魔法,只需定义仓库接口,在应用程序启动后,该接口在运行时会自动实现。
4,控制层实现
package com.manning.readinglist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
@RequestMapping("/")
public class ReadingListController {
private ReadingListRepository readingListRepository;
@Autowired
public ReadingListController(
ReadingListRepository readingListRepository) {
this.readingListRepository = readingListRepository;
}
@RequestMapping(value="/{reader}", method=RequestMethod.GET)
public String readersBooks(
@PathVariable("reader") String reader,
Model model) {
List<Book> readingList =
readingListRepository.findByReader(reader);
if (readingList != null) {
model.addAttribute("books", readingList);
}
return "readingList";
}
@RequestMapping(value="/{reader}", method=RequestMethod.POST)
public String addToReadingList(
@PathVariable("reader") String reader, Book book) {
book.setReader(reader);
readingListRepository.save(book);
return "redirect:/{reader}";
}
}
5,前端实现
<html>
<head>
<title>Reading List</title>
<link rel="stylesheet" th:href="@{/style.css}"></link>
</head>
<body>
<h2>Your Reading List</h2>
<div th:unless="${#lists.isEmpty(books)}">
<dl th:each="book : ${books}">
<dt class="bookHeadline">
<span th:text="${book.title}">Title</span> by
<span th:text="${book.author}">Author</span>
(ISBN: <span th:text="${book.isbn}">ISBN</span>)
</dt>
<dd class="bookDescription">
<span th:if="${book.description}"
th:text="${book.description}">Description</span>
<span th:if="${book.description eq null}">
No description available</span>
</dd>
</dl>
</div>
<div th:if="${#lists.isEmpty(books)}">
<p>You have no books in your book list</p>
</div>
<hr/>
<h3>Add a book</h3>
<form method="POST">
<label for="title">Title:</label>
<input type="text" name="title" size="50"></input><br/>
<label for="author">Author:</label>
<input type="text" name="author" size="50"></input><br/>
<label for="isbn">ISBN:</label>
<input type="text" name="isbn" size="15"></input><br/>
<label for="description">Description:</label><br/>
<textarea name="description" cols="80" rows="5"></textarea><br/>
<input type="submit"></input>
</form>
</body>
</html>
css:
body {
background-color: #cccccc;
font-family: arial,helvetica,sans-serif;
}
.bookHeadline {
font-size: 12pt;
font-weight: bold;
}
.bookDescription {
font-size: 10pt;
}
label {
font-weight: bold;
}
运行: