hql 分页及查询

hql 分页及查询

package test;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import m2o.Tabjd;
import m2o.TabjdDAO;

public class SelectCriteria {

//Criteria面向对象的查询
public List search(){
TabjdDAO dao=new TabjdDAO();
Criteria c=dao.getSession().createCriteria((new Tabjd()).getClass());
c.add(Restrictions.like("jdname", "路",MatchMode.ANYWHERE));//模糊查询
c.add(Restrictions.ge("jdid", 1)); //ge相当于大于等于
c.add(Restrictions.le("jdid", 3)); //le相当于小于等于
c.addOrder(Order.asc("jdid"));//排序
return c.list();
}

//hql分页的查询
public List search(int pageNo, int pageSize){
TabjdDAO dao=new TabjdDAO();
String hql="from Tabjd jd order by jd.jdid asc";
Query query=dao.getSession().createQuery(hql);
int firstResultIndex=pageSize*(pageNo-1);
query.setFirstResult(firstResultIndex);
query.setMaxResults(pageSize);
return query.list();
}

public static void main(String[] args){
SelectCriteria sc=new SelectCriteria();
List lw=sc.search();
System.out.println("*******");
Iterator it=lw.iterator();
while (it.hasNext()){
Tabjd jd=(Tabjd)it.next();
System.out.println(jd.getJdname()+"**"+jd.getTabqx().getQxname()+"**"+jd.getJdid());
}
List l=sc.search(2, 2);
System.out.println("*******");
Iterator itt=l.iterator();
while (itt.hasNext()){
Tabjd jd=(Tabjd)itt.next();
System.out.println(jd.getJdname()+"**"+jd.getTabqx().getQxname()+"**"+jd.getJdid());
}

}
}

 

你可能感兴趣的:(hql 分页及查询)