首页 > 学院 > 开发设计 > 正文

Criter涉及到的查询

2019-11-06 06:03:38
字体:
来源:转载
供稿:网友
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 {@Testpublic void find() {SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();Session session = sessionFactory.openSession();Criteria cr = session.createCriteria(Student.class);List<Student> list = cr.list();Iterator<Student> 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());}}@Testpublic 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<Student> list = cr.list();Iterator<Student> 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());}}@Testpublic 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<Student> list = cr.list();Iterator<Student> 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());}}@Testpublic void orderfind() {SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();Session session = sessionFactory.openSession();Criteria cr = session.createCriteria(Student.class);cr.addOrder(Order.desc("age"));List<Student> list = cr.list();Iterator<Student> 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());}}@Testpublic 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<Student> list = cr.list();Iterator<Student> it = list.iterator();while (it.hasNext()) {Student s = it.next();System.out.println("id号是:" + s.getSid() + "姓名:" + s.getSname() + "密码:" + s.getPassword() + "年龄:" + s.getAge());}*/}@Testpublic void orderbyage() {SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();Session session = sessionFactory.openSession();Criteria cr = session.createCriteria(Student.class);cr.addOrder(Order.desc("age"));List<Student> list = cr.list();Iterator<Student> 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());}}@Testpublic 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<Student> list = cr.list();Iterator<Student> 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());}}@Testpublic 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<Student> 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());}*/}@Testpublic 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<Student> 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());}}}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表