1. Hibernate多条件查询通用方法
//value[i]为第i个查询条件propertyName[i]的值 (本方法已通过测试) /*多条件查询,查询条件的值为空时自动除去该条件 * rigor为true时采用精确查询 */ public List searchByPropertys(String model,String[]propertyName,Object[] value,int page,boolean rigor){ StringBuffer sqlBuffer = new StringBuffer(); String ralation=" like "; if(rigor){ ralation=" = "; } sqlBuffer.append("from "+model+" as model\n"); int len=propertyName.length; List list=new ArrayList(); boolean first=true; for(int i=0;i< len;i++){="" if(value[i]!="null){" if(first){="" sqlbuffer.append("="" where="" "+="" "model."+="" propertyname[i]="" +="" ralation+"="" n");="" list.add(value[i]);="" first="false;" }else{="" sqlbuffer.append("="" and="" "+="" "model."+="" propertyname[i]="" +ralation+="" "="" n");="" list.add(value[i]);="" }="" }="" }="" try="" {="" session="" session="getSession();" query="" queryobject="session.createQuery(sqlBuffer.toString());" for(int="" i="">< list.size();i++){="" if(rigor){="" queryobject.setparameter(i,="" list.get(i));="" }else{="" queryobject.setparameter(i,="" "%"+list.get(i)+"%");="" }="" }="" list="queryObject.list();" closesession(session);="" return="" list;="" }="" catch="" (runtimeexception="" re)="" {="" log.error("find="" by="" property="" name="" failed",="" re);="" throw="" re;="" }="" }="" 2:hibernate多条件组合查询="" 之="" sql="" 拼接="" 这个方法与上面第一节中的相同,只不过上面的方法是将搜索的多个条件在外部(即调用方)封装在了数组中。="" public="" static="" void="" main(string[]="" args)="" {="" session="" session="null;" transaction="" tx="null;" list="" list="null;" criteria="" criteria="null;" try="" {="" session="HibernateSessionFactory.getSession();" tx="session.beginTransaction();" detachedcriteria="" detachedcriteria="DetachedCriteria" .forclass(infotab.class);="" string="" sql=" 1=1 " ;="" integer="" pareaid="0;" 父地区;="" integer="" careaid="0;" 子地区;="" integer="" categoryid="0;" 类别;="" string="" infoprivider="中介" ;="" 来源;="" string="" housetype="地下室" ;="" 房屋类型;="" integer="" hxbedroom="0;" 室;="" integer="" hxlivingroom="0;" 厅;="" string="" hzhousestatus="有房出租" ;="" 合租类型;="" string="" hzrequestsex="男" ;="" 性别要求;="" string="" fixup="尚未" ;="" 装修程度;="" integer="" lcheightmolecuse="0;" 楼层;="" string="" orientation="东南" ;="" 朝向要求;="" integer="" buildarea="2000;" 建筑面积;="" integer="" usearea="80;" 使用面积;="" integer="" rentaldigit="2000;" 租金/价格;="" string="" title="出租" ;="" 标题;="" if(pareaid!="0)" {="" sql+="pareaId=" +="" pareaid;="" }="" if(careaid!="0)" {="" sql+=" and careaId=" +="" careaid;="" }="" if(categoryid!="0)" {="" sql+=" and categoryId=" +="" categoryid;="" }="" if(!infoprivider.equals(""))="" {="" sql+=" and infoPrivider='" +="" infoprivider="" +="" "'";="" }="" if(!housetype.equals(""))="" {="" sql+=" and houseType='" +="" housetype="" +"'";="" }="" if(hxbedroom!="0)" {="" sql+=" and hxBedRoom=" +="" hxbedroom;="" }="" if(hxlivingroom!="0)" {="" sql+=" and hxLivingRoom=" +="" hxlivingroom;="" }="" if(!hzhousestatus.equals(""))="" {="" sql+=" and hzHouseStatus='" +="" hzhousestatus="" +="" "'";="" }="" if(!hzrequestsex.equals(""))="" {="" sql+=" and hzRequestSex='" +="" hzrequestsex="" +"'";="" }="" if(!fixup.equals(""))="" {="" sql+=" and fixUp='" +="" fixup="" +="" "'";="" }="" if(lcheightmolecuse!="0)" {="" sql+=" and lcHeightMolecuse=" +="" lcheightmolecuse;="" }="" if(!orientation.equals(""))="" {="" sql+=" and orientation='" +="" orientation="" +="" "'";="" }="" if(buildarea!="0)" {="" sql+=" and buildArea=" +="" buildarea;="" }="" if(usearea!="0)" {="" sql+=" and useArea=" +="" usearea;="" }="" if(rentaldigit!="0)" {="" sql+=" and rentalDigit=" +="" rentaldigit;="" }="" if(!title.equals(""))="" {="" sql+=" and title like '%" +="" title="" +="" "%'";="" }="" sql+=" order by id desc" ;="" system.out.println(sql);="" detachedcriteria.add(restrictions.sqlrestriction(sql));="" criteria="detachedCriteria.getExecutableCriteria(session);" list="criteria.list();" for(int="" i="">< list.size();i++)="" {="" infotab="" infotab="(InfoTab)list.get(i);" system.out.println(infotab.gettitle()="" +"="" "+="" infotab.getcategoryid()="" +"="" "+="" infotab.getpareaname()="" +"="" "+="" infotab.getcareaname()="" +"="" "="" +="" infotab.gethousetype()="" +"="" "="" +="" infotab.getinfoprivider());="" }="" tx.commit();="" }="" catch="" (hibernateexception="" he)="" {="" he.printstacktrace();="" }="">
用不用框架思路都差不多 遍历所有需要判断的属性 如果有值就添加条件 ?无值就下一个
for(属性?xxx:全部属性)
{
????if(xxx!=null)
????????criteria.add(restrictions.like(xxx,yyyy));
}
criteria.list();
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....