Criter涉及到的查询

package com.etc.testCriteria;


import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;


import com.etc.entity.Student;


public class testCriteriademo {


@Test
public void find() {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Student.class);
List list = cr.list();
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(
"id号是:" + s.getSid()+"班级:"+s.getCid() + "姓名:" + s.getSname() + "密码:" + s.getPassword() + "年龄:" + s.getAge()+"出生日期"+s.getBirthday());
}


}


@Test
public void findbycond() {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Student.class);
// 查询年龄大于11岁且小于14岁的的学生
cr.add(Restrictions.gt("age", 11));
cr.add(Restrictions.lt("age", 14));
List list = cr.list();


Iterator it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println("id号是:" + s.getSid()+"班级:"+s.getCid() + "姓名:" + s.getSname() + "密码:" + s.getPassword() + "年龄:" + s.getAge()+"出生日期"+s.getBirthday());
}


}

@Test
public void findorcond() {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Student.class);
// 查询年龄大于11岁且小于14岁的的学生
cr.add(Restrictions.or(Restrictions.lt("age", 12), Restrictions.gt("age", 13)));
List list = cr.list();
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println("id号是:" + s.getSid()+"班级:"+s.getCid() + "姓名:" + s.getSname() + "密码:" + s.getPassword() + "年龄:" + s.getAge()+"出生日期"+s.getBirthday());
}


}

@Test
public void orderfind() {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Student.class);
cr.addOrder(Order.desc("age"));
List list = cr.list();
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println("id号是:" + s.getSid() +"班级:"+s.getCid()+ "姓名:" + s.getSname() + "密码:" + s.getPassword() + "年龄:" + s.getAge()+"出生日期"+s.getBirthday());
}


}
@Test
public void countfind() {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Student.class);
cr.setProjection(Projections.avg("age"));
System.out.println("学生平均年龄是:"+cr.uniqueResult());
/*List list = cr.list();
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println("id号是:" + s.getSid() + "姓名:" + s.getSname() + "密码:" + s.getPassword() + "年龄:" + s.getAge());
}
*/
}
@Test
public void orderbyage() {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Student.class);
cr.addOrder(Order.desc("age"));
List list = cr.list();
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println("id号是:" + s.getSid()+"班级:"+s.getCid() + "姓名:" + s.getSname() + "密码:" + s.getPassword() + "年龄:" + s.getAge()+"出生日期"+s.getBirthday());
}


}
@Test
public void birthbetween() {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Student.class);
Calendar c1=Calendar.getInstance();
c1.set(Calendar.YEAR, 2014);
c1.set(Calendar.MONTH,0);
c1.set(Calendar.DATE, 1);

Calendar c2=Calendar.getInstance();
c2.set(Calendar.YEAR, 2014);
c2.set(Calendar.MONTH, 11);
c2.set(Calendar.DATE, 1);

Date d1=c1.getTime();
Date d2=c2.getTime();


cr.add(Restrictions.between("birthday", d1, d2));
List list = cr.list();
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println("id号是:" + s.getSid()+"班级:"+s.getCid() + "姓名:" + s.getSname() + "密码:" + s.getPassword() + "年龄:" + s.getAge()+"出生日期"+s.getBirthday());
}


}
@Test
public void groupbyclass() {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Student.class);
cr.setProjection(Projections.groupProperty("cid"));
List list = cr.list();
System.out.println(list.size());
/*Iterator it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println("id号是:" + s.getSid()+"班级:"+s.getCid() + "姓名:" + s.getSname() + "密码:" + s.getPassword() + "年龄:" + s.getAge()+"出生日期"+s.getBirthday());
}*/


}
@Test
public void detchedCriteria() {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Student.class);
//先建立DetchedCriteria对象
DetachedCriteria detachedCriteria=DetachedCriteria.forClass(Student.class);
//加入查询条件
detachedCriteria.add(Restrictions.eq("age", 13));
//绑定session并返回一个Criteria实例
cr=detachedCriteria.getExecutableCriteria(session);


List list = cr.list();
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println("id号是:" + s.getSid()+"班级:"+s.getCid() + "姓名:" + s.getSname() + "密码:" + s.getPassword() + "年龄:" + s.getAge()+"出生日期"+s.getBirthday());
}


}
}

你可能感兴趣的:(Criteria)