Thymeleaf Maven+Servlet+Mysql图书框架—2(八)

一、图书列表功能

1、 从数据库取出数据展示图书信息

BooksDao--->BooksDaoImpl

java 复制代码
public class BooksDaoImpl extends BaseDao implements BooksDao {  
  @Override
    public List<Books> queryBook() throws Exception {
        String sql = "select  id,title,author,isbn,stock,publish_date publishDate,category from books";
        return executeQuery(Books.class,sql);
    }

}

BooksService-->BooksServiceImpl

java 复制代码
public class BooksServiceImpl implements BooksService {   
 @Override
    public List<Books> findBook() throws Exception {
        return booksDao.queryBook();
    }

}

BooksServlet

java 复制代码
package org.hlx.servlet.model;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.hlx.dao.BooksDao;
import org.hlx.dao.impl.BooksDaoImpl;
import org.hlx.pojo.Books;
import org.hlx.service.BooksService;
import org.hlx.service.impl.BooksServiceImpl;
import org.hlx.servlet.base.CustomTemplateEngine;
import org.hlx.servlet.base.ModelServlet;

import java.io.IOException;
import java.util.List;

/**
 * @author : HLX
 * @ClassName :UserSevlet
 * @date : 2026/1/13 14:55
 * @Version :1.0
 * @Description: TODO
 * @modyified By :
 */
@Slf4j
@WebServlet("/booksServlet")
public class BooksServlet extends ModelServlet {

    private BooksService booksService = new BooksServiceImpl();

    /**
     * 1、查询书籍信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CustomTemplateEngine engine = CustomTemplateEngine.getInstance(req);
        try {
            System.out.println("执行list方法...");
            // 查询书籍信息
            List<Books> books = booksService.findBook();
            // 将书籍信息保存到request域中
            req.setAttribute("books", books);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 转发到list.html页面
        engine.processTemplate("list", req, resp);

    }

}

2、列表页面list.html

html 复制代码
<!DOCTYPE html>
<!--引入thymeleaf包-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>图书列表</title>
    <base th:href="@{/}">
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css">

    <style>
        .table th{
            text-align: center;
        }

    </style>
</head>
<body>
<h2 class="text-center">欢迎来到图书列表!</h2>
<h2 class="text-center" th:if="${#lists.isEmpty(books)}">
    暂无图书信息!
</h2>
<div class="container">
    <table th:if="${not #lists.isEmpty(books)}" class="table table-bordered table-striped table-hover">
        <thead>
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>作者</th>
            <th>ISBN</th>
            <th>库存</th>
            <th>出版日期</th>
            <th>种类</th>
            <th colspan="2">操作</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="book:${books}">
            <td th:text="${book.id}"></td>
            <td th:text="${book.title}"></td>
            <td th:text="${book.author}"></td>
            <td th:text="${book.isbn}"></td>
            <td th:text="${book.stock}"></td>
            <td th:text="${book.publishDate}"></td>
            <td th:text="${book.category}"></td>
            <td><a class="btn btn-info btn-small" th:href="@{/booksServlet(method='update',id=${book.id})}">修改</a>
            </td>
            <td><a class="delBtn btn btn-danger btn-small"
                   th:href="@{/booksServlet(method='remove',id=${book.id})}">删除</a></td>
        </tr>
        </tbody>
    </table>
</div>

</body>
</html>

3、美化列表页面

使用Bootstrap3框架引入,使得页面更加漂亮直观

官网:https://v3.bootcss.com/

拷贝最新版本的Link链接

复制代码
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css">

4、启动服务,运行如下:

二、删除图书功能

1、BooksServlet

java 复制代码
@Slf4j
@WebServlet("/booksServlet")
public class BooksServlet extends ModelServlet {

    private BooksService booksService = new BooksServiceImpl();


    /**
     * 2、删除书籍信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void remove(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CustomTemplateEngine engine = CustomTemplateEngine.getInstance(req);
        try {
            System.out.println("执行remove方法...");
            int id = Integer.parseInt(req.getParameter("id"));
            System.out.println("id=>>>" + id);
            // 删除书籍信息
            booksService.remove(id);
            // 重定向到list.html页面
            resp.sendRedirect( req.getContextPath()+"/booksServlet?method=list");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2、列表页面list.html 添加Jquery3来提示删除对话框

html 复制代码
    <!--     jquery库文件-->
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>



<script>
    // 删除按钮的点击事件
    $(".delBtn").click(function (e) {
        var flag= confirm("确定要删除吗?");
        // 如果用户点击取消,阻止表单提交
        if(!flag){
            e.preventDefault();
        }
    });
</script>
</body>

3、启动服务,运行如下:

1)单击删除-》提示对话框

2)单击删除》删除数据

相关推荐
indexsunny2 小时前
互联网大厂Java面试实战:Spring Boot与微服务在电商场景中的应用解析
java·数据库·spring boot·微服务·maven·flyway·电商
sunnyday04262 小时前
从混乱到清晰:Maven 依赖版本管理最佳实践
java·spring boot·后端·maven
凌冰_2 小时前
Thymeleaf Maven+Servlet+Mysql图书框架—3(九)
mysql·servlet·maven
roman_日积跬步-终至千里2 小时前
【大数据框架】Calcite 基础概念:从 SQL 到执行计划的思维路径
java·大数据·sql
狂龙骄子2 小时前
MySQL表字段批量修改SQL实战技巧
数据库·sql·mysql·alter table·批量修改·sql实战技巧
cypking2 小时前
后端框架搭建完全指南
java
roman_日积跬步-终至千里2 小时前
【SQL】SQL 语句的解析顺序:理解查询执行的逻辑
java·数据库·sql
雨中飘荡的记忆2 小时前
Spring Test 从入门到实战
java·后端·spring
TeamDev2 小时前
JxBrowser 8.16.0 版本发布啦!
java·chromium·浏览器自动化·jxbrowser·浏览器控件·枚举清理·跨配置文件复制密码