@Override public PageUtil<MessageEntity> query(PageUtil<MessageEntity> page) { String concentratorAddress = (String) page.getParameter("concentratorAddress"); String type = (String) page.getParameter("type"); String analytical = (String) page.getParameter("analytical"); String date = (String) page.getParameter("date"); String date1 = (String) page.getParameter("date1"); String sWhere = "signal = 1"; if (StringUtils.isNotEmpty(concentratorAddress)) { sWhere = "concentratorAddress = " + concentratorAddress; } if (StringUtils.isNotEmpty(type)) { sWhere += " AND type = " + type; } if (StringUtils.isNotEmpty(analytical)) { sWhere += " AND analytical like '%" + analytical + "%' "; } if(StringUtils.isNotEmpty(date)){ sWhere += " AND datetime >= " + date ; } if(StringUtils.isNotEmpty(date1)){ sWhere += " AND datetime <= " + date1 ; } System.out.println("sWhere:" + sWhere); int count = messageDao.count(sWhere); page.setTotalSize(count); if (count != 0) { List<MessageEntity> messageList = messageDao.queryByPage(page .getBegin() - 1, page.getPageSize(), sWhere, " id "); page.setResultSet(messageList); } return page; }
日期是不是少了单引号?
调试一下,把完整的SQL语句贴上来看下。
已解决,得出结论是hql不能直接用字符串拼接特殊类型的字段,比如日期类型,修改拼接代码如下:
@Override public PageUtil<MessageEntity> query(PageUtil<MessageEntity> page) { String concentratorAddress = (String) page.getParameter("concentratorAddress"); String type = (String) page.getParameter("type"); String analytical = (String) page.getParameter("analytical"); String formatbgString = (String) page.getParameter("date"); String formatendString = (String) page.getParameter("date1"); // Date formatbgString = null; // Date formatendString = null; // if(date != null || date1 != null){ // formatbgString = StrToDate(date); // formatendString = StrToDate(date1); // } // String sWhere = "signal = 1"; StringBuilder where = new StringBuilder("signal = 1"); if (concentratorAddress != null && !"".equals(concentratorAddress)) { where.append(" AND concentratorAddress=" + concentratorAddress ); } if (type != null && !"".equals(type)) { where.append(" AND type=" + type ); } if (analytical != null && !"".equals(analytical)) { where.append(" AND analytical like '%" + analytical + "%' " ); } if (formatbgString != null && !"".equals(formatbgString)) { where.append("and datetime > to_date('" + formatbgString + "', 'yyyy-MM-dd HH24:mi:ss') "); } if (formatendString != null && !"".equals(formatendString)) { where.append("and datetime < to_date('" + formatendString + "', 'yyyy-MM-dd HH24:mi:ss') "); } // if (date1 != null && !"".equals(date1)) { // where.append("and to_number(to_char(applyTime,'yyyy-mm-dd hh24:mi:ss')) <= ?" + date1); // } // if (StringUtils.isNotEmpty(concentratorAddress)) { // sWhere = "concentratorAddress = " + concentratorAddress; // } // if (StringUtils.isNotEmpty(type)) { // sWhere += " AND type = " + type; // } // if (StringUtils.isNotEmpty(analytical)) { // sWhere += " AND analytical like '%" + analytical + "%' "; // } // System.out.println("dateMin:" + dateMin); // System.out.println("dateMax:" + dateMax); // if(StringUtils.isNotEmpty(date)){ // sWhere += " AND datetime >= " + date ; // } // if(StringUtils.isNotEmpty(date1)){ // sWhere += " AND datetime <= " + date1 ; // } // System.out.println(date); // System.out.println(date1); // sWhere += " AND datetime >= Tue Nov 01 17:22:24 CST 2016"; // sWhere += " AND datetime <= Mon Jan 23 17:22:37 CST 2017"; // sWhere += " AND datetime >= 2016-11-23 " ; // sWhere += " AND datetime <= 2017-01-01 " ; // if (StringUtils.isNotEmpty(date) && StringUtils.isNotEmpty(date1) ) { // sWhere += " AND datetime <='" + date1 + "' AND '" + date + "'<= datetime"; // } // System.out.println("sWhere:" + sWhere); String HQL = where.toString(); int count = messageDao.count(HQL); page.setTotalSize(count); if (count != 0) { List<MessageEntity> messageList = messageDao.queryByPage(page .getBegin() - 1, page.getPageSize(), HQL, " id "); page.setResultSet(messageList); } return page; }