SSH綜合查詢的兩種方式

ludaojuan21發表於2008-11-12
[color=red]方法1:直接用ORACLE方言函式進行比較[/color]

//綜合查詢
public List getOpeRecsByCon1(HttpServletRequest request){
log.debug("finding OpeRecords instances by Condition");
try {

String factoryId = "";
String spciltyId = "";
String dutyId = "";
String classId = "";
factoryId = request.getParameter("factoryId");
spciltyId = request.getParameter("spciltyId");
dutyId = request.getParameter("dutyId");
classId = request.getParameter("classId");

StringBuffer queryString = new StringBuffer();
queryString.append("from OpeRecords o where 1=1");

//如果選擇了執行單位和專業
if((factoryId.length()!=0)&&(spciltyId.length()!=0)){
queryString.append(" and o.modelid in (select a.modelid from Opemodel a where a.runfactory='"+factoryId+"' and a.specialtyid='"+spciltyId+"')");
}

//如果僅僅選擇了執行單位
if((factoryId.length()!=0)&&(spciltyId.length()==0)){
queryString.append(" and o.modelid in (select a.modelid from Opemodel a where a.runfactory='"+factoryId+"')");
}

//如果僅僅選擇了專業
if((factoryId.length()==0)&&(spciltyId.length()!=0)){
queryString.append(" and o.modelid in (select a.modelid from Opemodel a where a.specialtyid='"+spciltyId+"')");
}

//如果選擇了班次
if(dutyId.length()!=0){
queryString.append(" and o.dutyid='"+dutyId+"'");
}

//如果選擇了值別
if(classId.length()!=0){
queryString.append(" and o.classid='"+classId+"'");
}

String startTimeStr = request.getParameter("startTime");
String endTimeStr = request.getParameter("endTime");

if(startTimeStr!=null&&!startTimeStr.equals("")){
queryString.append(" and o.gettime >to_date('"+startTimeStr+"','yyyy-MM-dd HH24:mi:ss')");
}else if(endTimeStr!=null&&!endTimeStr.equals("")){
queryString.append(" and o.gettime <to_date('"+endTimeStr+"','yyyy-MM-dd HH24:mi:ss')");
}else if(startTimeStr!=null&&!startTimeStr.equals("")&&endTimeStr!=null&&!endTimeStr.equals("")){
queryString.append(" and o.gettime >to_date('"+startTimeStr+"','yyyy-MM-dd HH24:mi:ss')");
queryString.append(" and o.gettime <to_date('"+endTimeStr+"','yyyy-MM-dd HH24:mi:ss')");
}
return getHibernateTemplate().find(queryString.toString());
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}



[color=red]方法2:使用佔位符對物件進行比較[/color]
	
//綜合查詢
public List getOpeRecsByCon2(HttpServletRequest request){
log.debug("finding OpeRecords instances by Condition");
try {

String factoryId = "";
String spciltyId = "";
String dutyId = "";
String classId = "";
factoryId = request.getParameter("factoryId");
spciltyId = request.getParameter("spciltyId");
dutyId = request.getParameter("dutyId");
classId = request.getParameter("classId");

StringBuffer queryString = new StringBuffer();
queryString.append("from OpeRecords o where 1=1");

//如果選擇了執行單位和專業
if((factoryId.length()!=0)&&(spciltyId.length()!=0)){
queryString.append(" and o.modelid in (select a.modelid from Opemodel a where a.runfactory='"+factoryId+"' and a.specialtyid='"+spciltyId+"')");
}

//如果僅僅選擇了執行單位
if((factoryId.length()!=0)&&(spciltyId.length()==0)){
queryString.append(" and o.modelid in (select a.modelid from Opemodel a where a.runfactory='"+factoryId+"')");
}

//如果僅僅選擇了專業
if((factoryId.length()==0)&&(spciltyId.length()!=0)){
queryString.append(" and o.modelid in (select a.modelid from Opemodel a where a.specialtyid='"+spciltyId+"')");
}

//如果選擇了班次
if(dutyId.length()!=0){
queryString.append(" and o.dutyid='"+dutyId+"'");
}

//如果選擇了值別
if(classId.length()!=0){
queryString.append(" and o.classid='"+classId+"'");
}

String startTimeStr = request.getParameter("startTime");
String endTimeStr = request.getParameter("endTime");

if((startTimeStr!=null&&!startTimeStr.equals(""))&&(endTimeStr!=null&&endTimeStr.equals(""))){
Date startTime = StringToDate.StrToDate4yyyyMMddHHmmss(startTimeStr);
queryString.append(" and o.gettime >?)");
return getHibernateTemplate().find(queryString.toString(),new Object[]{startTime});
}else if(endTimeStr!=null&&!endTimeStr.equals("")&&(startTimeStr!=null&&startTimeStr.equals(""))){
Date endTime = StringToDate.StrToDate4yyyyMMddHHmmss(endTimeStr);
queryString.append(" and o.gettime <?");
return getHibernateTemplate().find(queryString.toString(),new Object[]{endTime});
}else{
Date startTime = StringToDate.StrToDate4yyyyMMddHHmmss(startTimeStr);
Date endTime = StringToDate.StrToDate4yyyyMMddHHmmss(endTimeStr);
queryString.append(" and o.gettime between ? and ?");
return getHibernateTemplate().find(queryString.toString(),new Object[]{startTime,endTime});
}
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}


[color=red]注:帶註釋符的是一種方法(使用佔位符對物件進行比較),不帶註釋符是直接用ORACLE方言函式進行比較[/color]

相關文章