This article is followed by a book mall classification module for your reference, the specific content as follows
1. Create related class
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); /** * 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); /** * Load Method * @param bid * @return/public book findbybid (String Bid) {try {* * * * We need to save the category in the book object
Information */String sql = "SELECT * from book where bid=?";
map<string,object> map = qr.query (sql, New Maphandler (), Bid);
* * Use a map to map out two objects, and then create a relationship between the 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 = Sele
CT Count (*) from the book where cid=? ";
Number CNT = (number) qr.query (SQL, New Scalarhandler (), CID);
return Cnt.intvalue ();
catch (SQLException e) {throw new RuntimeException (e); }/** * Add Books * @param book * */public void Add (????
?,?)"; 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 Books * @param bid/public void Delete (string bid) {try {string sql = Update Book set del=true wher
E 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 ();
}
/**
* Search Books by Category *
@param CID
* @return
/Public list<book> findbycategory (String CID) { C17/>return bookdao.findbycategory (CID);
}
Public book load (String Bid) {return
bookdao.findbybid (BID);
}
/**
* Add Books * @param book * * */public
void Add (title book) {
bookdao.add);
}
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. The query gets book * 3.
Save, 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 {R
Equest.setattribute ("Booklist", Bookservice.findall ());
return "f:/jsps/book/list.jsp"; /** * Query by Category * @param request * @param response * @return * @throws servletexception * @throws ioexception * * p Ublic 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. Search All Books
Process: left.jsp (All categories)-> Bookservlet#findall ()->/jsps/book/list.jsp
3. Search 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 the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.