javaweb图书商城设计之图书模块(4)
程序员文章站
2024-03-11 14:13:13
本文接着上一篇图书商城分类模块进行学习,供大家参考,具体内容如下
1、创建相关类
cn.itcast.bookstore.book
domain:book
dao:...
本文接着上一篇图书商城分类模块进行学习,供大家参考,具体内容如下
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: QR 二维码中插入图片实现方法