Version: Next

图书管理

1. 查询图书

  • Controller

    @Controller
    @RequestMapping("/book")
    public class BookController {
    @Autowired
    private BookService bookService;
    //查询所有书籍,返回到书籍展示页面
    @RequestMapping("/allBook")
    public String list(Model model) {
    List<Books> books = bookService.queryAllBooks();
    model.addAttribute("books", books);
    return "allBook";
    }
    }
  • index.jsp

    <%--
    Created by IntelliJ IDEA.
    User: Cesky001
    Date: 2020/4/16
    Time: 19:12
    To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>$Title$</title>
    <style>
    a{
    text-decoration: none;
    color: black;
    font-size: 18px;
    }
    h3{
    width: 180px;
    height: 30px;
    margin: 100px auto;
    text-align: center;
    line-height: 38px;
    background: lightskyblue;
    border-radius: 5px;
    }
    </style>
    </head>
    <body>
    <h3>
    <a href="${pageContext.request.contextPath}/book/allBook">进入书籍页面</a>
    </h3>
    </body>
    </html>
  • allBook.jsp 引入BootStrap,用在线的,在线搜索一个BootStrap CDN

    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    • 完整页面

      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      <%--
      Created by IntelliJ IDEA.
      User: Cesky001
      Date: 2020/4/17
      Time: 12:38
      To change this template use File | Settings | File Templates.
      --%>
      <%@ page contentType="text/html;charset=UTF-8" language="java" %>
      <html>
      <head>
      <title>书籍展示</title>
      <!-- 新 Bootstrap 核心 CSS 文件 -->
      <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"
      rel="stylesheet">
      <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
      <%-- <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>--%>
      <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
      <%-- <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>--%>
      </head>
      <body>
      <div class="container">
      <%-- 头--%>
      <div class="row clearfix">
      <div class="col-md-12 column">
      <div class="page-header">
      <h1>
      <small>书籍列表——显示所有书籍</small>
      </h1>
      </div>
      </div>
      </div>
      <%--展示数据的table--%>
      <div class="row clearfix">
      <div class="col-md-12 column">
      <table class="table table-hover table-striped">
      <thead>
      <tr>
      <th>书籍编号</th>
      <th>书籍名称</th>
      <th>书籍数量</th>
      <th>书籍详情</th>
      </tr>
      </thead>
      <%-- 从数据库中查询,从controller设置的参数books中遍历出来--%>
      <tbody>
      <c:forEach var="book" items="${books}">
      <tr>
      <td>${book.bookID}</td>
      <td>${book.bookName}</td>
      <td>${book.bookCounts}</td>
      <td>${book.detail}</td>
      </tr>
      </c:forEach>
      </tbody>
      </table>
      </div>
      </div>
      </div>
      </body>
      </html>

2. 添加图书

  • allBook页面添加新增书籍按钮
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>书籍列表——显示所有书籍</small>
</h1>
</div>
</div>
<!-- 添加书籍按钮 -->
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary"
href="${pageContext.request.contextPath}/book/toAddPage">新增书籍</a>
</div>
</div>
</div>
  • Controller:添加一个路由跳转到增加书籍的编辑页面
//跳转到增加书籍页面
@RequestMapping("/toAddPage")
public String toAddPage() {
return "addBook";
}
  • 新建一个页面addBook,上面有一个form表单
<div class="container">
<%-- 头--%>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>书籍列表——新增书籍</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/book/addBook" method="post">
<div class="form-group">
<label for="bookName">书籍名称:</label>
<input type="text" class="form-control" id="bookName" name="bookName" required>
</div>
<div class="form-group">
<label for="bookCounts">书籍数目:</label>
<input type="text" class="form-control" id="bookCounts" name="bookCounts" required>
</div>
<div class="form-group">
<label for="detail">书籍描述:</label>
<input type="text" class="form-control" id="detail" name="detail" required>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="添加">
</div>
</form>
</div>
  • Controller:写一个执行添加操作的路由addBook
//添加书籍的请求
@RequestMapping("/addBook")
public String addBook(Books books){
System.out.println(books);
bookService.addBook(books);
return "redirect:/book/allBook";
}

3. 修改删除图书

