JavaWeb_(Hibernate框架)Hibernate中数据查询语句Criteria基本用法
Criteria进行数据查询与HQL和SQL的区别是Criteria完全是面向对象的方式在进行数据查询,将不再看到有sql语句的痕迹,使用Criteria 查询数据包括以下步骤:
1. 通过session的createCriteria创建一个Criteria 对象
2. Criteria.add 增加约束。 在本例中增加一个对name的模糊查询(like)
3. 调用list()方法返回查询结果的集合
本文展示四种基本的Criteria查询用法
1.Criteria基本查询 2.Criteria条件查询 3.Criteria查询总数 4.Criteria查询用户名中带有字符'a'的总人数
package com.Gary.dao; import java.util.List; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.query.Query; import com.Gary.domain.User; import com.Gary.utils.HibernateUtils; public class CriteriaDao { //Criteria基本查询 public void search() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select * from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据) CriteriaQuery<User> createQuery = criteriaBuilder.createQuery(User.class); Root<User> from = createQuery.from(User.class); createQuery.select(from); List<User> resultList = session.createQuery(createQuery).getResultList(); System.out.println(resultList); //模板:完成操作 beginTransaction.commit(); session.close(); } //条件查询 public void search2() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select * from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据) CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class); Root<User> root = criteria.from(User.class); //查询到数据库中所有值 criteria.select(root).where(root.get("id").in("1")); //执行查询 List<User> resultList = session.createQuery(criteria).getResultList(); //打印出查询出来的第1个用户 System.out.println(resultList.get(0).getUsername()); //模板:完成操作 beginTransaction.commit(); session.close(); } //条件总数记录查询 public void search3() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select count(*) from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据)(数Integer Long) CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class); //查找哪个表 Root<User> root = criteria.from(User.class); criteria.select(criteriaBuilder.count(root)); //执行查询 Long count = session.createQuery(criteria).uniqueResult(); //查询总数 System.out.println(count); //模板:完成操作 beginTransaction.commit(); session.close(); } //查询名字中带有a的人数总数 -- 2 public void search4() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select count(*) from user where username like '%i%' CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class); Root<User> root = criteria.from(User.class); criteria.select(criteriaBuilder.count(root)).where(criteriaBuilder.like(root.get("username"),"%a%")); Long count = session.createQuery(criteria).uniqueResult(); System.out.println(count); //模板:完成操作 beginTransaction.commit(); session.close(); } }
数据库user表
向数据库中添加假数据
1、Criteria基本查询
Root<User> from = createQuery.from(User.class); createQuery.select(from);
CriteriaDao.java向数据库发起查询请求
//Criteria基本查询 public void search() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select * from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据) CriteriaQuery<User> createQuery = criteriaBuilder.createQuery(User.class); Root<User> from = createQuery.from(User.class); createQuery.select(from); List<User> resultList = session.createQuery(createQuery).getResultList(); System.out.println(resultList); //模板:完成操作 beginTransaction.commit(); session.close(); }
package com.Gary.dao; import java.util.List; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.query.Query; import com.Gary.domain.User; import com.Gary.utils.HibernateUtils; public class CriteriaDao { //Criteria基本查询 public void search() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select * from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据) CriteriaQuery<User> createQuery = criteriaBuilder.createQuery(User.class); Root<User> from = createQuery.from(User.class); createQuery.select(from); List<User> resultList = session.createQuery(createQuery).getResultList(); System.out.println(resultList); //模板:完成操作 beginTransaction.commit(); session.close(); } }
2、Criteria条件查询
//createQuery-->查询条件(首先需要知道查询什么类型数据) CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class); Root<User> root = criteria.from(User.class); //查询到数据库中所有值 criteria.select(root).where(root.get("id").in("1")); //执行查询 List<User> resultList = session.createQuery(criteria).getResultList();
CriteriaDao.java向数据库发起查询请求
public void search2() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select * from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据) CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class); Root<User> root = criteria.from(User.class); //查询到数据库中所有值 criteria.select(root).where(root.get("id").in("1")); //执行查询 List<User> resultList = session.createQuery(criteria).getResultList(); //打印出查询出来的第1个用户 System.out.println(resultList.get(0).getUsername()); //模板:完成操作 beginTransaction.commit(); session.close(); }
package com.Gary.dao; import java.util.List; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.query.Query; import com.Gary.domain.User; import com.Gary.utils.HibernateUtils; public class CriteriaDao { //Criteria基本查询 public void search() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select * from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据) CriteriaQuery<User> createQuery = criteriaBuilder.createQuery(User.class); Root<User> from = createQuery.from(User.class); createQuery.select(from); List<User> resultList = session.createQuery(createQuery).getResultList(); System.out.println(resultList); //模板:完成操作 beginTransaction.commit(); session.close(); } public void search2() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select * from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据) CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class); Root<User> root = criteria.from(User.class); //查询到数据库中所有值 criteria.select(root).where(root.get("id").in("1")); //执行查询 List<User> resultList = session.createQuery(criteria).getResultList(); //打印出查询出来的第1个用户 System.out.println(resultList.get(0).getUsername()); //模板:完成操作 beginTransaction.commit(); session.close(); } }
3、Criteria查询总数
//createQuery-->查询条件(首先需要知道查询什么类型数据)(数Integer Long) CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class); //查找哪个表 Root<User> root = criteria.from(User.class); criteria.select(criteriaBuilder.count(root)); //执行查询 Long count = session.createQuery(criteria).uniqueResult();
CriteriaDao.java向数据库发起查询请求
public void search3() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select count(*) from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据)(数Integer Long) CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class); //查找哪个表 Root<User> root = criteria.from(User.class); criteria.select(criteriaBuilder.count(root)); //执行查询 Long count = session.createQuery(criteria).uniqueResult(); //查询总数 System.out.println(count); //模板:完成操作 beginTransaction.commit(); session.close(); }
package com.Gary.dao; import java.util.List; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.query.Query; import com.Gary.domain.User; import com.Gary.utils.HibernateUtils; public class CriteriaDao { //Criteria基本查询 public void search() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select * from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据) CriteriaQuery<User> createQuery = criteriaBuilder.createQuery(User.class); Root<User> from = createQuery.from(User.class); createQuery.select(from); List<User> resultList = session.createQuery(createQuery).getResultList(); System.out.println(resultList); //模板:完成操作 beginTransaction.commit(); session.close(); } //条件查询 public void search2() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select * from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据) CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class); Root<User> root = criteria.from(User.class); //查询到数据库中所有值 criteria.select(root).where(root.get("id").in("1")); //执行查询 List<User> resultList = session.createQuery(criteria).getResultList(); //打印出查询出来的第1个用户 System.out.println(resultList.get(0).getUsername()); //模板:完成操作 beginTransaction.commit(); session.close(); } //条件总数记录查询 public void search3() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select count(*) from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据)(数Integer Long) CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class); //查找哪个表 Root<User> root = criteria.from(User.class); criteria.select(criteriaBuilder.count(root)); //执行查询 Long count = session.createQuery(criteria).uniqueResult(); //查询总数 System.out.println(count); //模板:完成操作 beginTransaction.commit(); session.close(); } }
4、Criteria查询用户名中带有字符'a'的总人数
criteria.select(criteriaBuilder.count(root)).where(criteriaBuilder.like(root.get("username"),"%a%"));
CriteriaDao.java向数据库发起查询请求
//查询名字中带有a的人数总数 -- 2 public void search4() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select count(*) from user where username like '%i%' CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class); Root<User> root = criteria.from(User.class); criteria.select(criteriaBuilder.count(root)).where(criteriaBuilder.like(root.get("username"),"%a%")); Long count = session.createQuery(criteria).uniqueResult(); System.out.println(); //模板:完成操作 beginTransaction.commit(); session.close(); }
package com.Gary.dao; import java.util.List; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.query.Query; import com.Gary.domain.User; import com.Gary.utils.HibernateUtils; public class CriteriaDao { //Criteria基本查询 public void search() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select * from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据) CriteriaQuery<User> createQuery = criteriaBuilder.createQuery(User.class); Root<User> from = createQuery.from(User.class); createQuery.select(from); List<User> resultList = session.createQuery(createQuery).getResultList(); System.out.println(resultList); //模板:完成操作 beginTransaction.commit(); session.close(); } //条件查询 public void search2() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select * from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据) CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class); Root<User> root = criteria.from(User.class); //查询到数据库中所有值 criteria.select(root).where(root.get("id").in("1")); //执行查询 List<User> resultList = session.createQuery(criteria).getResultList(); //打印出查询出来的第1个用户 System.out.println(resultList.get(0).getUsername()); //模板:完成操作 beginTransaction.commit(); session.close(); } //条件总数记录查询 public void search3() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select count(*) from user CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //createQuery-->查询条件(首先需要知道查询什么类型数据)(数Integer Long) CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class); //查找哪个表 Root<User> root = criteria.from(User.class); criteria.select(criteriaBuilder.count(root)); //执行查询 Long count = session.createQuery(criteria).uniqueResult(); //查询总数 System.out.println(count); //模板:完成操作 beginTransaction.commit(); session.close(); } //查询名字中带有a的人数总数 -- 2 public void search4() { Session session = HibernateUtils.getSession(); Transaction beginTransaction = session.beginTransaction(); //模板:开始Criteria操作 //操作 select count(*) from user where username like '%i%' CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class); Root<User> root = criteria.from(User.class); criteria.select(criteriaBuilder.count(root)).where(criteriaBuilder.like(root.get("username"),"%a%")); Long count = session.createQuery(criteria).uniqueResult(); System.out.println(count); //模板:完成操作 beginTransaction.commit(); session.close(); } }
(如需转载学习,请标明出处)