This article will follow the previous article on the book mall classification module for your reference. The specific content is as follows
1. Create related classes
cn.itcast.bookstore.book
domain: Book
dao: BookDao
service:BookService
web.servle: BookServlet
Book
public class Book { private String bid; private String bname; private double price; private String author; private String image; private Category category; private boolean del;}BookDao
public class BookDao { private QueryRunner qr = new TxQueryRunner(); /** * Query all books* @return */ public List<Book> findAll() { try { String sql = "select * from book where del=false"; return qr.query(sql, new BeanListHandler<Book>(Book.class)); } catch(SQLException e) { throw new RuntimeException(e); } } /** * Query by category* @param cid * @return */ public List<Book> findByCategory(String cid) { try { String sql = "select * from book where cid=? and del=false"; return qr.query(sql, new BeanListHandler<Book>(Book.class), cid); } catch(SQLException e) { throw new RuntimeException(e); } } /** * Loading method* @param bid * @return */ public Book findByBid(String bid) { try { /* * We need to save the information of Category in the Book object*/ String sql = "select * from book where bid=?"; Map<String,Object> map = qr.query(sql, new MapHandler(), bid); /* * Use a Map to map two objects, and then establish a relationship for these two objects! */ Category category = CommonUtils.toBean(map, Category.class); Book book = CommonUtils.toBean(map, Book.class); book.setCategory(category); return book; } catch(SQLException e) { throw new RuntimeException(e); } } /** * Query the number of books under the specified category* @param cid * @return */ public int getCountByCid(String cid) { try { String sql = "select count(*) from book where cid=?"; Number cnt = (Number)qr.query(sql, new ScalarHandler(), cid); return cnt.intValue(); } catch(SQLException e) { throw new RuntimeException(e); } } /** * Add book* @param book */ public void add(Book book) { try { String sql = "insert into book values(?,?,?,?,?,?)"; Object[] params = {book.getBid(), book.getBname(), book.getPrice(), book.getAuthor(), book.getImage(), book.getCategory().getCid()}; qr.update(sql, params); } catch(SQLException e) { throw new RuntimeException(e); } } /** * Delete the book* @param bid */ public void delete(String bid) { try { String sql = "update book set del=true where bid=?"; qr.update(sql, bid); } catch(SQLException e) { throw new RuntimeException(e); } } public void edit(Book book) { try { String sql = "update book set bname=?, price=?, author=?, image=?, cid=? where bid=?"; Object[] params = {book.getBname(), book.getPrice(), book.getAuthor(), book.getImage(), book.getCategory().getCid(), book.getBid()}; qr.update(sql, params); } catch(SQLException e) { throw new RuntimeException(e); } }}BookService
public class BookService { private BookDao bookDao = new BookDao(); /** * Query all books* @return */ public List<Book> findAll() { return bookDao.findAll(); } /** * Query books by category* @param cid * @return */ public List<Book> findByCategory(String cid) { return bookDao.findByCategory(cid); } public Book load(String bid) { return bookDao.findByBid(bid); } /** * Add book* @param book */ public void add(Book book) { bookDao.add(book); } public void delete(String bid) { bookDao.delete(bid); } public void edit(Book book) { bookDao.edit(book); }}BookServlet
public class BookServlet extends BaseServlet { private BookService bookService = new BookService(); public String load(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1. Get the parameter bid * 2. Query get Book * 3. Save and forward to desc.jsp */ request.setAttribute("book", bookService.load(request.getParameter("bid"))); return "f:/jsps/book/desc.jsp"; } /** * Query all books* @param request * @param response * @return * @throws ServletException * @throws IOException */ public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("bookList", bookService.findAll()); return "f:/jsps/book/list.jsp"; } /** * Query by category* @param request * @param response * @return * @throws ServletException * @throws IOException */ public String findByCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String cid = request.getParameter("cid"); request.setAttribute("bookList", bookService.findByCategory(cid)); return "f:/jsps/book/list.jsp"; }}2. Query all books
Process: left.jsp (all classes) -> BookServlet#findAll() -> /jsps/book/list.jsp
3. Query books by category
Process: left.jsp -> BookServlet#findByCategory() -> list.jsp
4. Query details (load)
Process: list.jsp (click on a book) -> BookServlet#load() -> desc.jsp
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.