java条件查询分页_Web 条件查询、分页查

在table表格上面 创建几个按钮

1

2

3

4

5

6

7

8 rel="stylesheet" type="text/css" />

9

11

12 function addProduct() {13 window.location.href = "${pageContext.request.contextPath}/AddProductUIServlet";14 }15 function del(pid){16 var isdel=confirm("确认删除吗?");17 if(isdel){18 location.href = "${pageContext.request.contextPath}/DeleteProductServlet?pid="+pid;19 }20 }21

22

23

24

25

26 action="${pageContext.request.contextPath}/ConditionServlet"

27 method="post">

28 商品名称:

29 是否热门:

30 请选择

31 是

32 否

33

34 所属分类:

35 请选择

36

37 ${cate.cname }

38

39

40

41

42 bgColor="#f5fafe" border="0">

43

44

45

商品列表

46

47

48

49

50

51 class="button_add" οnclick="addProduct()">

52 添加

53

54

55

56

57

58

59 bordercolor="gray" border="1" id="DataGrid1"

60 style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; WIDTH: 100%; WORD-BREAK: break-all; BORDER-BOTTOM: gray 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #f5fafe; WORD-WRAP: break-word">

61

63

64

序号

65

商品图片

66

商品名称

67

商品价格

68

是否热门

69

编辑

70

删除

71

72

73

74 οnmοuseοut="this.style.backgroundColor = '#F5FAFE';">

75

76 width="18%" >${vs.count }

77

78 width="17%">

79 src="${pageContext.request.contextPath }/${pro.pimage}">

80

81 width="17%">${pro.pname }

82

83 width="17%">${pro.market_price }

84

85 width="17%">${pro.is_hot==1?"是":"否" }

86

88

90 border="0" style="CURSOR: hand">

91

92

93

95 width="16" height="16" border="0" style="CURSOR: hand">

96

97

98

99

100

101

102

103

104

105

106

107

黄色为添加的功能

5bc5be3293fc7fa8f159182fa186e397.png

d5a55edcd5cf8582fc53edd7104996a2.png

然后将这三个条件封装起来定义实体类 Condition (条件)

在web包下 新建个Servlet,起名跟上面jsp 26行,ConditionServlet  完全一致才可匹配

packagecom.oracle.domain;//创建实体类 Condition 条件

public classCondition {//将页面搜索的三个条件 封装

privateString pname;privateString is_hot;privateString cid;publicString getPname() {returnpname;

}public voidsetPname(String pname) {this.pname =pname;

}publicString getIs_hot() {returnis_hot;

}public voidsetIs_hot(String is_hot) {this.is_hot =is_hot;

}publicString getCid() {returncid;

}public voidsetCid(String cid) {this.cid =cid;

}

@OverridepublicString toString() {return "Condition [pname=" + pname + ", is_hot=" + is_hot + ", cid=" + cid + "]";

}

}

在Web层新建 ConditionServlet

packagecom.oracle.web;//创建条件Servlet

importjava.io.IOException;importjava.lang.reflect.InvocationTargetException;importjava.util.List;importjava.util.Map;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.commons.beanutils.BeanUtils;importcom.oracle.domain.Category;importcom.oracle.domain.Condition;importcom.oracle.domain.Product;importcom.oracle.service.CategoryService;importcom.oracle.service.ProductService;public class ConditionServlet extendsHttpServlet {private ProductService productService=newProductService();private CategoryService categoryService=newCategoryService();public void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//解决乱码

request.setCharacterEncoding("UTF-8");//获取所有条件参数所在的Map集合

Map map =request.getParameterMap();//创建Condition对象

Condition condition=newCondition();//用BeanUtils进行封装

try{

BeanUtils.populate(condition, map);

}catch (IllegalAccessException |InvocationTargetException e) {//TODO Auto-generated catch block

e.printStackTrace();

}//调用Service方法//获取根据条件查询的商品列表

List list=productService.getProductByCondition(condition);//获取所有分类

List list2=categoryService.getCategory();//往域中放值 这里必须跟AdminProductListServlet 下的两个值完全一致

request.setAttribute("ProductList", list);

request.setAttribute("CategoryList", list2);

// 这里必须要跟 AdminProductListServlet 下的两个值要完全一致

request.setAttribute("Category", condition);//请求转发

request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response);

}public void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

doGet(request, response);

}

}

在Dao层下创建 ProductDao

//根据Condition条件查询商品

public List getProductByCondition(Condition condition) throwsSQLException{//创建QueryRunner对象

QueryRunner qr=newQueryRunner(MyDBUtils.getDataSource());//获取sql

String sql="select * from product where 1=1";//where1=1是一个恒等:不管下面对不对,上面语句永远没问题//定义个数组

ArrayList arr=new ArrayList();//两个条件:1.condition不等于空 2. condition.getname不等于空串

if(condition.getPname()!=null&&condition.getPname().trim()!=""){

sql+=" and pname like ?";

arr.add("%"+condition.getPname()+"%");//这里需要加% 因为有like 下面就不用加了

}if(condition.getIs_hot()!=null&&condition.getIs_hot()!=""){

sql+=" and is_hot=?";

arr.add(condition.getIs_hot());

}if(condition.getCid()!=null&&condition.getCid().trim()!=""){

sql+=" and cid=?";

arr.add(condition.getCid());

}

List list=qr.query(sql, new BeanListHandler(Product.class),arr.toArray());returnlist;

}

在service层创建ProductService

//根据Condition查询商品

public ListgetProductByCondition(Condition condition) {

List list = null;try{

list=productDao.getProductByCondition(condition);

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}returnlist;

}

因为按照条件查询的话,所有商品的分类也要得到,所以在web包下 ConditionServlet里面将categoryservice封装起来

private CategoryService categoryService=new CategoryService();

pm

分页案例

packagecom.oracle.domain;importjava.util.ArrayList;importjava.util.List;//创建实体类 pagebean :分页5个实体类

public class PageBean{//封装当前页

privateInteger currentPage;//封装总页数

privateInteger totalPage;//封装每页显示条数

privateInteger cuerrentCount;//封装总条数

privateInteger totalCount;//封装每页显示的数据

private List list=new ArrayList();publicInteger getCurrentPage() {returncurrentPage;

}public voidsetCurrentPage(Integer currentPage) {this.currentPage =currentPage;

}publicInteger getTotalPage() {returntotalPage;

}public voidsetTotalPage(Integer totalPage) {this.totalPage =totalPage;

}publicInteger getCuerrentCount() {returncuerrentCount;

}public voidsetCuerrentCount(Integer cuerrentCount) {this.cuerrentCount =cuerrentCount;

}publicInteger getTotalCount() {returntotalCount;

}public voidsetTotalCount(Integer totalCount) {this.totalCount =totalCount;

}public ListgetList() {returnlist;

}public void setList(Listlist) {this.list =list;

}

@OverridepublicString toString() {return "PageBean [currentPage=" + currentPage + ", totalPage=" + totalPage + ", cuerrentCount=" +cuerrentCount+ ", totalCount=" + totalCount + ", list=" + list + "]";

}

}

创建 ProductPageServlet

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值