欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-13 19:10:38
...
public List<TDjdReportMain> getDjdReportList(Integer year,Integer quarter,String memberSecondOrgcode,short status){
		return this.getHibernateTemplate().execute(new HibernateCallback<List<TDjdReportMain>>(){
			@Override
			public List<TDjdReportMain> doInHibernate(Session s) throws HibernateException {
				
				StringBuffer buf = new StringBuffer();
				Map<String,Object> params = new HashMap<String,Object>();
				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();
			}
		});
		
		
	}

 

相关标签: hibernate hql