3.1 修改书籍

  • 修改allBooks页面,添加修改删除按钮

    <c:forEach var="book" items="${books}">
    <tr>
    <td>${book.bookID}</td>
    <td>${book.bookName}</td>
    <td>${book.bookCounts}</td>
    <td>${book.detail}</td>
    <td>
    <a href="${pageContext.request.contextPath}/book/toUpdate?id=${book.bookID}">修改</a>
    &nbsp;|&nbsp;
    <a href="#">删除</a>
    </td>
    </tr>
    </c:forEach>
  • 添加/toUpdate路由,用来跳转到修改页面

    • 跳转时,携带当前书籍的信息
    //跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toUpdatePaper(int id, Model model) {
    Books book = bookService.queryBookById(id);
    model.addAttribute("books", book);
    return "updateBook";
    }
  • 添加一个修改页面updateBook.jsp

    • 用隐藏域传递id,也可以用路由传递id。这样可以在后端Controller直接接收到一个Books对象
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>修改书籍</title>
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"
    rel="stylesheet">
    </head>
    <body>
    <div class="container">
    <%-- 头--%>
    <div class="row clearfix">
    <div class="col-md-12 column">
    <div class="page-header">
    <h1>
    <small>书籍列表——修改书籍</small>
    </h1>
    </div>
    </div>
    </div>
    <form action="${pageContext.request.contextPath}/book/updateBook" method="post">
    <%-- 用隐藏域传递id,这样后端Controller可以直接接收一个Books对象 --%>
    <input type="hidden" name="bookID" value="${books.bookID}">
    <div class="form-group">
    <label for="bookName">书籍名称:</label>
    <input type="text" class="form-control" id="bookName" value="${books.bookName}"
    name="bookName"
    required>
    </div>
    <div class="form-group">
    <label for="bookCounts">书籍数目:</label>
    <input type="text" class="form-control" id="bookCounts" value="${books.bookCounts}" name="bookCounts"
    required>
    </div>
    <div class="form-group">
    <label for="detail">书籍描述:</label>
    <input type="text" class="form-control" id="detail" value="${books.detail}"
    name="detail" required>
    </div>
    <div class="form-group">
    <input type="submit" class="form-control" value="修改">
    </div>
    </form>
    </div>
    </body>
    </html>
  • 写一个/updateBooks路由,用来调用持久层更新数据

    //修改书籍
    @RequestMapping("/updateBook")
    public String updateBook(Books books) {
    System.out.println("update book ------> " + books);
    bookService.updateBook(books);
    return "redirect:/book/allBook";
    }

3.2 删除书籍

  • 修改allBooks页面,给删除按钮添加路由,并传递id参数

    <td>
    <a href="${pageContext.request.contextPath}/book/toUpdate?id=${book.bookID}">修改</a>
    &nbsp;|&nbsp;
    <a href="${pageContext.request.contextPath}/book/deleteBook?id=${book.bookID}">删除</a>
    </td>
  • 写一个/deleteBook路由,接收id参数,调用数据库删除记录

    @RequestMapping("/deleteBook")
    public String deleteBook(int id) {
    bookService.deleteBookById(id);
    return "redirect:/book/allBook";
    }

4. 搜索图书

  • allBooks页面添加一个搜索框

    • 传一个参数,定义在name属性里,queryBookName指明要查询的书籍名称
    • 设置一个新按钮显示全部书籍,用来在搜索书籍之后重新显示全部书籍
    <div class="row">
    <div class="col-md-4 column">
    <a class="btn btn-primary"
    href="${pageContext.request.contextPath}/book/toAddPage">新增书籍</a>
    <a class="btn btn-primary"
    href="${pageContext.request.contextPath}/book/allBook">显示全部书籍</a>
    </div>
    <%-- 搜索框--%>
    <div class="col-md-4 column"></div>
    <div class="col-md-4 column">
    <form action="${pageContext.request.contextPath}/book/queryBook" method="post"
    style="float: right"
    class="form-inline">
    <input type="text" name="queryBookName" class="form-control"
    placeholder="请输入要查询的书籍名称">
    <input type="submit" value="查询" class="btn btn-primary">
    </form>
    </div>
    </div>
info

从DAO层,从下往上写

  • BookMapper接口

    List<Books> queryBookByName(String bookNmae);
  • BookMapper.xml

    <select id="queryBookByName" resultType="com.bsx.pojo.Books">
    SELECT * FROM books WHERE bookName LIKE #{bookName}
    </select>
  • Service接口

    List<Books> queryBookByName(String bookName);
  • Service实现类

    • 两边加上通配符%
    public List<Books> queryBookByName(String bookName) {
    String name = "%" + bookName + "%";
    return bookMapper.queryBookByName(name);
    }
  • Controller/queryBook路由

    • 复用allBook页面
    • allBook页面上之前写好了对books进行遍历显示的代码
    //查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName, Model model){
    List<Books> books = bookService.queryBookByName(queryBookName);
    model.addAttribute("books",books);
    return "allBook";
    }