用Nutz的自定义Sql进行分页查询,一种方法是可以建立返回数据(即jsp页面要查询的相对应字段)的实体类,还有一种不需要映射实体类,今天就来介绍一下不需要映射实体类的方法:
示例是一个有查询条件以及分页功能的列表查询功能。
示例:
1.Jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="wd" uri="http://www.wondersgroup.com/taglib" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<c:set scope="request" var="pageForm" value="queryForm" />
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>流转问题列表</title>
<link href="${ctx}/skins/blue/css/sjdcbs.css" rel="stylesheet" type="text/css" />
<link href="${ctx}/tiles/Validform/css/style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="${ctx }/tiles/scripts/jquery-1.8.0.min.js"></script>
<script src="${ctx}/tiles/My97DatePicker/WdatePicker.js"></script>
</head>
<script type="text/javascript">
function query(){
$('#queryForm').attr("action","${ctx}/surveyRecord/toFlowProblemList");
$('#queryForm').submit();
}
//结束时间必须大于开始时间的控制
function checkTime(){
var beginDate=$("#startDate").val();
var endDate=$("#endDate").val();
var d1 = new Date(beginDate.replace(/\-/g, "\/"));
var d2 = new Date(endDate.replace(/\-/g, "\/"));
if(beginDate!=""&&endDate!=""&&d1 >d2){
alert("结束时间必须大于开始时间!");
$("#endDate").val("");
return false;
}
}
function daochu(){
var unit = '${sessionScope.sessionUser.unit}';
var startDate = $('#startDate').val();
var endDate = $('#endDate').val();
var planassignUnit = $('#planassignUnit').val();
if(confirm("确定导出?")){
$('#queryForm').attr('action','${ctx}/surveyRecord/toExportFlowProblem?startDate='+startDate+'&endDate='+endDate+'&planassignUnit='+planassignUnit);
$('#queryForm').submit();
}
}
}
</script>
<body>
<form action="${ctx }/surveyRecord/toFlowProblemList" method="post" name="queryForm" id="queryForm">
<table width="96%" border="0" height="100px" align="center" cellpadding="0" cellspacing="0" class="query_search">
<tr style="height:50px;">
<td align="right">调研日期:</td>
<td align="left" >
<input type="text" size="18" class="dfinput" id="startDate" name="filter_dt_surveyDate_start" value="${param.filter_dt_surveyDate_start }" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"> 到
<input type="text" size="18" class="dfinput" id="endDate" name="filter_dt_surveyDate_end" onchange="checkTime()" value="${param.filter_dt_surveyDate_end }" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})">
</td>
<td width="15%" align="right" >拟分派单位:</td>
<td align="left">
<wd:select className="selectInput" style="width:300px;" id="planassignUnit" name="planassignUnit" dicCode="1236" initValue="-请选择-" defaultValue="${param.planassignUnit}"/></td>
</tr>
<tr>
<td align="right" colspan="4">
<input type="button" class="minButton" value="查 询" onclick="query()" />
<input type="button" class="minButton" value="导 出" onclick="daochu()" />
</td>
</tr>
</table>
<table width="96%" class="table_list">
<tr>
<th align="center">序号</th>
<th align="center">编号</th>
<th align="center">问题类别</th>
<th align="center">问题来源</th>
<th align="center">问题提出调研对象</th>
<th align="center">联系方式</th>
<th align="center">问题概述</th>
<th align="center">初步意见</th>
<th align="center">初步意见说明</th>
<th align="center">拟分派单位</th>
<th align="center">分派说明</th>
<th align="center">是否签收</th>
</tr>
<c:forEach items="${obj.map}" var="list" varStatus="row">
<tr>
<td>${(obj.pager.pageNumber - 1)* obj.pager.pageSize + row.index + 1 }</td>
<td>${list.problemNo }</td>
<td>${list.problemType }</td>
<td>${list.surveyType }</td>
<td>${list.problemCharge }</td>
<td>${list.problemPhone }</td>
<td>${list.problemBrief }</td>
<td>${list.preIdea }</td>
<td>${list.preIdeaMemo }</td>
<td>${list.planassignUnit }</td>
<td>${list.receiveMemo }</td>
<td>
<c:if test="${list.receiveResult == 1 }">已签收</c:if>
<c:if test="${list.receiveResult != 1 }">未签收</c:if>
</td>
</tr>
</c:forEach>
</table>
<table width="96%" class="tables">
<tr>
<td>
<!-- (3)分页 -->
<jsp:include page="/common/pager-iframe.jsp"></jsp:include>
</td>
</tr>
</table>
</form>
</body>
</html>
2.pager-iframe.jsp(分页公共页面,我放在了common下)
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ include file="/common/taglibs.jsp" %>
<input type="hidden" name="page" id="pageNo" value="${obj.pager.pageNumber}"/>
<input type="hidden" name="rows" id="pageSize" value="${obj.pager.pageSize}"/>
<table class="tablespage" width="100%" border="0" align="center" cellspacing="0" >
<tr>
<td align="right">
共 <strong>${obj.pager.recordCount}</strong> 条记录 |
<a href="javascript:jumpPage(1)">首页</a>
<c:if test="${obj.pager.pageNumber != 1}">
<a href="javascript:jumpPage(${obj.pager.pageNumber-1})">上一页</a>
</c:if>
<c:if test="${obj.pager.pageNumber == 1}">
<a href="#">上一页</a>
</c:if>
<c:if test="${obj.pager.pageNumber < obj.pager.pageCount}">
<a href="javascript:jumpPage(${obj.pager.pageNumber+1})">下一页</a>
</c:if>
<c:if test="${obj.pager.pageNumber == obj.pager.pageCount}">
<a href="#">下一页</a>
</c:if>
<a href="javascript:jumpPage(${obj.pager.pageCount})">尾页</a> |
共 <strong>${obj.pager.pageCount}</strong> 页 当前为 第 <strong>${obj.pager.pageNumber}/${obj.pager.pageCount}</strong> 页 |
转到第
<input type="text" class="dfinput" style="width:30px;" id="jumpNo" name="jumpNo" />
页 <a href="javascript:jumpNo()">go</a>
</td>
</tr>
</table>
<script language="JavaScript">
function jumpNo() {
if($("#jumpNo").val()<=parseInt('${obj.pager.pageCount}') && $("#jumpNo").val()>0){
jumpPage($("#jumpNo").val());
}else{
$("#jumpNo").val("");
}
}
function jumpPage(pageNo) {
$("#pageNo").val(pageNo);
//alert($("#pageNo").val());
//alert($("#${pageForm}").attr('action'));
$("#${pageForm}").submit();
}
function changeSize() {
$("#${pageForm}").submit();
}
</script>
3. 后台方法
package com.wonders.jmfw.at;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Entity;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.dao.util.Daos;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import com.alibaba.fastjson.JSONObject;
import com.wonders.tiles.authority.entity.SessionUser;
import com.wonders.tiles.dic.DicDataUtils;
@IocBean
@At("/surveyRecord")
public class SurveyRecordAt {
@Inject
private Dao dao;
/**
* 流转问题查询
*/
@At
@Ok("jsp:jsp.survey.ddy.problem.flow.flowProblem_list")
public Map<String, Object> toFlowProblemList(HttpServletRequest request){
Map<String, Object> result = new HashMap<String, Object>();
String sqls = "select a.problemno as problemNo, a.problem_type as problemType,d.survey_type as surveyType,a.problem_charge as problemCharge,a.problem_phone as problemPhone,a.problem_brief as problemBrief,a.pre_idea as preIdea, "
+ "a.pre_idea_memo as preIdeaMemo,b.planassign_unit as planassignUnit,b.receive_memo as receiveMemo,b.receive_result as receiveResult from survey_problem_flow b "
+ "left join survey_problem a on a.problem_id = b.problem_id left join survey_record_problem c "
+ "on a.problem_id = c.problem_id left join survey_record d on c.record_id = d.record_id "
+ "where a.pre_idea in('4','5','6') ";
String startDate = request.getParameter("filter_dt_surveyDate_start");
String endDate = request.getParameter("filter_dt_surveyDate_end");
if(StringUtils.isNotEmpty(startDate)) {
sqls += " and d.survey_date >= "+"to_date('"+startDate+"','yyyy-MM-dd')";
}
if(StringUtils.isNotEmpty(endDate)) {
sqls +=" and d.survey_date <= "+"to_date('"+endDate+"','yyyy-MM-dd')";
}
String planassignUnit = request.getParameter("planassignUnit");
if(StringUtils.isNotEmpty(planassignUnit)) {
sqls += " and b.planassign_unit = '"+ planassignUnit + "' ";
}
sqls += "order by problemno desc";
Sql sql = Sqls.create(sqls);
Long count = (long) Daos.queryCount(dao, sqls);
Pager pager = ConUtils.makePaginationPager(request);//起始页,每页数目
pager.setRecordCount(count.intValue());//设置记录总数
sql.setPager(pager);
//在这里设置回调,将返回的数据进行设置,下方中一个DicDataUtils是我们开发过程中的一个公共类,用来转换key-value值的,可以忽略DicDataUtil公共类,在开发的时候根据各自的实际情况转换即可,一般情况下都是obj.put("problemNo", rs.getString("problemNo"));写法
sql.setCallback(new SqlCallback() {
@Override
public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
List<JSONObject> list = new ArrayList<JSONObject>();
while (rs.next()) {
JSONObject obj = new JSONObject();
obj.put("problemNo", rs.getString("problemNo"));
obj.put("problemType", DicDataUtils.getInstance().getDicData(1239,rs.getString("problemType")));
obj.put("surveyType", DicDataUtils.getInstance().getDicDataStr(1238, rs.getString("surveyType").toString().split(","), ","));
obj.put("problemCharge", rs.getString("problemCharge"));
obj.put("problemPhone", rs.getString("problemPhone"));
obj.put("problemBrief", rs.getString("problemBrief"));
obj.put("preIdea", DicDataUtils.getInstance().getDicData(1256,rs.getString("preIdea")));
obj.put("preIdeaMemo", rs.getString("preIdeaMemo"));
obj.put("planassignUnit", DicDataUtils.getInstance().getDicData(1236,rs.getString("planassignUnit")));
obj.put("receiveMemo", rs.getString("receiveMemo"));
obj.put("receiveResult", rs.getString("receiveResult"));
list.add(obj);
}
return list;
}
});//实体回调,不构建实体类
Entity<Record> entity = dao.getEntity(Record.class);//Record是Nutz自带的一个类,查一下就知道了
sql.setEntity(entity);
dao.execute(sql);
List<Record> map = sql.getList(Record.class);
result.put("map", map);
result.put("pager", pager);
return result;
}
}
4.运行结果截图展示:
表头: