1.项目结构(省略Service层)
2.pom依赖
<?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.7.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cucn</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.application.yml
spring:
thymeleaf:
cache: false
suffix: .html
prefix: classpath:/templates/
mode: HTML
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/library?useSSL=false
username: root
password: root
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*Mapper.xml
type-aliases-package: com.cucn.springboot.pojo
logging:
level:
com.cucn.springboot.mapper: debug
3.实体类
@Data
@TableName("book")
public class Book {
private String isbn;
@TableField("book_name")
private String bookName;
private String author;
private String publisher;
private String intro;
@TableLogic(value = "1",delval = "0")
private String status;
}
4.Mapper
5.BookMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cucn.springboot.mapper.BookMapper">
</mapper>
6.Controller
@Controller
@RequestMapping("/book")
public class BookController {
@Resource
private BookMapper bookMapper;
@RequestMapping("/list")
public String list(Model model){
List<Book> bookList = bookMapper.selectList(new QueryWrapper<Book>());
model.addAttribute("books",bookList);
return "listBook";
}
@RequestMapping("/insert")
public String insert(Model model,Book book){
book.setStatus("1");
bookMapper.insert(book);
return "redirect:/book/list";
}
@RequestMapping("/del/{isbn}")
public String del(@PathVariable String isbn){
bookMapper.delete(new QueryWrapper<Book>().eq("isbn",isbn));
return "redirect:/book/list";
}
@RequestMapping("/sel/{isbn}")
public String sel(@PathVariable String isbn,Model model){
Book selbook = bookMapper.selectOne(new QueryWrapper<Book>().eq("isbn", isbn));
model.addAttribute("book",selbook);
return "updatePage";
}
@RequestMapping("/update")
public String update(Book book){
if (book.getIsbn()!= null) {
bookMapper.update(book,new QueryWrapper<Book>().eq("isbn",book.getIsbn()));
}
return "redirect:/book/list";
}
@RequestMapping("/doinsert")
public String doinsert(){
return "insertBook";
}
}
7.
insertBook.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>添加图书</title>
</head>
<body>
<div style="width: 500px">
<form method="post" action="/book/list">
<button type="submit">返回</button>
</form>
<form method="post" action="/book/insert">
<span>书号:</span><!--/*@thymesVar id="book" type="com.cucn.springboot.pojo.Book"*/-->
<input type="text" name="isbn" placeholder="请输入书号"></br>
<span>书名:</span><input type="text" name="bookName" placeholder="请输入书名"></br>
<span>作者:</span><input type="text" name="author" placeholder="请输入作者"></br>
<span>出版社:</span><input type="text" name="publisher" placeholder="请输入出版社"></br>
<input type="submit" value="提交">
</form>
</div>
</body>
</html>
listBook.html
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>图书列表</title>
</head>
<body>
<div>
<form method="post" action="/book/doinsert">
<button type="submit">新增</button>
</form>
<table style="border:gray 1px solid">
<tr>
<td>书号</td>
<td>书名</td>
<td>作者</td>
<td>出版社</td>
<td colspan="8">介绍</td>
<td>操作</td>
</tr>
<tr th:each="books,booksStat : ${books}">
<td th:text="${books.isbn}"></td>
<td th:text="${books.bookName}"></td>
<td th:text="${books.author}"></td>
<td th:text="${books.publisher}"></td>
<td colspan="8">
<p th:text="${books.intro}" style="width: 200px;white-space: nowrap ;text-overflow: ellipsis;overflow: hidden"></p>
</td>
<td>
<a th:href="'/book/del/'+${books.isbn}">删除</a>
<a th:href="'/book/sel/'+${books.isbn}">修改</a>
</td>
</tr>
</table>
</div>
</body>
</html>
updatePage.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>修改图书</title>
</head>
<body>
<div style="width: 500px">
<form method="post" action="/book/list">
<button type="submit">返回</button>
</form>
<form method="post" action="/book/update">
<span>书号:</span><input type="hidden" name="isbn" th:value="${book.isbn}" placeholder="请输入书号"></br>
<span>书名:</span><input type="text" name="bookName" th:value="${book.bookName}" placeholder="请输入书名"></br>
<span>作者:</span><input type="text" name="author" th:value="${book.author}" placeholder="请输入作者"></br>
<span>出版社:</span><input type="text" name="publisher" th:value="${book.publisher}" placeholder="请输入出版社"></br>
<span>介绍:</span></br>
<textarea rows="16" cols="30" name="intro" th:text="${book.intro}" placeholder="介绍"></textarea></br>
<input type="submit" value="提交">
</form>
</div>
</body>
</html>
8.表结构
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`isbn` varchar(13) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '图书号',
`book_name` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '书名',
`author` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '作者',
`publisher` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '出版社',
`intro` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '简介',
`status` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0' COMMENT '是否删除(未删1,已删0)',
PRIMARY KEY (`isbn`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '图书表' ROW_FORMAT = Dynamic;
9.页面效果