JPA标准ORDER BY子句
ORDER BY子句用于对数据进行排序并按升序或降序排列数据。 CriteriaQuery
接口提供了orderBy()
方法来定义排序的类型。
标准ORDER BY示例
在这里,我们将在student
表上执行多个ORDER BY
操作。假设该表格包含以下记录 -
DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `S_ID` int(11) NOT NULL, `S_NAME` varchar(255) DEFAULT NULL, `S_AGE` int(11) DEFAULT NULL, PRIMARY KEY (`S_ID`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES ('101', 'Gaurav', '24'); INSERT INTO `student` VALUES ('102', 'Rahul', '22'); INSERT INTO `student` VALUES ('103', 'Chris', '20'); INSERT INTO `student` VALUES ('104', 'Ronit', '26'); INSERT INTO `student` VALUES ('105', 'Roy', '21');
现在,请按照以下步骤执行操作: -
第1步: 创建一个实体类。在com.zyiz.jpa
包下创建了StudentEntity.java
类文件。 该类包含三个属性:s_id
,s_name
,s_age
以及所有必需的注解。
文件:StudentEntity.java -
package com.zyiz.jpa; import javax.persistence.*; @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; } }
第2步: 将实体类和其他数据库配置映射到 persistence.xml 文件中。
文件: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.zyiz.jpa.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/testdb?serverTimezone=UTC" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="123456" /> <property name="eclipselink.logging.level" value="SEVERE" /> <property name="eclipselink.ddl-generation" value="create-or-extend-tables" /> </properties> </persistence-unit> </persistence>
这里,我们创建了基本实体类并将配置映射到persistence.xml 文件中,可以通过以下方式执行不同类型的SELECT
排序操作 -
1. 按升序排序
在这里,我们将通过一个简单的示例从数据库查询并按s_age
字段升序排序。
文件:Asc.java -
package com.zyiz.jpa.jpql; import com.zyiz.jpa.StudentEntity; import javax.persistence.*; import javax.persistence.criteria.*; 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(); } }
执行上面代码,得到以下输出结果 -
s_id s_name s_age Chris 20 Roy 21 Rahul 22 Gaurav 24 Ronit 26
2. 按降序排序
在这里,我们将通过一个简单的示例从数据库查询并按s_age
字段降序排序。
文件:Desc.java -
package com.zyiz.jpa.jpql; import com.zyiz.jpa.StudentEntity; import javax.persistence.*; import javax.persistence.criteria.*; import java.util.*; 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(); } }
执行上面代码,得到以下输出结果 -
s_id s_name s_age Ronit 26 Gaurav 24 Rahul 22 Roy 21 Chris 20
上一篇:JPA标准SELECT子句
下一篇:JPA标准WHERE子句
- Java教程
- Vim教程
- Swing教程
- Spring教程
- Spring Web Services教程
- Spring MVC教程
- Spring JDBC教程
- Spring Cloud教程
- Spring Boot教程
- Spring Boot CLI教程
- Spring Batch教程
- Spring AOP教程
- PDFBox教程
- JSP教程
- JSF教程
- JPA教程
- Java面向对象设计
- Java设计模式
- Java虚拟机教程
- Java泛型教程
- Java正则表达式教程
- Java数据类型教程
- Java并发编程教程
- Java密码学教程
- Java多线程教程
- Java国际化(i18n)教程
- JavaFX教程
- Java9教程
扫描二维码
程序员编程王