JPA Criteria ORDER BY排序
ORDER BY 子句用于对数据进行排序并按升序或降序排列它们。CriteriaQuery 接口提供 orderBy() 方法来定义排序的类型。
JPA Criteria ORDER BY排序示例
在这里,我们将对学生表执行几个 ORDER BY 操作。让我们假设该表包含以下记录:
StudentEntity实体类
package com.yiidian.pojo;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class StudentEntity {
@Id
private int s_id;
private String s_name;
private int s_age;
public StudentEntity(int s_id, String s_name, int s_age) {
super();
this.s_id = s_id;
this.s_name = s_name;
this.s_age = s_age;
}
public StudentEntity() {
super();
}
public int getS_id() {
return s_id;
}
public void setS_id(int s_id) {
this.s_id = s_id;
}
public String getS_name() {
return s_name;
}
public void setS_name(String s_name) {
this.s_name = s_name;
}
public int getS_age() {
return s_age;
}
public void setS_age(int s_age) {
this.s_age = s_age;
}
}
persistence.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Student_details">
<class>com.yiidian.pojo.StudentEntity</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/jpa?serverTimezone=UTC&useSSL=false" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="eclipselink.logging.level" value="SEVERE" />
<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
</properties>
</persistence-unit>
</persistence>
按升序排序
package com.yiidian.test;
import com.yiidian.pojo.StudentEntity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.*;
public class Asc {
public static void main(String args[]) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Student_details");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<StudentEntity> cq = cb.createQuery(StudentEntity.class);
Root<StudentEntity> stud = cq.from(StudentEntity.class);
cq.orderBy(cb.asc(stud.get("s_age")));
CriteriaQuery<StudentEntity> select = cq.select(stud);
TypedQuery<StudentEntity> q = em.createQuery(select);
List<StudentEntity> list = q.getResultList();
System.out.print("s_id");
System.out.print("\t s_name");
System.out.println("\t s_age");
for (StudentEntity s : list) {
System.out.print(s.getS_id());
System.out.print("\t" + s.getS_name());
System.out.println("\t" + s.getS_age());
}
em.getTransaction().commit();
em.close();
emf.close();
}
}
输出结果为:
按降序排序
package com.yiidian.test;
import com.yiidian.pojo.StudentEntity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.List;
public class Desc {
public static void main(String args[]) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Student_details");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<StudentEntity> cq = cb.createQuery(StudentEntity.class);
Root<StudentEntity> stud = cq.from(StudentEntity.class);
cq.orderBy(cb.desc(stud.get("s_age")));
CriteriaQuery<StudentEntity> select = cq.select(stud);
TypedQuery<StudentEntity> q = em.createQuery(select);
List<StudentEntity> list = q.getResultList();
System.out.print("s_id");
System.out.print("\t s_name");
System.out.println("\t s_age");
for (StudentEntity s : list) {
System.out.print(s.getS_id());
System.out.print("\t" + s.getS_name());
System.out.println("\t" + s.getS_age());
}
em.getTransaction().commit();
em.close();
emf.close();
}
}
输出结果为:
热门文章
优秀文章