分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

jsp 多条件组合查询

发布时间:2023-09-06 02:28责任编辑:赖小花关键词:jsjsp

web层:

 ???public String query(HttpServletRequest request, HttpServletResponse response) ???????????throws ServletException, IOException { ???????/* ????????* 1. 封装表单数据到Customer对象中,它只有四个属性(cname、gender、cellphone、email) ????????* ??它就是一个条件 ????????* 2. 使用Customer调用service方法,得到List<Customer> ????????* 3. 保存到request域中 ????????* 4. 转发到list.jsp ????????*/ ???????Customer criteria = CommonUtils.toBean(request.getParameterMap(), Customer.class); ???????List<Customer> cstmList = customerService.query(criteria); ???????request.setAttribute("cstmList", cstmList); ???????return "/list.jsp"; ???}

service层:

 ???/** ????* 多条件组合查询 ????* @param criteria ????* @return ????*/ ???public List<Customer> query(Customer criteria) { ???????return customerDao.query(criteria); ???}

domain层:

/** * 领域对象 与表单和数据库表对应 * ?* @author cxf * ?*/public class Customer { ???/* ????* 对应数据库表 ????*/ ???private String cid;// 主键 ???private String cname;// 客户名称 ???private String gender;// 客户性别 ???private String birthday;// 客户生日 ???private String cellphone;// 客户手机 ???private String email;// 客户邮箱 ???private String description;// 客户的描述 ???public String getCid() { ???????return cid; ???} ???public void setCid(String cid) { ???????this.cid = cid; ???} ???public String getCname() { ???????return cname; ???} ???public void setCname(String cname) { ???????this.cname = cname; ???} ???public String getGender() { ???????return gender; ???} ???public void setGender(String gender) { ???????this.gender = gender; ???} ???public String getBirthday() { ???????return birthday; ???} ???public void setBirthday(String birthday) { ???????this.birthday = birthday; ???} ???public String getCellphone() { ???????return cellphone; ???} ???public void setCellphone(String cellphone) { ???????this.cellphone = cellphone; ???} ???public String getEmail() { ???????return email; ???} ???public void setEmail(String email) { ???????this.email = email; ???} ???public String getDescription() { ???????return description; ???} ???public void setDescription(String description) { ???????this.description = description; ???} ???@Override ???public String toString() { ???????return "Customer [cid=" + cid + ", cname=" + cname + ", gender=" ???????????????+ gender + ", birthday=" + birthday + ", cellphone=" ???????????????+ cellphone + ", email=" + email + ", description=" ???????????????+ description + "]"; ???}}

dao层:

 /** ????* 多条件组合查询 ????* @param criteria ????* @return ????*/ ???public List<Customer> query(Customer criteria) { ???????try { ???????????/* ????????????* 1. 给出sql模板 ????????????* 2. 给出参数 ????????????* 3. 调用query方法,使用结果集处理器:BeanListHandler ????????????*/ ???????????/* ????????????* 一、 给出sql模板 ????????????* 二、 给出参数! ????????????*/ ???????????/* ????????????* 1. 给出一个sql语句前半部 ????????????*/ ???????????StringBuilder sql = new StringBuilder("select * from t_customer where 1=1"); ???????????/* ????????????* 2. 判断条件,完成向sql中追加where子句 ????????????*/ ???????????/* ????????????* 3. 创建一个ArrayList,用来装载参数值 ????????????*/ ???????????List<Object> params = new ArrayList<Object>(); ???????????String cname = criteria.getCname(); ???????????if(cname != null && !cname.trim().isEmpty()) { ???????????????sql.append(" and cname like ?"); ???????????????params.add("%" + cname + "%"); ???????????} ???????????????????????String gender = criteria.getGender(); ???????????if(gender != null && !gender.trim().isEmpty()) { ???????????????sql.append(" and gender=?"); ???????????????params.add(gender); ???????????} ???????????????????????String cellphone = criteria.getCellphone(); ???????????if(cellphone != null && !cellphone.trim().isEmpty()) { ???????????????sql.append(" and cellphone like ?"); ???????????????params.add("%" + cellphone + "%"); ???????????} ???????????????????????String email = criteria.getEmail(); ???????????if(email != null && !email.trim().isEmpty()) { ???????????????sql.append(" and email like ?"); ???????????????params.add("%" + email + "%"); ???????????} ???????????????????????/* ????????????* 三、执行query ????????????*/ ???????????return qr.query(sql.toString(), ????????????????????new BeanListHandler<Customer>(Customer.class), ????????????????????params.toArray()); ???????} catch(SQLException e) { ???????????throw new RuntimeException(e); ???????} ???}

jsp 多条件组合查询

原文地址:https://www.cnblogs.com/appium/p/10218288.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved