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

一、实现多条件查询

public List<Product> findProductListByCondition(Condition condition) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
//定义一个存储实际参数的容器
List<String> list = new ArrayList<String>();
String sql = "select * from product where 1=1";
if(condition.getPname()!=null&&!condition.getPname().trim().equals("")){
sql+=" and pname like ? ";
list.add("%"+condition.getPname().trim()+"%");
}
if(condition.getIsHot()!=null&&!condition.getIsHot().trim().equals("")){
sql+=" and is_hot=? ";
list.add(condition.getIsHot().trim());
}
if(condition.getCid()!=null&&!condition.getCid().trim().equals("")){
sql+=" and cid=? ";
list.add(condition.getCid().trim());
} List<Product> productList = runner.query(sql, new BeanListHandler<Product>(Product.class) , list.toArray()); return productList;
}

二、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用于存储分页的数据

public class PageBean<T> {

    //当前页
private int currentPage;
//当前页显示的条数
private int currentCount;
//总条数
private int totalCount;
//总页数
private int totalPage;
//每页显示的数据
private List<T> productList = new ArrayList<T>(); public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getCurrentCount() {
return currentCount;
}
public void setCurrentCount(int currentCount) {
this.currentCount = currentCount;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getProductList() {
return productList;
}
public void setProductList(List<T> productList) {
this.productList = productList;
} }

2、WEB层代码

public class ProductListServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { ProductService service = new ProductService(); //模拟当前是第一页
String currentPageStr = request.getParameter("currentPage");
if(currentPageStr==null) currentPageStr="1";
int currentPage = Integer.parseInt(currentPageStr);
//认为每页显示12条
int currentCount = 12; PageBean<Product> pageBean = null;
try {
pageBean = service.findPageBean(currentPage,currentCount);
} catch (SQLException e) {
e.printStackTrace();
} request.setAttribute("pageBean", pageBean); request.getRequestDispatcher("/product_list.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

3、Service层代码

public class ProductService {

    public List<Product> findAllProduct() throws SQLException {
ProductDao dao = new ProductDao();
return dao.findAllProduct();
} //分页操作
public PageBean findPageBean(int currentPage,int currentCount) throws SQLException { ProductDao dao = new ProductDao(); //目的:就是想办法封装一个PageBean 并返回
PageBean pageBean = new PageBean();
//1、当前页private int currentPage;
pageBean.setCurrentPage(currentPage);
//2、当前页显示的条数private int currentCount;
pageBean.setCurrentCount(currentCount);
//3、总条数private int totalCount;
int totalCount = dao.getTotalCount();
pageBean.setTotalCount(totalCount);
//4、总页数private int totalPage;
/*
* 总条数 当前页显示的条数 总页数
* 10 4 3
* 11 4 3
* 12 4 3
* 13 4 4
*
* 公式:总页数=Math.ceil(总条数/当前显示的条数)
*
*/
int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
pageBean.setTotalPage(totalPage);
//5、每页显示的数据private List<T> productList = new ArrayList<T>();
/*
* 页数与limit起始索引的关系
* 例如 每页显示4条
* 页数 其实索引 每页显示条数
* 1 0 4
* 2 4 4
* 3 8 4
* 4 12 4
*
* 索引index = (当前页数-1)*每页显示的条数
*
*/
int index = (currentPage-1)*currentCount; List<Product> productList = dao.findProductListForPageBean(index,currentCount);
pageBean.setProductList(productList); return pageBean;
} }

4、DAO层代码

public class ProductDao {

    public List<Product> findAllProduct() throws SQLException {
return new QueryRunner(DataSourceUtils.getDataSource()).query("select * from product", new BeanListHandler<Product>(Product.class));
} //获得全部的商品条数
public int getTotalCount() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select count(*) from product";
Long query = (Long) runner.query(sql, new ScalarHandler());
return query.intValue();
} //获得分页的商品数据
public List<Product> findProductListForPageBean(int index,int currentCount) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product limit ?,?";
return runner.query(sql, new BeanListHandler<Product>(Product.class), index,currentCount);
} }

5、JSP页面代码

<div class="row" style="width: 1210px; margin: 0 auto;">
<div class="col-md-12">
<ol class="breadcrumb">
<li><a href="#">首页</a></li>
</ol>
</div> <c:forEach items="${pageBean.productList }" var="product">
<div class="col-md-2" style="height:250px">
<a href="product_info.htm">
<img src="${pageContext.request.contextPath }/${product.pimage}" width="170" height="170" style="display: inline-block;">
</a>
<p>
<a href="product_info.html" style='color: green'>${product.pname }</a>
</p>
<p>
<font color="#FF0000">商城价:&yen;${product.shop_price }</font>
</p>
</div>
</c:forEach> </div> <!--分页 -->
<div style="width: 380px; margin: 0 auto; margin-top: 50px;">
<ul class="pagination" style="text-align: center; margin-top: 10px;">
<!-- 上一页 -->
<!-- 判断当前页是否是第一页 -->
<c:if test="${pageBean.currentPage==1 }">
<li class="disabled">
<a href="javascript:void(0);" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.currentPage!=1 }">
<li>
<a href="${pageContext.request.contextPath }/productList?currentPage=${pageBean.currentPage-1}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
</c:if> <c:forEach begin="1" end="${pageBean.totalPage }" var="page">
<!-- 判断当前页 -->
<c:if test="${pageBean.currentPage==page }">
<li class="active"><a href="javascript:void(0);">${page}</a></li>
</c:if>
<c:if test="${pageBean.currentPage!=page }">
<li><a href="${pageContext.request.contextPath }/productList?currentPage=${page}">${page}</a></li>
</c:if> </c:forEach> <!-- 判断当前页是否是最后一页 -->
<c:if test="${pageBean.currentPage==pageBean.totalPage }">
<li class="disabled">
<a href="javascript:void(0);" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.currentPage!=pageBean.totalPage }">
<li>
<a href="${pageContext.request.contextPath }/productList?currentPage=${pageBean.currentPage+1}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</c:if> </ul>
</div>
<!-- 分页结束 -->

【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. STM32GPIO口8种模式细致分析(类比51单片机)

    关于STM32GPIO口的8种工作模式,我们先引出一些问题? STM32GPIO口如果既要输入又要输出怎么办? 1.浮空输入模式 上图红色的表示便是浮空输入的过程,外部输入时0读出的就是0,外部输入时 ...

  2. WPF之路一:相对路径图片显示

    由于公司项目的需要,改为WPF开发,因此需要学习WPF,遇到的第一个问题就是在显示的图片的时候,写绝对路径,图片显示没有问题,但是写相对路径的时候,发现图片无法正常显示,在网上搜了一下,得到的答案是需 ...

  3. Python之路-Linux命令基础(3)

    作业一: 1)将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) 2)将用户信息数据库文件和用户密码数据库文件纵向合并为一个文件/2.txt(追加) 3)将/1.txt./2 ...

  4. (删)Java线程同步实现二:Lock锁和Condition

    在上篇文章(3.Java多线程总结系列:Java的线程同步实现)中,我们介绍了用synchronized关键字实现线程同步.但在Java中还有一种方式可以实现线程同步,那就是Lock锁. 一.同步锁 ...

  5. Oracle常用数据字典

    1.查看所有存储过程.索引.表格.PACKAGE.PACKAGE BODY select * from user_objects; 2.查询所有的Job select * from user_jobs ...

  6. VS 2017 Git failed with a fatal error的解决办法

    前几天,满怀欣喜的从VS2015更新到了VS2017,经过这几天的试用,整体来说感觉还是挺不错的.昨天推送项目到远程服务器的时候,发现出现了推送失败的错误,错误如图: 按照提示,我看到输出窗口的输入内 ...

  7. Intellij IDEA 没办法创建java文件

    然后就是具体的解释和解决方案. 如上图红圈所示,我们可以根据对项目的任意目录进行这五种目录类型标注,这个知识点非常非常重要,必须会. Sources 一般用于标注类似 src 这种可编译目录.有时候我 ...

  8. 转账示例(二):service层面实现(本例采用QueryRunner来执行sql语句,数据源为C3P0)

    缺点:Service层面把Dao层面的开启事务操作完成了1.自行创建C3P0Uti,account数据库,导入Jar包 2.Dao层面 接口: package com.learning.dao; im ...

  9. 1001. Exponentiation高精度运算总结

    解题思路 这道题属于高精度乘法运算,要求输入一个实数R一个指数N,求实数R的N次方,由于R有5个数位,而N又特别大,因此用C++自带的数据类型放不下. 解题思路是通过数组储存每次乘积结果和底数的每一位 ...

  10. CF #345 Div1 D Zip-line

    题目链接:http://codeforces.com/contest/650/problem/D 大意是给一个数组,若干询问,每一次把一个数字改为另一个数字,问当前数组最长上升子序列,询问之间是独立的 ...