Java JPA 标准API使用
Posted 程序员JohnDeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java JPA 标准API使用相关的知识,希望对你有一定的参考价值。
一.创建条件查询的步骤
- 第一步通过EntityManager接口实例调用getCriteriaBuilder()方法创建CriteriaBuilder接口的对象
EntityManager em = emf.createEntityManager();
CriteriaBuilder cb= em.getCriteriaBuilder();
- 第二步构造一个CriteriaQuery接口实例来创建一个查询对象
CriteriaQuery<StudentEntity> cq =cb.createQuery(StudentEntity.class);
- 第三步从CriteriaQuery对象方法调用来设置查询的根
Root<StudentEntity> rootStudent =cq.from(StudentEntity.class);
- 第四步调用CriteriaQuery对象的select()方法指定查询结果集类型
CriteriaQuery<StudentEntity> select =cq.select(rootStudent);
- 第五步创建Query接口实例指定访问数据库记录的方法类型
Query query=em.createQuery(select);
- 第六步通过查询接口的方法来控制查询的执行
List<StudentEntity> list= query.getResultList();
二.标准API查询子句的方法
字句 | Criteria API接口 | 方法 |
---|---|---|
SELECT | CriteriaQuery | select() |
FROM | AbstractQuery | from() |
WHERE | AbstractQuery | where() |
ORDER BY | CriteriaQuery | orderBy() |
GROUP BY | AbstractQuery | groupBy() |
HAVING | AbstractQuery | having() |
三.举例说明查询代码实例
- 查询学生名称列表(一个列名)
EntityManagerFactory emf =Persistence.createEntityManagerFactory("student");
EntityManager em= emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<StudentEntity> cq = cb.createQuery(StudentEntity.class);
Root<StudentEntity> stud = cq.from(StudentEntity.class);
cq.select(stud.get("name"));
CriteriaQuery<StudentEntity> select = cq.select(stud);
TypedQuery<StudentEntity> query = em.createQuery(select);
List<StudentEntity> list = query.getResultList();
- 查询名称,年龄,性别学生列表(多个列名)
EntityManagerFactory emf =Persistence.createEntityManagerFactory("student");
EntityManager em= emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<StudentEntity> cq = cb.createQuery(StudentEntity.class);
Root<StudentEntity> stud = cq.from(StudentEntity.class);
cq.multiselect(stud.get("name"),stud.get("age"),stud.get("sex"));
CriteriaQuery<StudentEntity> select = cq.select(stud);
TypedQuery<StudentEntity> query = em.createQuery(select);
List<StudentEntity> list = query.getResultList();
- 根据名称排序(升序)查询学生列表(orderBy)
EntityManagerFactory emf =Persistence.createEntityManagerFactory("student");
EntityManager em= emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<StudentEntity> cq = cb.createQuery(StudentEntity.class);
Root<StudentEntity> stud = cq.from(StudentEntity.class);
cq.orderBy(cb.asc(stud.get("name"));
CriteriaQuery<StudentEntity> select = cq.select(stud);
TypedQuery<StudentEntity> query = em.createQuery(select);
List<StudentEntity> list = query.getResultList();
- 根据学生名称模糊查询列表(like)
EntityManagerFactory emf =Persistence.createEntityManagerFactory("student");
EntityManager em= emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
AbstractQuery<StudentEntity> cq = cb.createQuery(StudentEntity.class);
Root<StudentEntity> stud = cq.from(StudentEntity.class);
cq.where(cb.like(stud.get("name"),"%");
CriteriaQuery<StudentEntity> select = ((CriteriaQuery<StudentEntity>) cq).select(stud);
TypedQuery<StudentEntity> query = em.createQuery(select);
List<StudentEntity> list = query.getResultList();
- 根据多个学生ID查询列表(in)
EntityManagerFactory emf =Persistence.createEntityManagerFactory("student");
EntityManager em= emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
AbstractQuery<StudentEntity> cq = cb.createQuery(StudentEntity.class);
Root<StudentEntity> stud = cq.from(StudentEntity.class);
cq.where(cb.in(stud.get("id")).value("1001").value("1002").value("1003");
CriteriaQuery<StudentEntity> select = ((CriteriaQuery<StudentEntity>) cq).select(stud);
TypedQuery<StudentEntity> query = em.createQuery(select);
List<StudentEntity> list = query.getResultList();
- 查询10到18岁的学生列表(between)
EntityManagerFactory emf =Persistence.createEntityManagerFactory("student");
EntityManager em= emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
AbstractQuery<StudentEntity> cq = cb.createQuery(StudentEntity.class);
Root<StudentEntity> stud = cq.from(StudentEntity.class);
cq.where(cb.between(stud.get("age"),10,18));
CriteriaQuery<StudentEntity> select = ((CriteriaQuery<StudentEntity>) cq).select(stud);
TypedQuery<StudentEntity> query = em.createQuery(select);
List<StudentEntity> list = query.getResultList();
- 统计每个年龄段的学生有多少个(groupBy)
EntityManagerFactory emf =Persistence.createEntityManagerFactory("student");
EntityManager em= emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class);
Root<StudentEntity> stud = cq.from(StudentEntity.class);
cq.multiselect(stud.get("age"), cb.count(stud)).groupBy(stud.get("age"));
List<Object[]> list = em.createQuery(cq).getResultList();
- 统计24岁年龄段的学生有多少个
EntityManagerFactory emf =Persistence.createEntityManagerFactory("student");
EntityManager em= emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class);
Root<StudentEntity> stud = cq.from(StudentEntity.class);
cq.multiselect(stud.get("age"), cb.count(stud)).groupBy(stud.get("age"))
.having(cb.ge(stud.get("age"), 24));
List<Object[]> list = em.createQuery(cq).getResultList();
以上是关于Java JPA 标准API使用的主要内容,如果未能解决你的问题,请参考以下文章
JPA 标准 API 和 Oracle JSON_TABLE 函数