分页已经是有的,要在原来的基础上加上模糊查询,要模糊的字段有4、5个多。有没人写过,copy下来看下。在这谢谢了。

解决方案 »

  1.   

    这个其实就用 like 各个字段之间用 and连接就可以了
    如select * from User where username like '%字段一%' and password like '%字段二%'
      

  2.   

    request.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    ActPlanDao actPlanDao = new ActPlanDao();
    List<ActPlan> actPlanList = null;

    String actname = (String)request.getParameter("actname");
    String actdate = (String)request.getParameter("actdate");
    String organizer = (String)request.getParameter("organizer");
    String actplace = (String)request.getParameter("actplace");

    String sql = "select * from actplan where 1=1";
    String sign = request.getParameter("sign");

    if (sign != null) {
    actname = URLDecoder.decode(actname, "utf-8");
    actdate = URLDecoder.decode(actdate, "utf-8");
    organizer = URLDecoder.decode(organizer, "utf-8");
    actplace = URLDecoder.decode(actplace, "utf-8");
    } if (!("".equals(actname) || actname == null)) {
    sql = sql + " and actname='" + actname + "'";
    }
    if (!("".equals(actdate) || actdate == null)) {
    sql = sql + " and actdate='" + actdate + "'";
    }
    if (!("".equals(organizer) || organizer == null)) {
    sql = sql + " and organizer='" + organizer + "'";
    }
    if (!("".equals(actplace) || actplace == null)) {
    sql = sql + " and actplace='" + actplace + "'";
    } int rowcount = actPlanDao
    .getRowcount("select count(id) as result from (" + sql + ") result");// 条件查询得到数据的行数 String currentpage = request.getParameter("currentpage");
    if ("".equals(currentpage) || currentpage == null) {
    currentpage = "1";
    }
    PageBean pageBean = new PageBean(Integer.parseInt(currentpage),
    rowcount, 10); sql = sql + " limit " + (pageBean.getCurrentpage() - 1)
    * pageBean.getPagesize() + "," + pageBean.getPagesize(); try {
    actPlanList = actPlanDao.queryActPlan(sql);//条件查询活动计划
    } catch (ErrMsgException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      

  3.   

    个人建议动态sql还是不应该硬编码在java代码中
    可以把sql维护在xml中
    里面嵌入模板语言
    然后使用dom4j解析
    用模板引擎来实现动态sql拼接
    类似于ibatis的机制
      

  4.   

    如果用like连接其实都不用判断是不是为空
    至于sql是写在xml还是利用hibernate的机制去做,这个可以看个人喜好,也可以看整体的架构