BookDao
1 //按分类查询 2 public PageBeanfindByCategory(String cid,int pc) throws SQLException 3 { 4 List exprList=new ArrayList (); 5 exprList.add(new Expression("cid", "=", cid)); 6 return findByCriteria(exprList, pc); 7 8 } 9 10 //按书名做一个模糊查询 11 public PageBean findByBname(String bname,int pc) throws SQLException 12 { 13 List exprList=new ArrayList (); 14 exprList.add(new Expression("bname", "like", "%"+bname+"%")); 15 return findByCriteria(exprList, pc); 16 17 } 18 19 //按作者查询 20 public PageBean findByAuthor(String author,int pc) throws SQLException 21 { 22 List exprList=new ArrayList (); 23 exprList.add(new Expression("author", "like", "%"+author+"%")); 24 return findByCriteria(exprList, pc); 25 26 } 27 28 29 //按出版社查询 30 public PageBean findByPress(String press,int pc) throws SQLException 31 { 32 List exprList=new ArrayList (); 33 exprList.add(new Expression("press", "like", "%"+press+"%")); 34 return findByCriteria(exprList, pc); 35 36 } 37 //多条件组合查询 book对象本身就是条件 38 public PageBean findByCombination(Book criteria,int pc) throws SQLException 39 { 40 List exprList=new ArrayList (); 41 exprList.add(new Expression("bname", "like", "%"+criteria.getBname()+"%")); 42 exprList.add(new Expression("author", "like", "%"+criteria.getAuthor()+"%")); 43 exprList.add(new Expression("press", "like", "%"+criteria.getPress()+"%")); 44 return findByCriteria(exprList, pc); 45 46 } 47 48 // 49 50 /* 51 * 通用的查询方法 52 */ 53 private PageBean findByCriteria(List exprList,int pc) throws SQLException 54 { 55 /* 56 * 1、得到ps 57 * 2\得到tr 总记录数 通过 通过exprList生成where子句 58 * 3\得到beanlist 59 * 4\创建PageBean 返回 60 */ 61 //得到ps 62 int ps=PageConstants.BOOK_PAGE_SIZE; 63 /* 64 *总记录数 通过exprList生成where子句 select * from t_book where 1=1 and .. 65 *条件语句不好控制所以为了后面统一 在前面先给个1=1 后面统一用and 开头 拼凑sql语句的一重要技巧 66 */ 67 StringBuilder whereSql=new StringBuilder("where 1=1"); 68 List
BookService
1 /* 2 * 图书模块业务层 3 */ 4 public class BookService { 5 private BookDao bookDao = new BookDao(); 6 7 public PageBeanfindByCategory(String cid, int pc) { 8 9 try {10 return bookDao.findByCategory(cid, pc);11 } catch (SQLException e) {12 throw new RuntimeException(e);13 }14 15 }16 17 // 按书名查询18 19 public PageBean findByBname(String bname, int pc) {20 21 try {22 return bookDao.findByBname(bname, pc);23 } catch (SQLException e) {24 throw new RuntimeException(e);25 }26 27 }28 29 // 按作者查询30 public PageBean findByAuthor(String author, int pc) {31 32 try {33 return bookDao.findByAuthor(author, pc);34 } catch (SQLException e) {35 throw new RuntimeException(e);36 }37 38 }39 40 // 按出版社查询41 public PageBean findByPress(String press, int pc) {42 43 try {44 return bookDao.findByAuthor(press, pc);45 } catch (SQLException e) {46 throw new RuntimeException(e);47 }48 49 }50 51 //多条件组合查询52 public PageBean findByCombination(Book criteria, int pc)53 throws SQLException {54 try {55 return bookDao.findByCombination(criteria, pc);56 } catch (SQLException e) {57 throw new RuntimeException(e);58 }59 60 }61 62 }
BookServlet
1 //获取当前页码值 2 private int getPc(HttpServletRequest req) 3 { 4 int pc=1; 5 String param=req.getParameter("pc"); 6 if(param!=null&&!param.trim().isEmpty()) 7 { 8 try{ 9 pc=Integer.parseInt(param);10 }catch(RuntimeException e){}11 }12 return pc;13 }14 15 //获取url 截取url 页面中的分页导航中使用它作为超链接的目标 还带了条件 保证条件不丢16 private String getUrl(HttpServletRequest req)17 { //http://localhost:8080//goods/BookServlet18 //getRequestURI()获取/goods/BookServlet19 //req.getQueryString()获取method=findByCategory&cid=xxx20 String url=req.getRequestURI()+"?"+req.getQueryString();21 //如果url中存在pc参数 截取掉 如果不存在则不用截取22 int index=url.lastIndexOf("&pc=");23 if(index!=-1)24 {25 url=url.substring(0,index);26 }27 return url;28 }29 30 public String findByCategory(HttpServletRequest req, HttpServletResponse resp)31 throws ServletException, IOException {32 33 //得到pc 如果页面传递使用页面的 如果没传pc=134 int pc=getPc(req);35 36 //2得到url37 String url=getUrl(req);38 //3获取查询条件 本方法是cid 即分类id39 String cid=req.getParameter("cid");40 41 //使用pc cid 调用service 42 PageBeanpb=bookService.findByCategory(cid,pc);43 //使用pageBean设置url 保存pageBean 转发到/jsps/book/list.jsp页面44 pb.setUrl(url); 45 req.setAttribute("pb", pb);46 return "f:/jsps/book/list.jsp";47 48 49 }
left.jsp
1
- 2
- 4 5 7
8 ¥${book.currPrice} 9 ¥${book.price}10 (${book.discount}折)11
1213 ${book.bname }16
1718 ${book.author }20
2122 出 版 社:${book.press} }23
2425 出版时间:${book.publishtime}26
27 28