《SpringBoot实战》笔记2
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>readinglist</artifactId><version>0.0.1-SNAPSHOT</version><name>readinglist</name><description>Demo project for Spring Boot</description><properties><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><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
package com.example.readinglist;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ReadinglistApplication {public static void main(String[] args) {SpringApplication.run(ReadinglistApplication.class, args);}}
package com.example.readinglist;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;//@Entity注解表明它是一个JPA实体
@Entity
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 getReader() {return reader;}public void setReader(String reader) {this.reader = reader;}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;}}
package com.example.readinglist;import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;/*** 接下来,我们就要定义用于把Book对象持久化到数据库的仓库了。* ①因为用了Spring Data JPA, 所以我们要做的就是简单地定义一个接口,* 扩展一下Spring Data JPA的JpaRepository接口:*//*** 通过扩展JpaRepository,ReadingListRepository直接继承了18个执行常用持久化操作 的方法。* JpaRepository是个泛型接口,有两个参数:仓库操作的领域对象类型,及其ID属性的 类型*/
public interface ReadingListRepository extends JpaRepository<Book,Long> {/*** 可以根据读者的用户名来查找阅读列表。* @param reader* @return List*/List<Book> findByReader(String reader);
}
package com.example.readinglist;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;//这样组件扫描会自动将ReadingListController注册为 Spring应用程序上下文里的一个Bean
@Controller
//将其中所有的处理器 方法都映射到了“/”这个URL路径上。
@RequestMapping("/")
public class ReadingListController {private ReadingListRepository readingListRepository;public ReadingListController(ReadingListRepository readingListRepository){this.readingListRepository = readingListRepository;}/*** 处理/{reader}上的HTTP GET请求,根据路径里指定的读者,从(通 过控制器的构造器注入的)仓库获取Book列表。* 随后将这个列表塞入模型,用的键是 books,后返回readingList作为呈现模型的视图逻辑名称。* @param reader* @param model* @return String*/@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";}/*** addToReadingList():处理/{reader}上的HTTP POST请求,将请求正文里的数据绑定 到一个Book对象上。* 该方法把Book对象的reader属性设置为读者的姓名,* 随后通过仓 库的save()方法保存修改后的Book对象,* 后重定向到/{reader}(控制器中的另一个方 法会处理该请求)。* @param reader* @param book* @return*/@RequestMapping(value = "/{reader}",method = RequestMethod.POST)public String addToReadingList(@PathVariable("reader") String reader,Book book){book.setReader(reader);readingListRepository.save(book);return "redirect:/{reader}";}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.springframework.org/schema/data/jaxb">
<head><meta charset="UTF-8"><title>Reading List</title><link rel="stylesheet" th:href="@{/style.css}"/>
</head>
<body><h2>Your ReadingList</h2><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>
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; }
这里面似乎有个问题,就是css没有起作用、、、
《SpringBoot实战》笔记2相关推荐
- linux中文麻酱字_【树】Linux笔记 1
以下内容是参与[生信技能树-全球听第7期]的课程笔记,记录人:小瑛 ,有问题可在公众号后台留言 小白小白,请注意:笔记中出现的所有文件路径,仅作为参考,请勿模仿! 1. 登录服务器 1.1 Mac电 ...
- 【Linux笔记】CentOS下找不到eth0设备的解决方法
[Linux笔记]CentOS下找不到eth0设备的解决方法 参考文章: (1)[Linux笔记]CentOS下找不到eth0设备的解决方法 (2)https://www.cnblogs.com/ly ...
- linux网络配置命令笔记,初学者学习linux笔记与练习-第二天。一些基本命令以及初级网络配置...
菜鸟学习linux笔记与练习-----第二天.一些基本命令以及初级网络配置 基本命令 ??uname -a -s ??hostname显示主机名 若是要永久生效,则编辑以下文件 ??#vim /etc ...
- 【Linux笔记】LED驱动程序
前言 上一篇我们分享了字符设备驱动框架:[Linux笔记]驱动基础篇,当时分享的是hello驱动程序. 学STM32我们从点灯开始,学Linux驱动我们自然也要点个灯来玩玩,尽量在从这些基础例程中榨取 ...
- 分享:玩Linux笔记(2) —— 神奇的curl工具
玩Linux笔记(2) -- 神奇的curl工具 http://my.oschina.net/chihz/blog/96101
- Linux笔记常规部分总结(待续)
Linux笔记常规部分总结 详见最下方附件 包括: 一.Linux系统常用口令总结 二.Linux常见服务笔记总结 ========================================== ...
- 【03】Linux笔记
Linux 笔记 装机必备 立刻关机 sudo shutdown -t now 设置fish为默认shell chsh -s /usr/bin/fish 安装ssh服务 sudo apt instal ...
- OS和Linux笔记
OS和Linux笔记 操作系统 基本概念 进程管理 进程和线程 协程 同步互斥 死锁 CAS技术 IPC 线程间通信 内存管理 Linux 基础知识 守护进程 系统监测 编译调试 文件管理 零拷贝技术 ...
- Linux笔记——软件包管理
软件包管理,就是对Linux操作系统的软件及安装包进行管理,比如下载.安装.卸载.删除等,等同于Windows控制面板->软件管理. 一.软件包分类 二进制包--已经编译好的,如rpm包 源代码 ...
- 一年多前的Linux笔记,仅以此文纪念当时的年少无知
想我一个学计算机的学生,上了大学才开始摸计算机,那时候啥都不懂,学校的课程安排我就不吐槽了. 首先,大一上学期教的课程都是马哲啥的课,唯一一门与计算机拉的上关系的课是计算机基础,教的是office的基 ...
最新文章
- 播客#50:Sacha Greif
- Graphics.DrawRectangle Method(矩形)
- 算法(22)-leetcode-剑指offer6
- gson包在java语言怎么导入_Eclipse 导入Gson包
- 微软发布Visual Studio 2010 SP1公测版
- android 4.2 camera gallery2,Android Gallery2 修改双击、手势放大的最大倍数
- call to a member funciton get() on null
- error: Zip file too big (greater than 4294959102 bytes)
- 计算机无法读光盘,win10系统无法读取光盘如何解决 win10系统读取光盘失败的解决方法...
- python如何制作地图热力图
- 适合学生党上手测试的免费云服务器推荐
- 职场晋升加薪:除了自己努力,这20条规则和方法你也要知道!
- python计算md5码
- mysql日期 select_MySQL_MySql日期查询语句详解,使用DATE_FORMAT方法SELECT * FROM `le - phpStudy...
- STL的使用和背后数据结构
- ELK学习总结(2-1)mavel -》sense 和 索引初始化
- 2021年的诀尘子和Vue
- R语言学习(一)前言
- 《卧底经济学》书中精髓:我们如何正确理解“稀缺”这件事儿?
- 禁止 mysql nobody_Warning: mysql_real_escape_string(): Access denied for user 'nobody'@'localhost'