今天主要学习了数据库的多条件查询、attr和prop的区别和分页的实现

一、实现多条件查询

  1. public List<Product> findProductListByCondition(Condition condition) throws SQLException {
  2. QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
  3. //定义一个存储实际参数的容器
  4. List<String> list = new ArrayList<String>();
  5. String sql = "select * from product where 1=1";
  6. if(condition.getPname()!=null&&!condition.getPname().trim().equals("")){
  7. sql+=" and pname like ? ";
  8. list.add("%"+condition.getPname().trim()+"%");
  9. }
  10. if(condition.getIsHot()!=null&&!condition.getIsHot().trim().equals("")){
  11. sql+=" and is_hot=? ";
  12. list.add(condition.getIsHot().trim());
  13. }
  14. if(condition.getCid()!=null&&!condition.getCid().trim().equals("")){
  15. sql+=" and cid=? ";
  16. list.add(condition.getCid().trim());
  17. }
  18.  
  19. List<Product> productList = runner.query(sql, new BeanListHandler<Product>(Product.class) , list.toArray());
  20.  
  21. return productList;
  22. }

二、jquery中attr和prop的区别

在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了。

关于它们两个的区别,网上的答案很多。这里谈谈我的心得,我的心得很简单:

  • 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
  • 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。

attr和prop的区别我是通过这个了解到的:http://www.cnblogs.com/Showshare/p/different-between-attr-and-prop.html

三、分页的实现:

1、创建一个PageBean用于存储分页的数据

  1. public class PageBean<T> {
  2.  
  3. //当前页
  4. private int currentPage;
  5. //当前页显示的条数
  6. private int currentCount;
  7. //总条数
  8. private int totalCount;
  9. //总页数
  10. private int totalPage;
  11. //每页显示的数据
  12. private List<T> productList = new ArrayList<T>();
  13.  
  14. public int getCurrentPage() {
  15. return currentPage;
  16. }
  17. public void setCurrentPage(int currentPage) {
  18. this.currentPage = currentPage;
  19. }
  20. public int getCurrentCount() {
  21. return currentCount;
  22. }
  23. public void setCurrentCount(int currentCount) {
  24. this.currentCount = currentCount;
  25. }
  26. public int getTotalCount() {
  27. return totalCount;
  28. }
  29. public void setTotalCount(int totalCount) {
  30. this.totalCount = totalCount;
  31. }
  32. public int getTotalPage() {
  33. return totalPage;
  34. }
  35. public void setTotalPage(int totalPage) {
  36. this.totalPage = totalPage;
  37. }
  38. public List<T> getProductList() {
  39. return productList;
  40. }
  41. public void setProductList(List<T> productList) {
  42. this.productList = productList;
  43. }
  44.  
  45. }

2、WEB层代码

  1. public class ProductListServlet extends HttpServlet {
  2.  
  3. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  4. throws ServletException, IOException {
  5.  
  6. ProductService service = new ProductService();
  7.  
  8. //模拟当前是第一页
  9. String currentPageStr = request.getParameter("currentPage");
  10. if(currentPageStr==null) currentPageStr="1";
  11. int currentPage = Integer.parseInt(currentPageStr);
  12. //认为每页显示12条
  13. int currentCount = 12;
  14.  
  15. PageBean<Product> pageBean = null;
  16. try {
  17. pageBean = service.findPageBean(currentPage,currentCount);
  18. } catch (SQLException e) {
  19. e.printStackTrace();
  20. }
  21.  
  22. request.setAttribute("pageBean", pageBean);
  23.  
  24. request.getRequestDispatcher("/product_list.jsp").forward(request, response);
  25.  
  26. }
  27.  
  28. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  29. throws ServletException, IOException {
  30. doGet(request, response);
  31. }
  32. }

3、Service层代码

  1. public class ProductService {
  2.  
  3. public List<Product> findAllProduct() throws SQLException {
  4. ProductDao dao = new ProductDao();
  5. return dao.findAllProduct();
  6. }
  7.  
  8. //分页操作
  9. public PageBean findPageBean(int currentPage,int currentCount) throws SQLException {
  10.  
  11. ProductDao dao = new ProductDao();
  12.  
  13. //目的:就是想办法封装一个PageBean 并返回
  14. PageBean pageBean = new PageBean();
  15. //1、当前页private int currentPage;
  16. pageBean.setCurrentPage(currentPage);
  17. //2、当前页显示的条数private int currentCount;
  18. pageBean.setCurrentCount(currentCount);
  19. //3、总条数private int totalCount;
  20. int totalCount = dao.getTotalCount();
  21. pageBean.setTotalCount(totalCount);
  22. //4、总页数private int totalPage;
  23. /*
  24. * 总条数 当前页显示的条数 总页数
  25. * 10 4 3
  26. * 11 4 3
  27. * 12 4 3
  28. * 13 4 4
  29. *
  30. * 公式:总页数=Math.ceil(总条数/当前显示的条数)
  31. *
  32. */
  33. int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
  34. pageBean.setTotalPage(totalPage);
  35. //5、每页显示的数据private List<T> productList = new ArrayList<T>();
  36. /*
  37. * 页数与limit起始索引的关系
  38. * 例如 每页显示4条
  39. * 页数 其实索引 每页显示条数
  40. * 1 0 4
  41. * 2 4 4
  42. * 3 8 4
  43. * 4 12 4
  44. *
  45. * 索引index = (当前页数-1)*每页显示的条数
  46. *
  47. */
  48. int index = (currentPage-1)*currentCount;
  49.  
  50. List<Product> productList = dao.findProductListForPageBean(index,currentCount);
  51. pageBean.setProductList(productList);
  52.  
  53. return pageBean;
  54. }
  55.  
  56. }

