Hibernate 条件查询使用 hql 语句查询
原创
©著作权归作者所有:来自51CTO博客作者wx6316c4e40e2ec的原创作品,请联系作者获取转载授权,否则将追究法律责任
package cn.fh.jq.dao.impl;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import cn.cn.Utils.FenYe;
import cn.cn.Vo.HousesVo;
import cn.cn.dao.HousesDao;
import cn.cn.model.Houses;
import cn.cn.model.Housesarea;
import cn.cn.model.Userr;
@Repository
public class HousesDaoImpl extends HibernateDaoSupport implements HousesDao{
@Autowired
private void ss(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
@Override
public FenYe queryHouses(HousesVo vo, FenYe fy) {
String hql="from Houses where 1=1";
List<Object>li=new ArrayList<Object>();
//房源小区:
if(!StringUtils.isEmpty(vo.getHousName())) {
hql+=" and housName like ?";
li.add("%"+vo.getHousName()+"%");
}
//是否出售:出售 不出售
if(vo.getHousSell()!=null) {
hql+=" and housSell = ?";
li.add(vo.getHousSell());
}
//房源类型:一居室 二居室 三居室 四居室
if(!StringUtils.isEmpty(vo.getHousType())) {
String[] arr = vo.getHousType().split(",");
for (int i = 0; i < arr.length; i++) {
hql+=" and housType like ?";
li.add("%"+vo.getHousType()+"%");
}
}
//入住时间:
if(vo.getOneDate()!=null) {
hql+=" and housDate >= ?";
li.add(vo.getOneDate());
}
//退房时间
if(vo.getOutDate()!=null) {
hql+=" and housDate <= ?";
li.add(vo.getOutDate());
}
//房源地区:
if(vo.getHousesarea()!=null && vo.getHousesarea().getAreaId()!=-1) {
hql+=" and housesarea.areaId = ?";
li.add(vo.getHousesarea().getAreaId());
}
Query cq = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
for (int i = 0; i < li.size(); i++) {
cq.setParameter(i, li.get(i));
}
fy.setzongTiaoShu(cq.list().size());
fy.count();
cq.setFirstResult(fy.getqiShiZhi());
cq.setMaxResults(fy.getmeiYeShu());
List<Houses> list = cq.list();
fy.setList(list);
return fy;
}