国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

javaweb圖書商城設計之圖書模塊(4)

2019-11-26 13:34:47
字體:
來源:轉載
供稿:網友

本文接著上一篇圖書商城分類模塊進行學習,供大家參考,具體內容如下

1、創建相關類

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(); /** * 查詢所有圖書 * @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); } } /** * 按分類查詢 * @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); } } /** * 加載方法 * @param bid * @return */ public Book findByBid(String bid) { try { /* * 我們需要在Book對象中保存Category的信息 */ String sql = "select * from book where bid=?"; Map<String,Object> map = qr.query(sql, new MapHandler(), bid); /* * 使用一個Map,映射出兩個對象,再給這兩個對象建立關系! */ 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); } } /** * 查詢指定分類下的圖書本數 * @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); } } /** * 添加圖書 * @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); } } /** * 刪除圖書 * @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(); /** * 查詢所有圖書 * @return */ public List<Book> findAll() { return bookDao.findAll(); } /** * 按分類查詢圖書 * @param cid * @return */ public List<Book> findByCategory(String cid) { return bookDao.findByCategory(cid); } public Book load(String bid) { return bookDao.findByBid(bid); } /** *  添加圖書 * @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. 得到參數bid * 2. 查詢得到Book * 3. 保存,轉發到desc.jsp */ request.setAttribute("book", bookService.load(request.getParameter("bid"))); return "f:/jsps/book/desc.jsp"; } /** * 查詢所有圖書 * @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"; } /** * 按分類查詢 * @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、查詢所有圖書

流程:left.jsp(全部分類) -> BookServlet#findAll() -> /jsps/book/list.jsp

3、按分類查詢圖書

流程:left.jsp -> BookServlet#findByCategory() -> list.jsp

4、查詢詳細信息(加載)

流程:list.jsp(點擊某一本書) -> BookServlet#load() -> desc.jsp

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临沧市| 德格县| 通海县| 汶上县| 汉源县| 岳阳市| 利辛县| 莆田市| 寿光市| 鄂托克前旗| 墨玉县| 井陉县| 新竹市| 茶陵县| 灵武市| 城口县| 盐池县| 清水河县| 新田县| 当涂县| 象山县| 瓦房店市| 怀仁县| 舒兰市| 昌宁县| 茌平县| 白山市| 漳浦县| 裕民县| 新邵县| 六安市| 阿尔山市| 福海县| 东港市| 吉隆县| 蒙自县| 宜章县| 宿州市| 微博| 萨嘎县| 军事|