Dieser Artikel folgt dem vorherigen Artikel im Buchklassifizierungsmodul der Buch Mall als Referenz. Der spezifische Inhalt ist wie folgt
1. Erstellen Sie verwandte Klassen
cn.itcast.bookstore.book
Domain: Buch
DAO: Bookdao
Service: BookService
Web.Servle: BookServlet
Buch
Public Class Book {private String Bid; private Zeichenfolge Bname; privater Doppelpreis; privater String -Autor; privates Zeichenfolgenbild; Private Kategorie Kategorie; privat boolean del;}Bookdao
public class bookdao {private queryRunner qr = new txqueryRunner (); / ** * alle Bücher abfragen * @return */ publiclist <Book> findAll () {try {string sql = "SELECT * Aus dem Buch, wo del = false"; return Qr.Query (SQL, New Beanlisthandler <Book> (book.class)); } catch (SQLEXception e) {neue RunTimeException (e) werfen; }} / ** * Abfrage nach Kategorie * @param cid * @return * / publiclist <Book> findByCategory (String CID) {try {string SQL = "SELECT * Aus dem Buch, wo cid =? Und del = false"; return QR.Query (SQL, New Beanlisthandler <Book> (book.class), CID); } catch (SQLEXception e) {neue RunTimeException (e) werfen; }} / ** * Lademethode * @param bid * @return * / public book findBybid (String -Bid) {try { / * * Wir müssen die Informationen der Kategorie im Buchobjekt * / String SQL = "SELECT * aus Buch Where bid =?" Karte <String, Objekt> map = qr.Query (SQL, New Maphandler (), Bid); / * * Verwenden Sie eine Karte, um zwei Objekte zuzuordnen und dann eine Beziehung für diese beiden Objekte herzustellen! */ Category category = CommonUtils.tobean (MAP, category.class); Book book = CommonUtils.tobean (Karte, book.class); book.setCategory (Kategorie); Rückbuch; } catch (SQLEXception e) {neue RunTimeException (e) werfen; }} / *** Abfragen Sie die Anzahl der Bücher in der angegebenen Kategorie* @param cid* @return* / public int contountbycid (String cid) {try {string sql = "Wählen Sie Count (*) aus Buch Where Cid =?"; Nummer cnt = (Nummer) qr.Query (SQL, New ScalarHandler (), CID); return cnt.intValue (); } catch (SQLEXception e) {neue RunTimeException (e) werfen; }} / *** Buch hinzufügen* @param book* / public void add (book book) {try {string sql = "In Buchwerte einfügen (?,?,?,?,?)"; Object [] params = {book.getbid (), book.getbname (), book.getPrice (), book.getAuthor (), book.getImage (), book.getCategory (). GetCid ()}; QR.Update (SQL, Params); } catch (SQLEXception e) {neue RunTimeException (e) werfen; }} / *** das Buch löschen* @param bid* / public void delete (String -Bid) {try {String sql = "Aktualisieren Sie das Buch set del = true Where bid =?"; Qr.Update (SQL, Bid); } catch (SQLEXception e) {neue RunTimeException (e) werfen; }} public void edit (Buchbuch) {try {string sql = "book set bname =?, price =?, author =?, image =?, cid =? WO BID =?"; Object [] params = {book.getBname (), book.getPrice (), book.getAuthor (), book.getImage (), book.getCategory (). GetCid (), book.getbid ()}; QR.Update (SQL, Params); } catch (SQLEXception e) {neue RunTimeException (e) werfen; }}}Bookservice
public class bookService {private bookdao bookdao = new Bookdao (); / *** alle Bücher abfragen* @return*/ public list <Book> findall () {return bookDao.findall (); } / ** * Bücher nach Kategorie abfragen * @param cid * @return * / publiclist <Book> findByCategory (String cid) {return bookDao.findByCategory (CID); } public book load (string bid) {return bookDao.findbybid (bid); } / *** Buch hinzufügen* @param book* / public void add (book book) {bookdao.add (book); } public void delete (String Bid) {bookDao.delete (bid); } public void edit (Buchbuch) {bookDao.edit (Buch); }}BookServlet
Public Class BookServlet erweitert BaseServlet {privat bookService bookService = new BookService (); öffentliche String -Load (httpServletRequest -Anfrage, httpServletResponse -Antwort) löst ServletException aus, IOException { / * * 1. Erhalten Sie das Parameter -Gebot * 2. Abfrage Get Book * 3.. Speichern und weiterleiten an Desc.jsp * / Request.Setattribute ("Buch", bookService.load (Anfrage ("). 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"; } / ** * Abfrage nach Kategorie * @param request * @param response * @return * @throws ServletException * @throws ioException * / public String findByCategory (httpServletRequest -Anforderung, httpServletResponse) verurteilt servletException, ioException {String cid = Anforderung. Request.SetAttribute ("Buchliste", BookService.findbyCategory (CID)); return "f: /jsps/book/list.jsp"; }}2. Fragen Sie alle Bücher ab
Prozess: links.jsp (alle Klassen) -> BookServlet#findall () -> /jsps/book/list.jsp
3.. Bücher nach Kategorie abfragen
Prozess: links.jsp -> bookServlet#findByCategory () -> list.jsp
4. Abfragendetails (Laden)
Prozess: list.jsp (klicken Sie auf ein Buch) -> BookServlet#load () -> desc.jsp
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.