public class According_condition {
public static void main(String[]args){
Session session=HibernateUtil.getSession();
List<Book2> zzy=new ArrayList<>();
String HQL="from Book2 bo where bo.bookCount=2000";
Query W=session.createQuery(HQL);
zzy=W.list();
Iterator iterator=zzy.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
}
}
/**
* Created by Administrator on 2015/12/16.
*这是用来测试分页查询的类。
*/
public class Page_query {
public static void main(String[]args){
Session session= HibernateUtil.getSession();
List<Book2> zzy=new ArrayList<>();
String HQL="from Book2 bo";
Query W=session.createQuery(HQL);
int pageNo=3;
int pageSize=4;
zzy=W.setFirstResult(0).setMaxResults(pageSize).list();
/**对应的两个方法setFirstResult设定从哪一个对象开始检索,默认情况下从0开始
* setMaxResults设定一次对多检索出来的对象的数目**/
Iterator iterator=zzy.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
}
}
/**
* Created by Administrator on 2015/12/15.
* 这是一个用来测试绑定参数的测试类,绑定参数的方式用?的方式
*/
public class parameter_binding_HQL {
public static void main(String[]args) {
Session session = HibernateUtil.getSession();
List<Book2> zzy=new ArrayList<>();
String HQL="from Book2 bo where bo.bookCount=?";
Query query=session.createQuery(HQL);
query.setParameter(0,2000);
zzy=query.list();
Iterator iterator=zzy.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
}
}
/**
* Created by Administrator on 2015/12/15
* 这是一个用来测试绑定参数的类,这里用的是:参数的放来来绑定参数.
*/
public class parameter_binding_HQL_new {
public static void main(String[]args) {
Session session = HibernateUtil.getSession();
List<Book2> zzy=new ArrayList<>();
String HQL="from Book2 bo where bo.bookCount=:count";
Query query=session.createQuery(HQL);
query.setParameter("count",1222);
zzy=query.list();
Iterator iterator=zzy.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
}
}
/**
* Created by Administrator on 2015/12/15.
* 这个类用来使用HQL语句来查询所有的对象。
*/
public class select_all {
public static void main(String[]args){
Session session= HibernateUtil.getSession();
List<Book2> zzy=new ArrayList<Book2>();
String HQL="from Book2 bo";
Query W=session.createQuery(HQL);
zzy=W.list();
Iterator iterator=zzy.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
}
}
/**
* Created by Administrator on 2015/12/15.
* 这是一个可以根据降序和升序排序的HQL查询语句
*/
public class sort_query {
public static void main(String[]args){
Session session= HibernateUtil.getSession();
List<Book2> zzy=new ArrayList<Book2>();
String HQL="from Book2 bo order by bo.id desc";
//desc是降序,asc是升序
Query W=session.createQuery(HQL);
zzy=W.list();
Iterator iterator=zzy.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
}
}