java 编写模糊查询
Posted 丿狂奔的蜗牛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 编写模糊查询相关的知识,希望对你有一定的参考价值。
当输入商品名称或所属分类及是否热门 查询其中的1个或者2个的时候都能查出;
第一步:让jsp在页面中显示出来
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <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/Style1.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}/js/jquery-1.11.3.min.js"> //这个地方JQ文件不要忘记引入 </script> <script type="text/javascript"> function addProduct() { window.location.href = "${pageContext.request.contextPath}/AddProductUIServlet"; } function del(pid) { var flag = confirm("您确定删除吗?") if (flag) { location.href = "${pageContext.request.contextPath}/DeleteServiet?pid=" + pid; } } $(function() { $("#cid option[value=\'${Condition.cid}\']").prop("selected",true); //点击属性cid的时候让它显示出来 $("#is_hot option[value=\'${Condition.is_hot}\']").prop("selected", true); }) </script> </HEAD> <body> <br> <form id="Form1" name="Form1" action="${pageContext.request.contextPath}/ConditionServlet" //创建一个conditionservlet method="post"> 商品名称:<input type="text" name="pname" value="${Condition.pname }"> 所属分类:<select name="cid" id="cid"> <option value="">请选择</option> <c:forEach items="${categoryList }" var="c"> <option value="${c.cid }">${c.cname }</option> </c:forEach> </select> 是否热门:<select name="is_hot" id="is_hot"> <option value="">请选择</option> <option value="1">是</option> <option value="0">否</option> </select> <input type="submit" value="查询"> <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 class="ta_01" align="right"> <button type="button" id="add" name="add" value="添加" class="button_add" onclick="addProduct()"> 添加</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"> <tr style="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="17%">商品名称</td> <td align="center" width="17%">商品价格</td> <td align="center" width="17%">是否热门</td> <td width="7%" align="center">编辑</td> <td width="7%" align="center">删除</td> </tr> <c:forEach items="${ProductList }" var="pro" varStatus="vs"> <tr onmouseover="this.style.backgroundColor = \'white\'" onmouseout="this.style.backgroundColor = \'#F5FAFE\';"> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="18%">${vs.count }</td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%"><img width="40" height="45" src="${pro.pimage }"></td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">${pro.pname }</td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">${pro.market_price }</td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">${pro.is_hot==1?"是":"否"}</td> <td align="center" style="HEIGHT: 22px"><a href="${ pageContext.request.contextPath }/EditProductUIServlet?pid=${pro.pid}"> <img src="${pageContext.request.contextPath}/images/i_edit.gif" border="0" style="CURSOR: hand"> </a></td> <td align="center" style="HEIGHT: 22px"><a href="javascript:void(0)" onclick="del(\'${pro.pid}\')"> <img src="${pageContext.request.contextPath}/images/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand"> </a></td> </tr> </c:forEach> </table> </td> </tr> </TBODY> </table> </form> </body> </HTML>
第二步 创建 conditionservlet
public class ConditionServlet extends HttpServlet { private Productservice productservice=new Productservice(); private CategoryService categoryService=new CategoryService(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //输入的时候转成utf-8编码 //获取所有参数map集合 Map<String, String[]> map = request.getParameterMap(); //创建Condition对象 Condition condition=new Condition(); try { BeanUtils.populate(condition, map); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } List<Product> list = productservice.getCondition(condition); request.setAttribute("ProductList", list); List<Category> list2=categoryService.getCategory(); request.setAttribute("categoryList", list2); //这个地方的key值需要和jsp中的值一样 request.setAttribute("Condition", condition); request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response); //请求转发 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
第三步:创建实体类对象:
package com.oracle.domain; public class Condition { private String pname; private String is_hot; private String cid; public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getIs_hot() { return is_hot; } public void setIs_hot(String is_hot) { this.is_hot = is_hot; } public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } @Override public String toString() { return "Condition [pname=" + pname + ", is_hot=" + is_hot + ", cid=" + cid + "]"; } }
第四步:创建dao层对象:
//模糊查询 public List<Product> getCondition(Condition condition) throws SQLException{ QueryRunner qr = new QueryRunner(MyDBUtils.getDataSource()); String sql="select * from product where 1=1 "; ArrayList<Object> arr=new ArrayList<Object>(); if(condition.getPname().trim()!=null&&!condition.getPname().trim().equals("")){ //trim 判定不能是空格 sql+=" and pname like ?"; arr.add("%"+condition.getPname()+"%"); }if(condition.getIs_hot()!=null&&!condition.getIs_hot().equals("")){ //这个地方不能用else if sql+=" and Is_hot=?"; arr.add(condition.getIs_hot()); }if(condition.getCid()!=null&&!condition.getCid().equals("")){ sql+=" and cid=?"; arr.add(condition.getCid()); } List<Product> list=qr.query(sql, new BeanListHandler<Product>(Product.class),arr.toArray()); return list; }
第五步:创建service
public List<Product> getCondition(Condition condition){ List<Product> list=null; try { list=productdao.getCondition(condition); } catch (SQLException e) { e.printStackTrace(); } return list; }
这样就能把商品筛选出来 文字并在上面显示出来;
欢迎各位大神指点和评论;
以上是关于java 编写模糊查询的主要内容,如果未能解决你的问题,请参考以下文章
SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper
11.按要求编写Java应用程序。 创建一个叫做机动车的类: 属性:车牌号(String),车速(int),载重量(double) 功能:加速(车速自增)减速(车速自减)修改车牌号,查询车的(代码片段
按要求编写Java应用程序。 创建一个叫做机动车的类: 属性:车牌号(String),车速(int),载重量(double) 功能:加速(车速自增)减速(车速自减)修改车牌号,查询车的载重量(代码片段