4、DAO层代码

  1. public class ProductDao {
  2.  
  3. public List<Product> findAllProduct() throws SQLException {
  4. return new QueryRunner(DataSourceUtils.getDataSource()).query("select * from product", new BeanListHandler<Product>(Product.class));
  5. }
  6.  
  7. //获得全部的商品条数
  8. public int getTotalCount() throws SQLException {
  9. QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
  10. String sql = "select count(*) from product";
  11. Long query = (Long) runner.query(sql, new ScalarHandler());
  12. return query.intValue();
  13. }
  14.  
  15. //获得分页的商品数据
  16. public List<Product> findProductListForPageBean(int index,int currentCount) throws SQLException {
  17. QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
  18. String sql = "select * from product limit ?,?";
  19. return runner.query(sql, new BeanListHandler<Product>(Product.class), index,currentCount);
  20. }
  21.  
  22. }

5、JSP页面代码

  1. <div class="row" style="width: 1210px; margin: 0 auto;">
  2. <div class="col-md-12">
  3. <ol class="breadcrumb">
  4. <li><a href="#">首页</a></li>
  5. </ol>
  6. </div>
  7.  
  8. <c:forEach items="${pageBean.productList }" var="product">
  9. <div class="col-md-2" style="height:250px">
  10. <a href="product_info.htm">
  11. <img src="${pageContext.request.contextPath }/${product.pimage}" width="170" height="170" style="display: inline-block;">
  12. </a>
  13. <p>
  14. <a href="product_info.html" style='color: green'>${product.pname }</a>
  15. </p>
  16. <p>
  17. <font color="#FF0000">商城价:&yen;${product.shop_price }</font>
  18. </p>
  19. </div>
  20. </c:forEach>
  21.  
  22. </div>
  23.  
  24. <!--分页 -->
  25. <div style="width: 380px; margin: 0 auto; margin-top: 50px;">
  26. <ul class="pagination" style="text-align: center; margin-top: 10px;">
  27. <!-- 上一页 -->
  28. <!-- 判断当前页是否是第一页 -->
  29. <c:if test="${pageBean.currentPage==1 }">
  30. <li class="disabled">
  31. <a href="javascript:void(0);" aria-label="Previous">
  32. <span aria-hidden="true">&laquo;</span>
  33. </a>
  34. </li>
  35. </c:if>
  36. <c:if test="${pageBean.currentPage!=1 }">
  37. <li>
  38. <a href="${pageContext.request.contextPath }/productList?currentPage=${pageBean.currentPage-1}" aria-label="Previous">
  39. <span aria-hidden="true">&laquo;</span>
  40. </a>
  41. </li>
  42. </c:if>
  43.  
  44. <c:forEach begin="1" end="${pageBean.totalPage }" var="page">
  45. <!-- 判断当前页 -->
  46. <c:if test="${pageBean.currentPage==page }">
  47. <li class="active"><a href="javascript:void(0);">${page}</a></li>
  48. </c:if>
  49. <c:if test="${pageBean.currentPage!=page }">
  50. <li><a href="${pageContext.request.contextPath }/productList?currentPage=${page}">${page}</a></li>
  51. </c:if>
  52.  
  53. </c:forEach>
  54.  
  55. <!-- 判断当前页是否是最后一页 -->
  56. <c:if test="${pageBean.currentPage==pageBean.totalPage }">
  57. <li class="disabled">
  58. <a href="javascript:void(0);" aria-label="Next">
  59. <span aria-hidden="true">&raquo;</span>
  60. </a>
  61. </li>
  62. </c:if>
  63. <c:if test="${pageBean.currentPage!=pageBean.totalPage }">
  64. <li>
  65. <a href="${pageContext.request.contextPath }/productList?currentPage=${pageBean.currentPage+1}" aria-label="Next">
  66. <span aria-hidden="true">&raquo;</span>
  67. </a>
  68. </li>
  69. </c:if>
  70.  
  71. </ul>
  72. </div>
  73. <!-- 分页结束 -->

