九、修改删除书籍
1.在allBook中实现页面复用
2.BookController
@RequestMapping("/toUpdateBook") public String toUpdateBook(Model model, int id) { Books books = bookService.queryBookById(id); System.out.println(books); model.addAttribute("book",books ); return "updateBook"; } @RequestMapping("/updateBook") public String updateBook(Model model, Books book) { System.out.println(book); bookService.updateBook(book); Books books = bookService.queryBookById(book.getBookID()); model.addAttribute("books", books); return "redirect:/book/allBook"; }
3.修改页面updateBook
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改信息</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- 引入 Bootstrap --> <link href="https://cdn.bootcss.com/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"> <%--不加出现问题,提交了修改的sql请求,但是修改失败,初次考虑是事务的问题,配置完毕事务,依旧失败--%> <%--看一下sql语句,能否执行成功,sql执行失败,修改未完成--%> <%--前段传递隐藏域--%> <%--id要传,否则不成功。--%> <input type="hidden" name="bookID" value="${book.getBookID()}"/> 书籍名称:<input type="text" name="bookName" value="${book.getBookName()}"/> 书籍数量:<input type="text" name="bookCounts" value="${book.getBookCounts()}"/> 书籍详情:<input type="text" name="detail" value="${book.getDetail() }"/> <input type="submit" value="提交"/> </form> </div> </body> </html>
4.删除书籍
@RequestMapping("/del/{bookId}") public String deleteBook(@PathVariable("bookId") int id) { bookService.deleteBookById(id); return "redirect:/book/allBook"; }
<div class="col-md-4 column"> <%--查询书籍--%> <form action="${pageContext.request.contextPath}/book/queryBook" method="post" style="float:right"> <input type="text" name="queryBookName" class="form-control" placeholder="请输入要查询的书籍名称"> <input type="submit" value="查询" class="btn btn-primary"> </form> </div>
只要添加了jar包就要去打包。去artifact中lib
添加日志功能
在mybatis-config中增加
十、查询框
1.allBook增加查询功能
<settings> <!--标准的日志工厂实现--> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings>
我们设计是从顶到下,开发是从下到上。
2.dao->BookMapper,以及xml
//查询书 Books queryBookByName(@Param("bookName") String bookName);
<select id="queryBookByName" resultType="Books"> select * from books where bookName = #{bookName} </select>
3.Service->BookService以及Impl
//查询书 Books queryBookByName(String bookName);
public Books queryBookByName(String bookName) { return bookMapper.queryBookByName(bookName); }
至此底层搞定
4.BookController
//查询书籍 @RequestMapping("/queryBook") public String Book(String queryBookName, Model model){ Books books = bookService.queryBookByName(queryBookName); List<Books> list = new ArrayList<Books>(); list.add(books); model.addAttribute("list", list); return "allBook"; }
至此我们就可以运行了。
我们前端可以用标签来接收后端传输过来的信息,比如
<span>${error}</span>
model.addAttribute("error","未查到");