SpringMVC-08-整合SSM之CRUD
阿新 • • 發佈:2020-09-14
查詢書籍功能
-
完善Controller:
BookController
@Controller @RequestMapping("/book") public class BookController { //coontroller層調service層 @Autowired @Qualifier("BookServiceImpl") private BookService bookService; //查詢全部的書籍,並且返回到書籍展示也命啊 @RequestMapping("/allBook") public String list(Model model){ List<Books> books = bookService.queryAllBook(); model.addAttribute("list",books); return "allBook"; } }
-
完善首頁
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首頁</title> <style> a { text-decoration: none; color: black; font-size: 18px; } h3 { width: 180px; height: 38px; margin: 100px auto; text-align: center; line-height: 38px; background: deepskyblue; border-radius: 5px; } </style> </head> <body> <h3> <a href="${pageContext.request.contextPath}/book/allBook">進入書籍頁面</a> </h3> </body> </html>
-
完善查詢書籍頁面
allBook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>書籍展示</title> <%--BookStrap美化介面--%> <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> <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> <%--書籍從資料庫中查詢得到 books遍歷出來:foreach--%> <tbody> <c:forEach var="book" items="${list}"> <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>
增加書籍功能
-
在顯示所有書籍頁面新增 新增書籍按鈕
allBook.jsp
<div class="row"> <div class="col-md-4 column"> <%--toAddBook--%> <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增書籍</a> </div> </div>
-
新增跳轉功能和新增書籍功能
BookController.java
//跳轉到增加書籍頁面 @RequestMapping("/toAddBook") public String toAddPaper(){ return "addBook"; } //新增書籍的請求 @RequestMapping("/addBook") public String addBook(Books books){ System.out.println("addBook"+books); bookService.addBook(books); return "redirect:/book/allBook"; //重定向到@RequestMapping("/allBook")請求 }
-
新增新增書籍頁面及表單
addBook.jsp
<%@ 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/addBook" method=""> <div class="form-group"> <label>書籍名稱:</label> <input type="text" name="bookName" class="form-control" required> </div> <div class="form-group"> <label>書籍數量:</label> <input type="text" name="bookCounts" class="form-control" required> </div> <div class="form-group"> <label>書籍描述:</label> <input type="text" name="detail" class="form-control" required > </div> <div class="form-group"> <input type="submit" class="form-control" value="新增"> </div> </form> </div> </body> </html>
修改刪除書籍
-
修改頁面
updateBook
<%@ 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="get"> <%--出現的問題,我們提交了sql請求,但是修改失敗,看一下sql語句能否執行成功,前端沒傳id,前端應該傳遞隱藏域--%> <input type="hidden" name="bookID" value="${Qbooks.bookID}"> <div class="form-group"> <label>書籍名稱:</label> <input type="text" name="bookName" class="form-control" value="${Qbooks.bookName}" required> </div> <div class="form-group"> <label>書籍數量:</label> <input type="text" name="bookCounts" class="form-control" value="${Qbooks.bookCounts}" required> </div> <div class="form-group"> <label>書籍描述:</label> <input type="text" name="detail" class="form-control" value="${Qbooks.detail}" required > </div> <div class="form-group"> <input type="submit" class="form-control" value="修改"> </div> </form> </div> </body> </html>
-
增加修改和刪除的連結
allBook.jsp
<td> <a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.bookID}">修改</a> | <a href="${pageContext.request.contextPath}/book/deleteBook/${book.bookID}">刪除</a> </td>
-
Controller新增修改和刪除方法
//跳轉到修改頁面 @RequestMapping("/toUpdateBook") public String toUpdatePaper(int id,Model model){ Books books = bookService.queryBookById(id); model.addAttribute("Qbooks",books); return "updateBook"; } //修改書籍 @RequestMapping("/updateBook") public String updateBook(Books books){ System.out.println("updateBook:"+books); bookService.updateBook(books); return "redirect:/book/allBook"; } //刪除書籍 @RequestMapping("/deleteBook/{bookId}") public String deleteBook(@PathVariable("bookId") int id){ bookService.deleteBookById(id); return "redirect:/book/allBook"; }
從零開始增加按書名查詢功能
-
BookMapper介面和BookMapper.xml
//查詢一本書 Books queryBookByName(@Param("bookName")String bookName);
<select id="queryBookByName" resultType="Books"> select * from ssmbuild.books where bookName=#{bookName}; </select>
-
BookService介面和BookServiceImpl
//查詢一本書 Books queryBookByName(String bookName);
public Books queryBookByName(String bookName) { return bookMapper.queryBookByName(bookName); }
-
Controller
//查詢書籍 @RequestMapping("/queryBook") public String queryBook(String queryBookName,Model model){ Books books = bookService.queryBookByName(queryBookName); List<Books> list = new ArrayList<Books>(); list.add(books); if(books==null){ list = bookService.queryAllBook(); model.addAttribute("error","未查到"); } model.addAttribute("list",list); return "allBook"; }
-
前臺頁面
allBook.jsp
<div class="col-md-8 column"> <%--查詢書籍--%> <form class="form-inline" action="${pageContext.request.contextPath}/book/queryBook" method="get" style="float:right"> <span style="color: red;font-weight: bold">${error}</span> <input type="text" name="queryBookName" class="form-control" placeholder="請輸入要查詢的書的名稱"> <input type="submit" value="查詢" class="btn btn-primary"> </form> </div>
專案結構
小結及展望
這是第一個SSM整合案例,一定要爛熟於心!
SSM框架的重要程度不言而喻。