今天把查询功能写了写,昨天的时候提交页面报错,就没有继续做,今天试着把这个部分做出来,
花了一个来小时的时间吧,
public int findTotalCount(Map<String, String[]> condition) {
//1.定义模板初始化sql;
String sql="select count(*) from user where 1 = 1 ";
StringBuilder sb=new StringBuilder(sql);
//2.遍历map
Set<String> keySet = condition.keySet();
//定义参数集合
List<Object> params = new ArrayList<Object>();
for (String key :keySet){
//排除分页条件参数
if ("currentPage".equals(key) || "rows".equals(key)){
continue;
}
//获取value
String value = condition.get(key)[0];
//判断value是否有值
if (value!=null&&!"".equals(value)){
//有值
sb.append(" and "+key+" like ?");
params.add("%"+value+"%");//条件的值
}
}
return template.queryForObject(sql,Integer.class,params.toArray());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {标签:rows,String,每日,request,value,currentPage,小结,condition From: https://www.cnblogs.com/jingyk/p/17157393.html
request.setCharacterEncoding("utf-8");
//1.获取参数
String currentPage = request.getParameter("currentPage");//当前页码
String rows = request.getParameter("rows");//每页显示的条数
//进行一个判断
if (currentPage ==null||"".equals(currentPage)){
currentPage="1";
}
if (rows == null || "".equals(rows)){
rows="5";
}
//获取条件查询的参数
Map<String, String[]> condition = request.getParameterMap();
//2.调用service查询
UserService service= new UserServiceImpl();
PageBean<User> pb = service.findUserByPage(currentPage,rows,condition);
//System.out.println(pb);
//3.将PageBean存入request;
request.setAttribute("pb",pb);
//4.转发到list.jsp
request.getRequestDispatcher("/list.jsp").forward(request,response);
}
}