Book Query function

  1. Perfect Controller: BookController

    @Controller
    @RequestMapping("/book")
    public class BookController {
        // The coontroller layer calls the service layer
        @Autowired
        @Qualifier("BookServiceImpl")
        private BookService bookService;
    
        // Query all books and return to the book display
        @RequestMapping("/allBook")
        public String list(Model model){
            List<Books> books = bookService.queryAllBook();
    
            model.addAttribute("list",books);
    
            return "allBook"; }}Copy the code
  2. Improve the home page 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"> Enter the book page </a> </h3> </body> </ HTML >Copy the code
  3. Perfect the search book page 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 > books show < / title > < % - BookStrap beautification interface - % > < 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 > list of books - show all books < / small > < / h1 > < / div > < / div > < / div > < divclass="row clearfix">
            <div class="col-md-12 column">
                <table class="table table-hover table-striped"> < thead > < tr > < th > books number < / th > < th > books name < / th > < th > number of books < / th > < th > books details < / th > < / tr > < thead > < % - books from the database query get books traversal out: foreach--%> <tbody> <c:forEachvar="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>
    Copy the code

Add book function

  1. Add the new book button allbook.jsp to the Show all Books page

    <div class="row">
        <div class="col-md-4 column">
            <%--toAddBook--%>
            <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook"</a> </div> </div>Copy the code
  2. Added jump function and added book function bookController.java

    // Go to the Add books page
    @RequestMapping("/toAddBook")
    public String toAddPaper(a){
        return "addBook";
    }
    
    // Add a book request
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("addBook"+books);
        bookService.addBook(books);
        return "redirect:/book/allBook"; // Redirect the request to @requestMapping ("/allBook")
    }
    Copy the code
  3. Added addBook page and form addbook.jsp

    <%@ page contentType="text/html; charset=UTF-8" language="java"%> < HTML > <head> <title> Add book </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 > list of books, new books < / small > < / h1 > < / div > < / div > < / div > < form action ="${pageContext.request.contextPath}/book/addBook" method="">
            <div class="form-group"</label> <input type="text" name="bookName" class="form-control" required>
            </div>
            <div class="form-group"</label> <input type="text" name="bookCounts" class="form-control" required>
            </div>
            <div class="form-group"</label> <input type="text" name="detail" class="form-control" required >
            </div>
            <div class="form-group">
    
                <input type="submit" class="form-control" value="Add">
            </div>
    
        </form>
    </div>
    </body>
    </html>
    Copy the code

Modify delete books

  1. Modify updateBook

    <%@ page contentType="text/html; charset=UTF-8" language="java"%> < HTML > <head> <title> Modify the book </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 > list of books - modified books < / small > < / h1 > < / div > < / div > < / div > < form action ="${pageContext.request.contextPath}/book/updateBook" method="get"> <input type= "%" > <input type= "%"hidden" name="bookID" value="${Qbooks.bookID}">
    
            <div class="form-group"</label> <input type="text" name="bookName" class="form-control" value="${Qbooks.bookName}" required>
            </div>
            <div class="form-group"</label> <input type="text" name="bookCounts" class="form-control" value="${Qbooks.bookCounts}" required>
            </div>
            <div class="form-group"</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="Change">
            </div>
    
        </form>
    </div>
    </body>
    </html>
    Copy the code
  2. Added modified and deleted links to allbook.jsp

    <td>
        <a href="${pageContext.request.contextPath}/book/toUpdateBook? id=${book.bookID}">修改</a>
        &nbsp;|&nbsp;
        <a href="${pageContext.request.contextPath}/book/deleteBook/${book.bookID}"> Delete </a> </td>Copy the code
  3. Controller Added modification and deletion methods

    // Go to the modify page
    @RequestMapping("/toUpdateBook")
    public String toUpdatePaper(int id,Model model){
        Books books = bookService.queryBookById(id);
    
        model.addAttribute("Qbooks",books);
        return "updateBook";
    }
    
    // Modify the book
    @RequestMapping("/updateBook")
    public String updateBook(Books books){
        System.out.println("updateBook:"+books);
        bookService.updateBook(books);
    
        return "redirect:/book/allBook";
    }
    // Delete books
    @RequestMapping("/deleteBook/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }
    Copy the code

Add query function by title from scratch

  1. BookMapper interface and bookmapper.xml

    // Query a book
    Books queryBookByName(@Param("bookName")String bookName);
    Copy the code
    <select id="queryBookByName" resultType="Books">
        select * from ssmbuild.books where bookName=#{bookName};
    </select>
    Copy the code
  2. BookService interface and BookServiceImpl

    // Query a book
    Books queryBookByName(String bookName);
    Copy the code
    public Books queryBookByName(String bookName) {
    
        return bookMapper.queryBookByName(bookName);
    }
    Copy the code
  3. Controller

    // Query books
    @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"."Not found.");
        }
        model.addAttribute("list",list);
        return "allBook";
    }
    Copy the code
  4. The foreground page allbook.jsp

    <div class="col-md-8 column"> <%-- query book --%> <formclass="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="Please enter the name of the book you want to query.">
            <input type="submit" value="Query" class="btn btn-primary">
        </form>
    
    </div>
    Copy the code

The project structure

Summary and outlook

This is the first SSM integration case, be sure to know by heart!

The SSM framework is very important.