动态拼接hibernate多条件查询语句

public List getDjdReportList(Integer year,Integer quarter,String memberSecondOrgcode,short status){
		return this.getHibernateTemplate().execute(new HibernateCallback>(){
			@Override
			public List doInHibernate(Session s) throws HibernateException {
				
				StringBuffer buf = new StringBuffer();
				Map params = new HashMap();
				if(year==0&&quarter==0&&"0".equals(memberSecondOrgcode)){
					buf.append("from TDjdReportMain where status =:status");
				}else{
					buf.append("from TDjdReportMain ");
					buf.append("where 1=1 ");
					if(year!=null&&year!=0){
						buf.append(" and year=:year ");
					}
					if(quarter!=null&&quarter!=0){
						buf.append(" and quarter=:quarter ");
					}
					if(memberSecondOrgcode!=null&&!"0".equals(memberSecondOrgcode)){
						buf.append(" and memberSecondOrgcode=:memberSecondOrgcode ");
					}
					buf.append(" and status=:status ");
				}
				Query query = s.createQuery(buf.toString());
				if(year!=null&&year!=0){
					query.setParameter("year", year);
				}
				if(quarter!=null&&quarter!=0){
					query.setParameter("quarter", quarter);
				}
				if(memberSecondOrgcode!=null&&!"0".equals(memberSecondOrgcode)){
					query.setParameter("memberSecondOrgcode", memberSecondOrgcode);
				}
				query.setParameter("status", status);
				return query.list();
			}
		});
		
		
	}

带分页的

public PageResult searchXfList(int startIndex,int pageSize,String snum,String scode,String sname,String sdept,Date receiveDateStartDate,Date receiveDateEndDate){
		StringBuffer buf = new StringBuffer();
		Map params = new HashMap();
		if(snum==""&&scode==""&&sname==""&&sdept==""&&receiveDateStartDate==null&&receiveDateEndDate==null){
			buf.append("from TXf");
		}else{
			buf.append("from TXf ");
			buf.append("where 1=1 ");
			if(snum!=null&&!snum.equals("")){
				buf.append("and num=:snum ");
				params.put("snum", snum);
			}
			if(scode!=null&&!scode.equals("")){
				buf.append("and code=:scode ");
				params.put("scode", scode);
			}
			if(sname!=null&&!sname.equals("")){
				buf.append("and (userReflected=:sname or userReflect=:sname) ");
				params.put("sname", sname);
			}
			if(sdept!=null&&!sdept.equals("")){
				buf.append("and (deptReflected=:sdept or deptReflect=:sdept) ");
				params.put("sdept", sdept);
			}
			if(receiveDateStartDate!=null&&receiveDateEndDate!=null){
				buf.append("and receiveDate between :receiveDateStartDate and :receiveDateEndDate ");
				params.put("receiveDateStartDate", receiveDateStartDate);
				params.put("receiveDateEndDate", receiveDateEndDate);
			} else if(receiveDateStartDate!=null){
				buf.append("and receiveDate >=:receiveDateStartDate) ");
				params.put("receiveDateStartDate", receiveDateStartDate);
			}else if(receiveDateEndDate!=null){
				buf.append("and receiveDate <=:receiveDateEndDate) ");
				params.put("receiveDateEndDate", receiveDateEndDate);
			}
		
			
		}
		PageResult pr = (PageResult) this.query(buf.toString(),HqlUtils.generateCountHql(buf.toString(),null), params, startIndex,pageSize);
		return pr;
	}

 

你可能感兴趣的:(hibernate)