【JAVAWEB学习笔记】21_多条件查询、attr和prop的区别和分页的实现的更多相关文章

  1. jQuery学习笔记(四):attr()与prop()的区别

    这一节针对attr()与prop()之间的区别进行学习. 先看看官方文档是如何解释两者之间功能差异的: attr() Get the value of an attribute for the fir ...

  2. JavaWeb学习笔记总结 目录篇

    JavaWeb学习笔记一: XML解析 JavaWeb学习笔记二 Http协议和Tomcat服务器 JavaWeb学习笔记三 Servlet JavaWeb学习笔记四 request&resp ...

  3. 数据库学习笔记3 基本的查询流 2 select lastname+','+firstname as fullname order by lastname+','+firstname len() left() stuff() percent , select top(3) with ties

    数据库学习笔记3 基本的查询流 2   order by子句对查询结果集进行排序 多列和拼接 多列的方式就很简单了 select firstname,lastname from person.pers ...

  4. IBatis.Net学习笔记六--再谈查询

    在IBatis.Net学习笔记五--常用的查询方式 中我提到了一些IBatis.Net中的查询,特别是配置文件的写法. 后来通过大家的讨论,特别是Anders Cui 的提醒,又发现了其他的多表查询的 ...

  5. 【python学习笔记】5.条件、循环和其他语句

    [python学习笔记]5.条件.循环和其他语句 print: 用来打印表达式,不管是字符串还是其他类型,都输出以字符串输出:可以通过逗号分隔输出多个表达式 import: 导入模块     impo ...

  6. SQL反模式学习笔记18 减少SQL查询数据,避免使用一条SQL语句解决复杂问题

    目标:减少SQL查询数据,避免使用一条SQL语句解决复杂问题 反模式:视图使用一步操作,单个SQL语句解决复杂问题 使用一个查询来获得所有结果的最常见后果就是产生了一个笛卡尔积.导致查询性能降低. 如 ...

  7. Go语言学习笔记五: 条件语句

    Go语言学习笔记五: 条件语句 if语句 if 布尔表达式 { /* 在布尔表达式为 true 时执行 */ } 竟然没有括号,和python很像.但是有大括号,与python又不一样. 例子: pa ...

  8. 《python基础教程(第二版)》学习笔记 语句/循环/条件(第5章)

    <python基础教程(第二版)>学习笔记 语句/循环/条件(第5章) print 'AB', 123 ==> AB 123 # 插入了一个空格print 'AB', 'CD' == ...

  9. javaweb学习笔记整理补课

    javaweb学习笔记整理补课 * JavaWeb: * 使用Java语言开发基于互联网的项目 * 软件架构: 1. C/S: Client/Server 客户端/服务器端 * 在用户本地有一个客户端 ...

随机推荐

  1. 老李推荐:第14章9节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-遍历控件树查找控件

    老李推荐:第14章9节<MonkeyRunner源码剖析> HierarchyViewer实现原理-遍历控件树查找控件   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员 ...

  2. (转)JAVA多线程和并发基础面试问答

    JAVA多线程和并发基础面试问答 原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-ans ...

  3. 使用SharedPreferences存储用户配置信息

    用SharedPreferences来保存用户的基本配置信息非常的方便,实现起来也很容易:以下是一个简单的例子: 效果截图:    主要代码: public class MainActivity ex ...

  4. Android系统结构

    从上图中可以看出,Android系统架构为四层结构,从上层到下层分别是应用程序层.应用程序框架层.系统运行库层以及Linux内核层,分别介绍如下: (1)Appliacation Android平台不 ...

  5. JS学习中遇到的一些题目

    1.找出所有的水仙花数: 水仙花数例如:153 的特点: 1^3+5^3+3^=153 而且水仙花数只会是三位数,所以可以利用循环的方式来解决问题,循环条件可以设为: var i = 1;i < ...

  6. nginx反向代理的nginx.conf配置

    下面的配置是nginx.conf的示例 nginx反向代理 就是说把跨域的url通过本地代理的方式,变成同域的请求,如此来解决跨域问题 该配置下 通过http://localhost/html5/路径 ...

  7. canvas绘制一定数目的圆(均分)

    绘制多圆 2016年5月24日12:12:26 绘制一定数目(num)颜色随机的小圆,围成一个大圆.根据num完全自动生成,且小圆自动均分大圆路径(num≥20). 效果: 前置技能:(1).Canv ...

  8. Spring+IOC(DI)+AOP概念及优缺点

    Spring pring是一个轻量级的DI和AOP容器框架. 说它轻量级有一大部分原因是相对与EJB的(虽然本人从没有接触过EJB的应用),重要的是,Spring是非侵入式的,基于spring开发的应 ...

  9. 第六章 Hibernate关联映射

    第六章 hibernate关联映射一.本章知识点分为2部分:1.关联关系:单向多对一关联关系,双向一对多关联关系(含一对多关联关系),多对多关联关系2.延迟加载:类级别加载策略,一对多加载策略,多对一 ...

  10. 京东笔试---通过考试(DP)

    题目描述      小明同学要参加一场考试,考试一共有n道题目,小明必须作对至少60%的题目才能通过考试.考试结束后,小明估算出每题作对的概率,p1,p2,...,pn,你能帮他算出他通过考试的概率吗 ...