struts实现条件查询---利用dbutils

一.在list.jsp页面修改查询组件

是否上传简历
        <s:select list="#{'1':'有','2':'无'}" name="isUpload" id="isUpload" headerKey="0" headerValue="--请选择--"></s:select>

<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<HTML><HEAD><meta http-equiv="Content-Language" content="zh-cn"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><link href="${pageContext.request.contextPath}/css/Style.css" rel="stylesheet" type="text/css" /><script language="javascript" src="${pageContext.request.contextPath}/js/public.js"></script><script type="text/javascript" src="${pageContext.request.contextPath }/jquery/jquery-1.4.2.js"></script><script type="text/javascript">function addUser(){window.location.href = "${pageContext.request.contextPath}/user/add.jsp";}$(function(){// 为删除链接 加确认效果$(".delLink").click(function(event){var isConfirm = window.confirm("想好了吗?");if(!isConfirm){// 阻止提交event.preventDefault();}});});</script></HEAD><body><br><s:form action="user_list" namespace="/" method="post" theme="simple" id="Form1" name="Form1"><table cellSpacing="1" cellPadding="0" width="100%" align="center" bgColor="#f5fafe" border="0"><TBODY><tr><td class="ta_01" align="center" bgColor="#afd1f3"><strong>查 询 条 件</strong></td></tr><tr><td><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td height="22" align="center" bgColor="#f5fafe" class="ta_01">用户姓名</td><td class="ta_01" bgColor="#ffffff"><s:textfield name="userName" size="15" id="Form1_userName" cssClass="bg"/></td><td height="22" align="center" bgColor="#f5fafe" class="ta_01">性别:</td><td class="ta_01" bgColor="#ffffff"><s:select list="{'男','女'}" name="sex" id="sex" headerKey="" headerValue="--选择性别--"></s:select></td></tr><tr><td height="22" align="center" bgColor="#f5fafe" class="ta_01">学历:</td><td class="ta_01" bgColor="#ffffff"><s:select list="{'博士','硕士','研究生','本科','专科','高中'}" name="education" id="education" headerKey="" headerValue="--选择学历--"></s:select></td><td height="22" align="center" bgColor="#f5fafe" class="ta_01">是否上传简历</td><td class="ta_01" bgColor="#ffffff"><s:select list="#{'1':'有','2':'无' }" name="isUpload" id="isUpload" headerKey="" headerValue="--请选择--"></s:select></td></tr><tr><td width="100" height="22" align="center" bgColor="#f5fafe"class="ta_01"></td><td class="ta_01" bgColor="#ffffff"><font face="宋体" color="red">  </font></td><td align="right" bgColor="#ffffff" class="ta_01"><br><br></td><td align="right" bgColor="#ffffff" class="ta_01"><button type="submit" id="search" name="search" value="查询" class="button_view">
查询
</button><input type="reset" name="reset" value="重置" class="button_view"/></td></tr></table></td></tr><tr><td class="ta_01" align="center" bgColor="#afd1f3"><strong>用 户 列 表</strong></TD></tr><tr><td class="ta_01" align="right"><button type="button" id="add" name="add" value="添加" class="button_add" onclick="addUser()">
添加
</button></td></tr><tr><td class="ta_01" align="center" bgColor="#f5fafe"><table cellspacing="0" cellpadding="1" rules="all"bordercolor="gray" border="1" id="DataGrid1"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"><trstyle="FONT-WEIGHT: bold; FONT-SIZE: 12pt; HEIGHT: 25px; BACKGROUND-COLOR: #afd1f3"><td align="center" width="18%">登录名</td><td align="center" width="17%">用户姓名</td><td align="center" width="8%">性别</td><td align="center" width="23%">联系电话</td><td width="11%" align="center">学历</td><td width="7%" align="center">编辑</td><td width="7%" align="center">查看</td><td width="7%" align="center">删除</td></tr><s:iterator value="users" var="user"><tr onmouseover="this.style.backgroundColor = 'white'"onmouseout="this.style.backgroundColor = '#F5FAFE';"><td style="CURSOR: hand; HEIGHT: 22px" align="center"width="18%"><!-- user会push到root 同时 保存contextMap --><s:property value="logonName"/><s:property value="#user.logonName"/></td><td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%"><s:property value="#user.userName"/></td><td style="CURSOR: hand; HEIGHT: 22px" align="center"width="8%"><s:property value="#user.sex"/></td><td style="CURSOR: hand; HEIGHT: 22px" align="center"width="23%"><s:property value="#user.telephone"/></td><td style="CURSOR: hand; HEIGHT: 22px" align="center"><s:property value="#user.education"/></td><td align="center" style="HEIGHT: 22px"><s:a action="user_editview" namespace="/"><s:param name="userID" value="#user.userID"></s:param><img src="${pageContext.request.contextPath}/images/i_edit.gif" border="0" style="CURSOR: hand"></s:a></td><td align="center" style="HEIGHT: 22px"><s:a action="user_view" namespace="/"><s:param name="userID" value="#user.userID"></s:param><img src="${pageContext.request.contextPath}/images/button_view.gif" border="0" style="CURSOR: hand"></s:a></td><td align="center" style="HEIGHT: 22px"><s:a action="user_delete" namespace="/" cssClass="delLink"><s:param name="userID" value="#user.userID"></s:param><img src="${pageContext.request.contextPath}/images/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand"></s:a></td></tr></s:iterator>       </table></td></tr></TBODY></table></s:form></body>
</HTML>

二.添加校验

三.完成条件查询操作    
       1、是否上传简历,怎样在action中获取?

需要在User中添加一个属性  String isUpload

2、 在dao中怎样根据条件查询?

             在这里dao层我们使用的是dbutils,所以进行条件查询的时候需要用到QueryRunner类,而他的查询方法queryRunner.query(sql,
                    new BeanListHandler<User>(User.class), argList.toArray());需要sql语句,和所有参数的这两个参数,所以我们需要解决这两个问题。

1).sql语句生成
            
            2).参数怎样传递?

                创建一个List<Object>,在每一次判断时,直接将参数添加到集合中,
                最后将集合转换成Object[],做为参数传递到query方法中。

  String sql = "select * from s_user where 1=1 ";List<Object> params=new ArrayList<Object>();String username = user.getUserName();if (username != null && username.trim().length() > 0) {sql += " and userName like ?";params.add("%"+username+"%");}String sex = user.getSex();if (sex != null && sex.trim().length() > 0) {sql += " and sex=?";params.add(sex);}String education = user.getEducation();if (education != null && education.trim().length() > 0) {sql += " and education=?";params.add(education);}String isupload = user.getIsUpload();if ("1".equals(isupload)) {sql += " and filename is not null";} else if ("2".equals(isupload)) {sql += " and filename is null";}QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());return runner.query(sql, new BeanListHandler<User>(User.class),params.toArray());
