一个动态查询语句的具体实例

<%
String entityName="study";
List andExprs = new ArrayList();
String number="";
String name="";
String sex="";
String tel="";
//GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");
EntityConditionList mainCond=null;
if (request.getParameter("number")!=null) 
number=request.getParameter("number").toString();
if (request.getParameter("name")!=null) 
name=request.getParameter("name").toString();
if (request.getParameter("sex")!=null) 
sex=request.getParameter("sex").toString();
if (request.getParameter("tel")!=null) 
tel=request.getParameter("tel").toString();
//构造查询条件
if (number.compareTo("")!=0)
andExprs.add(new EntityExpr("number",EntityOperator.EQUALS,number));
if (name.compareTo("")!=0)
andExprs.add(new EntityExpr("name1",true,EntityOperator.LIKE,"%"+name+"%",true));
if (sex.compareTo("")!=0)
andExprs.add(new EntityExpr("sex1",true,EntityOperator.LIKE,"%"+sex+"%",true));
if (tel.compareTo("")!=0)
andExprs.add(new EntityExpr("tel",true,EntityOperator.LIKE,"%"+tel+"%",true));
//每个条件间的逻辑关系
if (andExprs.size() > 0) 
mainCond = new EntityConditionList(andExprs, EntityOperator.AND);
//要显示的字段列表
List fieldsToSelect = new ArrayList();
fieldsToSelect.add("number");
fieldsToSelect.add("name1");
fieldsToSelect.add("sex1");
fieldsToSelect.add("tel");
//排序字段列表
List orderBy = UtilMisc.toList("number", "name1");
//Distinct列表
EntityFindOptionsfindOpts = new EntityFindOptions(true,EntityFindOptions.TYPE_SCROLL_INSENSITIVE,EntityFindOptions.CONCUR_READ_ONLY, true);
//获取实体列表
EntityListIterator pli = delegator.findListIteratorByCondition(entityName, mainCond, null, fieldsToSelect, orderBy, findOpts);
while(pli.hasNext())
{
GenericValue cust = (GenericValue)pli.next();%>
<tr>
<td><%=cust.getString("number")%></td>
<td><%=cust.getString("name1")%></td>
<td><%=cust.getString("sex1")%></td>
<td><%=cust.getString("tel")%></td>
</tr>
<%}%>

</table>

你可能感兴趣的:(查询)