动态拼接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();
}
});
}
上一篇: 放屁的孩子像根草
推荐阅读