版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a549654065/article/details/86764422
环境:
操作系统:Window10企业版
IDE:IntelliJ IDEA 2018.2.4
数据库:MySQL
话不多说,直接实战。
新建项目,在新建的时候选择以下依赖
IDEA则会自动帮我们导入以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.5.RELEASE</version>
</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>
添加Druid数据源依赖,在pom.xml文件下添加如下依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.13</version>
</dependency>
创建数据库,在Navicat里面创建一个名为book的数据库,并创建数据表tbl_book,并插入数据,创建的数据表如下:
配置Druid数据源,在application.yml文件下添加如下代码。(默认是application.properties文件,个人比较喜欢在resource文件下创建application.yml文件来进行配置)
spring:
datasource:
# 数据库访问配置, 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/book?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
username: root
password: 123456
如果application.yml配置文件的driver-class-name标红的话
那直接到pom.xml
创建实体类和Mapper接口。
实体类:
public class BookEntity {
private int book_id;
private String book_name;
private String isbn;
private String author;
public int getBook_id() {
return book_id;
}
public void setBook_id(int book_id) {
this.book_id = book_id;
}
public String getBook_name() {
return book_name;
}
public void setBook_name(String book_name) {
this.book_name = book_name;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
Mapper接口
@Mapper
public interface BookMapper {
@Select("SELECT * FROM tbl_book")
List<BookEntity> selAllBooks();
@Select("SELECT * FROM tbl_book WHERE book_name LIKE #{keyword}")
List<BookEntity> selBooks(String keyword);
@Insert("INSERT INTO tbl_book(book_name,isbn,author) VALUES(#{book_name},#{isbn},#{author})")
int addBooks(BookEntity book);
@Delete("DELETE FROM tbl_book WHERE book_id = #{book_id}")
int delBooks(int book_id);
@Select("SELECT COUNT(*) FROM tbl_book")
int booksCount();
}
注意:Mapper接口要加上@Mapper注解或者在启动类加上@MapperScan(“xxx.xxx.xxx”)包扫描的注解
编写Service层接口以及其实现类
public interface BookService {
List<BookEntity> selAllBooks();
List<BookEntity> selBooks(String keyword);
int addBooks(BookEntity book);
int delBooks(int book_id);
}
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public List<BookEntity> selAllBooks() {
return bookMapper.selAllBooks();
}
@Override
public List<BookEntity> selBooks(String keyword) {
return bookMapper.selBooks(keyword);
}
@Override
public int addBooks(BookEntity book) {
return bookMapper.addBooks(book);
}
@Override
public int delBooks(int book_id) {
return bookMapper.delBooks(book_id);
}
}
在resource目录下创建templates文件夹,在templates文件夹下编写四个html页面,index索引页面,addBook添加书籍页面,selBooks查询书籍页面,selResult返回结果页面
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/book/add}">添加</a>
<a th:href="@{/book/toSel}">查询</a>
</body>
</html>
addBook.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/book}" method="post">
<table border="1" cellspacing="0" cellpadding="0" style="align:center;">
<tr><td>图书名称:</td><td><input type="text" name="book_name"></td></tr>
<tr><td>国际标准书号:</td><td><input type="text" name="isbn"></td></tr>
<tr><td>作者:</td><td><input type="text" name="author"></td></tr>
<tr><td><input type="submit" value="提交"></td><td><input type="button" value="返回"></td>
</tr>
</table>
</form>
</body>
</html>
selBooks.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" th:action="@{/book/sel}">
<input type="text" name="keyword"><input type="submit" value="查询">
</form>
</body>
</html>
selResult.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a th:href="@{/book/add}">新增</a>
<br>
<table border="1" cellspacing="0" cellpadding="0" style="align:center;">
<tr bgcolor="ff9900" style="font-weight:bold;">
<th>图书编号</th>
<th>图书名称</th>
<th>国际标准号</th>
<th>作者</th>
</tr>
<tr th:each="book:${books}">
<td>[[${book.book_id }]]</td>
<td>[[${book.book_name }]]</td>
<td>[[${book.isbn }]]</td>
<td>[[${book.author }]]</td>
<form method="post" id="deleteForm" th:action="@{/book/}+${book.book_id}">
<input type="hidden" name="_method" value="delete">
<td>|<button type="submit" onclick='return confirm("确定要删除吗?")'
>删除</button></td>
</form>
</tr>
</table>
</body>
</html>
编写BookController控制器
@Controller
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("/book/add")
public String toAddBook(){
return "book/addBook";
}
@RequestMapping("/book/toSel")
public String toSelBook(){
return "book/selBooks";
}
@PostMapping("/book")
public String addBook(BookEntity bookEntity){
bookService.addBooks(bookEntity);
return "redirect:/book/selAll";
}
@DeleteMapping("/book/{id}")
public String delBook(@PathVariable Integer id){
bookService.delBooks(id);
return "redirect:/book/selAll";
}
@RequestMapping("/book/selAll")
public String selAllBook(Model model){
List<BookEntity> bookEntityList = bookService.selAllBooks();
model.addAttribute("books",bookEntityList);
return "book/selResult";
}
@PostMapping("/book/sel")
public String selBook(String keyword, Model model){
if (keyword != null && keyword.length() > 0 ){
List<BookEntity> bookEntityList = bookService.selBooks(keyword);
model.addAttribute("books",bookEntityList);
return "book/selResult";
}else
return "redirect:/book/selAll";
}
}
运行界面如下: