Cet article suivra l'article précédent sur le module de classification du Mall Book pour votre référence. Le contenu spécifique est le suivant
1. Créer des classes connexes
cn.itcast.bookstore.book
domaine: livre
dao: bookdao
Service: BookService
web.servle: bookservlet
Livre
Livre de classe publique {Private String Bid; chaîne privée bname; double prix privé; auteur de chaîne privée; image de chaîne privée; catégorie de catégorie privée; booléen privé del;}Bookdao
classe publique bookdao {private queryrunner qr = new txQueryRunner (); / ** * Recherche tous les livres * @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); }} / ** * requête par catégorie * @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); }} / ** * Méthode de chargement * @param bid * @return * / livre public findbybid (String Bid) {try {/ * * Nous devons enregistrer les informations de la catégorie dans l'objet de livre * / String SQL = "SELECT * FROM Book Where Bid =?"; Map <string, object> map = qr.query (sql, new Maphandler (), bid); / * * Utilisez une carte pour cartographier deux objets, puis établissez une relation pour ces deux objets! * / Catégorie catégorie = Commonutils.Tobean (map, category.class); Livre de livres = Commonutils.Tobean (carte, livre.class); book.setCategory (catégorie); livre de retour; } catch (sqlexception e) {throw new RuntimeException (e); }} / ** * Requête le nombre de livres dans la catégorie spécifiée * @param cid * @return * / public int GetCountBycid (String cid) {try {String sql = "select count (*) from book where cid =?"; Numéro CNT = (Number) Qr.Query (SQL, new ScalarHandler (), CID); retour cnt.intvalue (); } catch (sqlexception e) {throw new RuntimeException (e); }} / ** * add book * @param livre * / public void add (livre de livre) {try {string sql = "insérer dans les valeurs du livre (?,?,?,?,?)"; Objet [] 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); }} / ** * Supprimer le livre * @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 (livre de livre) {try {string sql = "update book set bname =?, prix =?, auteur =?, image =?, cid =? où bid =?"; Objet [] 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
classe publique BookService {private bookdao bookdao = new bookdao (); / ** * interroger tous les livres * @return * / public list <Book> findall () {return bookdao.findall (); } / ** * Books de requête par catégorie * @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 livre * / public void add (livre de livre) {bookdao.add (livre); } public void delete (String Bid) {bookdao.delete (bid); } public void edit (livre de livre) {bookdao.edit (livre); }}Bookservlet
classe publique BookServlet étend BasEservlet {privé BookService BookService = new bookService (); Public String Load (HttpServLetRequest Request, HttpServletResponse Response) lève ServletException, ioException {/ * * 1. Obtenez le paramètre Bid * 2. Query Get Book * 3. Save and Forward to DeSc.Jsp * / Demande.SetAttribute ("Book", BookService.Load (request.getParameter ("Bid"))); return "f: /jsps/book/desc.jsp"; } / ** * Recherche tous les livres * @param request * @param réponse * @return * @throws servlexception * @throws ioException * / public String findall (httpservletRequest request, httpservletResponse réponse) lance Servlexception, ioException {request.SetAtTrribute ("booklist", bookservice.findall ();); return "f: /jsps/book/list.jsp"; } / ** * Query by catégorie * @param request * @param réponse * @return * @throws servlexception * @throws ioException * / public String findbyCategory (httpServLetRequest request, httpservletResponse réponse) lance Servlexception, ioException {string cid = request.getParameter ("CID"); request.setAttribute ("Booklist", BookService.FindByCategory (CID)); return "f: /jsps/book/list.jsp"; }}2. Remettre tous les livres
Processus: Left.jsp (toutes les classes) -> bookservlet # findall () -> /jsps/book/list.jsp
3. Requête des livres par catégorie
Processus: Left.jsp -> bookservlet # findByCategory () -> list.jsp
4. Détails de la requête (charge)
Processus: list.jsp (cliquez sur un livre) -> bookservlet # losh () -> desc.jsp
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.