/*** 条件查询* * @param user* @return*/public List<User> findByCondition(User user) {// 根据用户姓名、性别、学历、是否上传简历 组合查询String sql = "select * from s_user where 1=1 ";List<String> argList = new ArrayList<String>(); // 参数列表if (user.getUserName() != null&& user.getUserName().trim().length() > 0) {sql += "and userName like ? ";argList.add("%" + user.getUserName() + "%");}if (user.getSex() != null && user.getSex().trim().length() > 0) {sql += "and sex = ? ";argList.add(user.getSex());}if (user.getEducation() != null&& user.getEducation().trim().length() > 0) {sql += "and education = ? ";argList.add(user.getEducation());}if (user.getIsUpload() != null&& user.getIsUpload().trim().length() > 0) {if (user.getIsUpload().equals("1")) {// 上传简历sql += "and filename is not null";} else if (user.getIsUpload().equals("2")) {// 没有上传简历sql += "and filename is null";}}try {List<User> users = queryRunner.query(sql,new BeanListHandler<User>(User.class), argList.toArray());return users;} catch (SQLException e) {e.printStackTrace();throw new MySQLException(e);}}

struts实战--实现条件查询(利用dbutils)相关推荐

  1. ssm框架中利用pagehelper分页,完成模糊查询与select条件查询

    ssm框架中利用pagehelper分页,完成模糊查询与select条件查询 一.问题分析 1.1 往期回顾 pagehelper分页 https://blog.csdn.net/Jia_Peng_T ...

  2. Django开发实战2-5 模型- 基础条件查询

    Django开发实战2-5 模型-基础条件查询 一.基本查询 1.使用FilmInfo/PeopleInfo.objects.get() 查询fid=2的 数据 2.使用FilmInfo/People ...

  3. 实战 Java 第5天:开发商品查询(模糊查询与条件查询)接口

    实战 Java 第5天:开发商品查询接口 前言 一.在 ProductService 类中添加接口 二.在 ProductMapper 类中添加接口 三.增加 sql 语句 四.在 ProductCo ...

  4. MyBatis框架学习笔记04:利用MyBatis实现条件查询

    文章目录 一.查询需求 二.打开MyBatisDemo项目 三.对学生表实现条件查询 (一)创建学生映射器配置文件 (二)在MyBatis配置文件里注册学生映射器配置文件 (三)创建学生映射器接口 ( ...

  5. MongoDB利用$elemMatch对内嵌文档进行多条件查询

    目录 背景 内嵌文档单条件查询 多条件查询 背景 问题:遇到如下类型的数据,想要查询出所有文档中 满足members中任意一条的name以"Bule"开头且其中的gender为&q ...

  6. vue 前端项目带条件查询的分页列表开发实战

    一 添加医院设置路由 修改文件 E:\vue-sdgt\src\router\index.js {path: '/hospital',component: Layout,redirect: '/hos ...

  7. asp mysql查询_asp的 条件查询

    ASP.NET查询页面设置form的action属性只弹出一个页面,并且每次将页面设置到最前 原文:ASP.NET查询页面设置form的action属性只弹出一个页面,并且每次将页面设置到最前 背景 ...

  8. 爬虫实战(二)—利用requests、selenium爬取王者官网、王者营地APP数据及pymongo详解

    概述 可关注微信订阅号 loak 查看实际效果. 代码已托管github,地址为:https://github.com/luozhengszj/LOLGokSpider ,包括了项目的所有代码. 本文 ...

  9. HBase高性能复杂条件查询引擎

    推荐:博主历时三年倾注大量心血创作的<大数据平台架构与原型实现:数据中台建设实战>一书已由知名IT图书品牌电子工业出版社博文视点出版发行,真诚推荐给每一位读者!点击<重磅推荐:建大数 ...

最新文章

  1. idea设置文件多行显示
  2. Open3D编译安装
  3. [leetcode]203. Remove Linked List Elements链表中删除节点
  4. PLSQL_Database Link的基本概念和用法(概念)
  5. JS针对图片加载及404处理
  6. 6、Java包的命名与划分
  7. 安卓rom制作教程_刹那 刷机教程20-1三星S10 N10等系列 韩版国行安卓9升级10 详细步骤...
  8. linux_oracle视频讲解百度云,Oracle免费视频课程2012版
  9. c3p0连接池配置说明
  10. 海康/大华/宇视等网络摄像头云台控制功能探测方法解析
  11. apicloud中阿里云推送使用
  12. ASP.net如何优雅地获取屏幕高度及宽度(通过JS)
  13. 低延迟视频传输 UDP JPEG图像压缩 opencv
  14. 第015讲: 跟王家林学习从1000个代码案例中学习人工智能和大数据实战第015讲:Scala中Tuple源码剖析及代码实践
  15. WIN10如何设置默认便签应用
  16. Docker笔记 —— 简介与安装 + Centos的一些配置
  17. OpenCV————鼠标绘制和滑动条
  18. 2019,我的工作寻找之路
  19. 数据结构基础--搜索树
  20. 什么是死锁?死锁的预防?

热门文章

  1. [Oracle] 数据排重
  2. 二十、 二叉树的同构
  3. Windows下搭建PySpark环境
  4. android微信支付的实现
  5. cmake (2)指令说明
  6. chap10 构建Web内容的技术
  7. 编译Bitcoin BCH configure: error: libdb_cxx headers missing ,终于解决了
  8. 113. 路径总和 (剑指 Offer 34. 二叉树中和为某一值的路径)(回溯算法)
  9. CA/TA参数传输中tmpref,memref和Value的区别
  10. [crypto]-50-base64_encode和base64_decode的C语言实现