代码仓库+文档:https://gitee.com/DerekAndroid/ServletJDBCSearchPage.git
效果:
分析:
批量删除
搜索
具体思路:
1.批量删除商品
需求:根据数据前面的复选框选中情况,批量删除选中的数据
步骤分析:
1.拷贝day17 修改web
2.为表头和列表加上复选框,加上删除勾选的按钮
3.使用jquery实现全选全不选的功能
//导入jquery.js文件
//派发单击事件
//获取表头复选框的选中状态
//获取列表中所有复选框对象
//使列表中复选框状态和表头复选框状态保持一致
4.为列表复选框添加name属性和value 外面嵌套一个form表单 在点击删除勾选的时候派发一个click事件,让事件方法拥有提交form表单的功能
5.servlet的操作
//获取前台传递的id数组
//调用service
6.service操作
//开启事物
//遍历数组
//调用dao删除
//提交事物
//出现异常,回滚事物
7.dao操作
//获取QueryRunner对象
//编写sql
//执行sql(手动获取连接)
2.模糊查询
需求:在列表页面有按照名称和关键字查询的两个输入框,输入查询信息后,点击搜索按钮,那么在下面的列表中会显示出符合条件的数据
select * from product where pname like ? and pdesc like ?
select * from product where pname like ?
select * from product where pdesc like ?
select * from product
StringBuffer sb = new StringBuffer("select * from where 1=1");
//判断输入的搜索项是否为“”
if(name!=""){
sb.append( and pname like ?)
}
if(kw!=""){
sb.append( and pdesc like ?)
}
3.分页
需求:针对于从数据库查询的多条数据,展示在一个页面上 分页数展示
作用:提高用户的体验度
物理分页:用户需要看那一页的数据,那么就从数据库中查询出来当页的数据
逻辑分页:把数据库中的所有数据查询出来,保存到内存中,当用户需要看某一页的数据的时候,直接从内存中读取
mysql数据库分页:
limit n,m (n是查询的索引,m查询几个)
第一页: 0 3
第二页: 3 3
第三页: 6 3
第n页: (当前页-1)*m ,m
分页需要的数据:
1.页面展示的内容 select * from product limit (当前页-1)*m ,m
2.每页显示的条数 自己规定
3.当前页 从前台传递过去
4.总条数 select count(*) from product
5.总页数 总条数/每页显示的条数 向上取整
后台实现:
封装为PageBean
1.List list; //页面展示的内容 select * from product limit (pageNumber-1)*pageSize ,pageSize
2.int pageSize; //每页显示的条数 自己规定
3.int pageNumber //当前页 从前台传递过去
4.int pageCount //总条数 select count(*) from product
5.int pageTotle //总页数 (int)Math.ceil(pageCount*1.0/pageSize)
前台实现:
[上一页]1 2 3 4 5 6 7[下一页] 第几页/共几页
sql表
CREATE DATABASE day17;
USE day17;
CREATE TABLE `product` (
`pid` VARCHAR (96),
`pname` VARCHAR (150),
`market_price` DOUBLE ,
`shop_price` DOUBLE ,
`pimage` VARCHAR (600),
`pdate` DATE ,
`pdesc` VARCHAR (765)
);
INSERT INTO `product` VALUES('1','小米 4c 标准版','1399','1299','products/1/c_0001.jpg','2015-11-02','小米 4c 标准版 全网通 白色 移动联通电信4G手机 双卡双待');
INSERT INTO `product` VALUES('10','华为 Ascend Mate7','2699','2599','products/1/c_0010.jpg','2015-11-02','华为 Ascend Mate7 月光银 移动4G手机 双卡双待双通6英寸高清大屏,纤薄机身,智能超八核,按压式指纹识别!!选择下方“移动老用户4G飞享合约”,无需换号,还有话费每月返还!');
INSERT INTO `product` VALUES('11','vivo X5Pro','2399','2298','products/1/c_0014.jpg','2015-11-02','移动联通双4G手机 3G运存版 极光白【购机送蓝牙耳机+蓝牙自拍杆】新升级3G运行内存·双2.5D弧面玻璃·眼球识别技术');
INSERT INTO `product` VALUES('12','努比亚(nubia)My 布拉格','1899','1799','products/1/c_0013.jpg','2015-11-02','努比亚(nubia)My 布拉格 银白 移动联通4G手机 双卡双待【嗨11,下单立减100】金属机身,快速充电!布拉格相机全新体验!');
INSERT INTO `product` VALUES('13','华为 麦芒4','2599','2499','products/1/c_0012.jpg','2015-11-02','华为 麦芒4 晨曦金 全网通版4G手机 双卡双待金属机身 2.5D弧面屏 指纹解锁 光学防抖');
INSERT INTO `product` VALUES('14','vivo X5M','1899','1799','products/1/c_0011.jpg','2015-11-02','vivo X5M 移动4G手机 双卡双待 香槟金【购机送蓝牙耳机+蓝牙自拍杆】5.0英寸大屏显示·八核双卡双待·Hi-Fi移动KTV');
INSERT INTO `product` VALUES('15','Apple iPhone 6 (A1586)','4399','4288','products/1/c_0015.jpg','2015-11-02','Apple iPhone 6 (A1586) 16GB 金色 移动联通电信4G手机长期省才是真的省!点击购机送费版,月月送话费,月月享优惠,畅享4G网络,就在联通4G!');
数据库操作Dao-ProductDao
package com.itheima.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.itheima.bean.PageBean;
import com.itheima.bean.Product;
import com.itheima.utils.DataSourceUtils;
public class ProductDao {
/**
* 查询所有商品
* @return
* @throws SQLException
*/
public List<Product> findAll() throws SQLException {
//创建QueryRunner
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
//编写sql
String sql = "select * from product";
//执行sql
List<Product> query = qr.query(sql, new BeanListHandler<Product>(Product.class));
return query;
}
/*
*添加商品
*/
public void saveProduct(Product pro) throws SQLException {
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into product values(?,?,?,?,?,?,?)";
qr.update(sql, pro.getPid(),pro.getPname(),pro.getMarket_price(),pro.getShop_price(),pro.getPimage(),pro.getPdate(),pro.getPdesc());
}
/**
* 根据id查询数据
* @param id
* @return
* @throws SQLException
*/
public Product getProByPid(String id) throws SQLException {
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where pid=?";
Product query = qr.query(sql, new BeanHandler<Product>(Product.class), id);
return query;
}
/**
* 根据id修改商品
* @param pro
* @throws SQLException
*/
public void updatePro(Product pro) throws SQLException {
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "update product set pname=?,market_price=?,shop_price=?,pdesc=? where pid=?";
qr.update(sql, pro.getPname(),pro.getMarket_price(),pro.getShop_price(),pro.getPdesc(),pro.getPid());
}
/**
* 根据id删除商品信息
* @param pid
* @throws SQLException
*/
public void deletePro(String pid) throws SQLException {
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "delete from product where pid = ?";
qr.update(sql, pid);
}
/**
* 批量删除
* @param id
* @throws SQLException
*/
public void delCheck(String id) throws SQLException {
//创建QueryRunner对象(不带参数)
QueryRunner qr = new QueryRunner();
//编写sql
String sql = "delete from product where pid = ?";
//执行sql(手动获取连接)
qr.update(DataSourceUtils.getConnection(), sql, id);
}
/**
* 模糊查询
* @param name
* @param kw
* @return
* @throws SQLException
*/
public List<Product> search(String name, String kw) throws SQLException {
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
StringBuilder sb = new StringBuilder("select * from product where 1=1");
List<String> list = new ArrayList<>();
System.out.println(name+kw);
if(name!=""){
sb.append(" and pname like ?");
list.add("%"+name+"%");
}
if(kw!=""){
sb.append(" and pdesc like ?");
list.add("%"+kw+"%");
}
List<Product> query = qr.query(sb.toString(), new BeanListHandler<Product>(Product.class), list.toArray());
return query;
}
/**
* 获取需要显示的数据
* @param pb
* @return
* @throws SQLException
*/
public List<Product> getList(PageBean<Product> pb) throws SQLException {
// TODO Auto-generated method stub
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product limit ?,?";
List<Product> query = qr.query(sql, new BeanListHandler<Product>(Product.class), pb.getIndex(),pb.getPageSize());
return query;
}
/**
* 获取总条数
* @return
* @throws SQLException
*/
public int getPageCount() throws SQLException {
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select count(*) from product";
int intValue = ((Long)qr.query(sql,new ScalarHandler())).intValue();
return intValue;
}
}
分页业务bean
package com.itheima.bean;
import java.util.List;
public class PageBean<T> {
List<T> list; //页面展示的内容 select * from product limit (pageNumber-1)*pageSize ,pageSize
private int pageSize; //每页显示的条数 自己规定
private int pageNumber; //当前页 从前台传递过去
private int pageCount; //总条数 select count(*) from product
private int pageTotle; //总页数 (int)Math.ceil(pageCount*1.0/pageSize)
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public void setPageTotle(int pageTotle) {
this.pageTotle = pageTotle;
}
//获取总页数
public int getPageTotle() {
return (int)Math.ceil(pageCount*1.0/pageSize);
}
//获取数据索引
public int getIndex(){
return (pageNumber-1)*pageSize;
}
//定义构造
public PageBean(int pageSize, int pageNumber) {
super();
this.pageSize = pageSize;
this.pageNumber = pageNumber;
}
}