public int count(HdmainForm hf) {
// TODO 模糊查询count方法
int allNum = 0;
session = this.getSession();
Hdmain hd = new Hdmain();
Hddate hdd = new Hddate();
Corporation cor = new Corporation();
ArticleId art = new ArticleId();
System.out.println("DAO count:"+hf.getHdCorporation());
cor.setCorporationName(hf.getHdCorporation());
hd.setCorporation(cor);
art.setArticleIdAta(hf.getHdATA());
art.setArticleIdName(hf.getHdarticleId());
hd.setArticleId(art);
System.out.println("count for form:"+hd.getCorporation());
System.out.println("count for form: "+hf.getHdCorporation());

Example exCustomer = Example.create(hd);
exCustomer.ignoreCase().enableLike(MatchMode.ANYWHERE);
//模版忽略大小写,enableLike模糊
exCustomer.excludeZeroes();
try {
allNum = ((Integer)session.createCriteria(Hdmain.class).add(exCustomer)
.setProjection(Projections.rowCount())
.uniqueResult()).intValue();
System.out.println("allNum:"+allNum);
return allNum;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
this.session.close();
}
System.out.println("allNum:"+allNum);
return allNum;
}
public List query(int first, int size, HdmainForm hf) {
// TODO 分页的query方法
session = this.getSession();
List list = null;
Hdmain hd = new Hdmain();
Hddate hdd = new Hddate();
Corporation cor = new Corporation();
ArticleId art = new ArticleId();
hd.setMainCycle(hf.getHdCycle());
hd.setMainWarrantyTime(hf.getHdWarrantyTime());
hd.setMainManhourTest(hf.getHdmanhourTest());
hd.setMainManhourRepair(hf.getHdmanhourRepair());
hd.setMainManhourOverhaul(hf.getHdmanhourOverhaul());
hd.setMainImmobilityRepair(hf.getHdimmobilityRepair());
hd.setMainImmobilityOverhaul(hf.getHdimmobilityOverhaul());
hdd.setDateData(hf.getHdDate());
hd.setHddate(hdd);
System.out.println("在query里看看公司名称:"+hf.getHdCorporation());
cor.setCorporationName(hf.getHdCorporation());
hd.setCorporation(cor);
art.setArticleIdAta(hf.getHdATA());
art.setArticleIdName(hf.getHdarticleId());
hd.setArticleId(art);
Example exCustomer = Example.create(hd);
exCustomer.ignoreCase().enableLike(MatchMode.ANYWHERE);
exCustomer.excludeZeroes();
try {
list = session.createCriteria(Hdmain.class).add(exCustomer)
.setFirstResult(first).setMaxResults(size).list();
} catch (Exception e) {
// TODO: handle exception
}finally{
this.session.close();
}
return list;
}public ActionForward dodolist(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) { HdmainForm hf = (HdmainForm)form;
System.out.println("进入到dodolist");
String scp = request.getParameter("page");
PageBean pb = new PageBean();
try {
System.out.println("form值 hf:"+hf.getHdCorporation()
+" form id:"+hf.getHdarticleId()
+" form ata:"+hf.getHdATA());

pb.setAllRecord(hdmainBiz.count(hf));
pb.count(scp);
pb.setCurrentList(hdmainBiz.query(pb.getFirst(), pb.getSize(),hf));
request.setAttribute("pb",pb);
return mapping.findForward("showlist");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return mapping.findForward("showlist");
}<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %><%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'showlist.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="CSS/table.css" type="text/css">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/service.js"></script> <script type="text/javascript" src="js/comm.js"></script>
<script type="text/javascript">
function toAdd(){
document.forms[0].action+="?method=toAdd";
document.forms[0].submit();
}
function dolist(){
document.forms[0].action+="?method=dodolist";
document.forms[0].submit();
}
function toDel(id)
{
document.forms[0].action+="?method=toDelete&id="+id;
document.forms[0].submit();
}
function toUpdate(id)
{
document.forms[0].action+="?method=toUpdate&id="+id;
document.forms[0].submit();
}
</script>
</head> <body>
<html:form method="post" action="main.do">
<!--  -->

<table width="100%">
<tr>
<td>
<font color="#0000CC">您正在做的业务是:数 据</font>
</td>
</tr>
</table>
<table >
<tr>
<td>公司名称:<input type="text" name="hdCorporation"></td>
<td>件  号:<input type="text" name="hdarticleId"></td>
<td>章节号:<input type="text" name="hdATA"></td>
<td><input type="button" value="添加" onclick="javascript:toAdd();"></input></td>
<td><input type="button" value="查询" onclick="javascript:dolist();"/></td>

</tr>
</table>
<table width="100%" border="1" cellpadding=0 cellspacing=1
bordercolorlight=#848284 bordercolordark=#eeeee class="TABLE_STYLE1">

<tr align="center">
<td width="8%" class="TD_STYLE1">公司名称</td>
<td width="8%" class="TD_STYLE1">ATA章节号</td>
<td width="12%" class="TD_STYLE1">件号</td>
<td width="8%" class="TD_STYLE1">测试(工时)</td>
<td width="8%" class="TD_STYLE1">修理(工时)</td>
<td width="8%" class="TD_STYLE1">翻修(工时)</td>
<td width="8%" class="TD_STYLE1">修理(固定)</td>
<td width="8%" class="TD_STYLE1">翻修(固定)</td>
<td width="8%" class="TD_STYLE1">保修期</td>
<td width="8%" class="TD_STYLE1">周期</td>
<td width="8%" class="TD_STYLE1">报价日期</td>
<td width="8%" class="TD_STYLE1">操作</td>
</tr>

<logic:iterate name="pb" id="list" property="currentList">
<tr align="center" class="TD_STYLE2">
<td>${list.corporation.corporationName}</td>
<td>${list.articleId.articleIdAta}</td>
<td>${list.articleId.articleIdName}</td>
<td>${list.mainManhourTest}</td>
<td>${list.mainManhourRepair}</td>
<td>${list.mainManhourOverhaul}</td>
<td>${list.mainImmobilityRepair}</td>
<td>${list.mainImmobilityOverhaul}</td>
<td>${list.mainWarrantyTime}</td>
<td>${list.mainCycle}</td>
<td>${list.hddate.dateData}</td>
<td>
<a href="javascript:toUpdate('${list.mainId}');">更新</a>
<a href="javascript:toDel('${list.mainId}');">删除</a>
</td>
</tr>
</logic:iterate>
</table>

<table align="center" class="TD_STYLE9">
<tr>
<td>
总记录数${pb.allRecord}条记录,总页数有${pb.allPage}页,当前是${pb.currentPage}页
从第${pb.first+1}条记录到第${pb.last+1}条记录
</td>
</tr>
<tr>
<td>
<logic:equal value="1" name="pb" property="currentPage">
     首页 上一页
    </logic:equal>
<logic:greaterThan value="1" name="pb" property="currentPage">
<a href="main.do?method=list&page=1">首页</a>
<a href="main.do?method=list&page=${pb.currentPage-1}">上一页</a>
</logic:greaterThan>
<logic:equal value="${pb.allPage}" name="pb" property="currentPage">
    下一页 尾页
    </logic:equal>
<logic:lessThan value="${pb.allPage}" name="pb" property="currentPage">
<a href="main.do?method=list&page=${pb.currentPage+1}">下一页</a>
<a href="main.do?method=list&page=${pb.allPage}">尾页</a>
</logic:lessThan>

<input type="text" name="page" size="2">
<input type="button" value="转到"> <a href="index.jsp">返回</a>
</td>
</tr>
</table>
</html:form>
</body>

</html>

解决方案 »

  1.   

    你把sql语句打印下
     在查询分析器里执行下  看是不是?
      

  2.   

    Hibernate: select top 5 this_.main_ID as main1_3_0_, this_.main_ArticleId as main2_3_0_, this_.main_Date as main3_3_0_, this_.main_Corporation as main4_3_0_, this_.main_manhour_Test as main5_3_0_, this_.main_manhour_Repair as main6_3_0_, this_.main_manhour_Overhaul as main7_3_0_, this_.main_immobility_Repair as main8_3_0_, this_.main_immobility_Overhaul as main9_3_0_, this_.main_WarrantyTime as main10_3_0_, this_.main_Cycle as main11_3_0_ from dbo.hdmain this_ where (1=1)
    Hibernate: select articleid0_.articleId_Id as articleId1_0_0_, articleid0_.articleId_Name as articleId2_0_0_, articleid0_.articleId_ATA as articleId3_0_0_ from dbo.articleId articleid0_ where articleid0_.articleId_Id=?
    Hibernate: select hddate0_.date_Id as date1_2_0_, hddate0_.date_Data as date2_2_0_ from dbo.hddate hddate0_ where hddate0_.date_Id=?
    Hibernate: select corporatio0_.corporation_Id as corporat1_1_0_, corporatio0_.corporation_Name as corporat2_1_0_ from dbo.corporation corporatio0_ where corporatio0_.corporation_Id=?
    查询出全部的语句了 还是这样...
    主要是把所有的数据全部查出来了
      

  3.   

     没看到模糊查询语句~~~
    再就是看你的id理传了什么值
    where (1=1)
      

  4.   

    这个是不错,不过 我的entity里有11个属性,我模糊查询只吧其中3个属性 当作条件...
     这3个条件应该只能查出数据库里相应的数据,而不是所有的数据,...问题就是现在老是所有的数据被查询出来了
      

  5.   


    我用的方法是 hibernate 里的 QBE查询的
    Example exCustomer = Example.create(hd);//比如这个 是一个条件模版 把 entity类型给他 当成条件
            exCustomer.ignoreCase().enableLike(MatchMode.ANYWHERE);//这个就是条件语句了
            exCustomer.excludeZeroes();