在 SQL 语句中,使用 order by 语句用于对结果集进行排序。JPQL 中也支持使用 order by 语句对结果进行排序,默认按照升序(ASC)对记录进行排序。如果您希望按照降序对记录进行排序,可以使用 DESC 关键字。
(1)配置 persistence.xml,如下:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"> <persistence-unit name="openJPA2" transaction-type="RESOURCE_LOCAL"> <!-- JPA提供者 --> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <!-- 声明实体类 --> <class>com.hxstrive.openjpa.entity2.User</class> <!-- 配置JPA数据库属性 --> <properties> <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/openjpa_learn?useSSL=false& serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"/> <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/> <property name="openjpa.ConnectionUserName" value="root"/> <property name="openjpa.ConnectionPassword" value="aaaaaa"/> <property name="openjpa.Log" value="SQL=TRACE"/> <!-- 自动生成表 --> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> <!-- 不使用加载时强化和编译时强化,使用运行时Unenhanced(不能发挥OpenJPA的最大效能,所以也不推荐) --> <property name="openjpa.ClassLoadEnhancement" value="false"/> <property name="openjpa.DynamicEnhancementAgent" value="false"/> <property name="openjpa.RuntimeUnenhancedClasses" value="supported"/> </properties> </persistence-unit> </persistence>
(2)用户表实体映射,代码如下:
@Data @Entity @Table public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column private String name; @Column private Integer age; @Column private Float salary; }
实例1:根据用户薪水降序排序,代码如下:
public class JPQLDemo7 { /** 持久化单元名称 */ private static final String PERSISTENCE_NAME = "openJPA2"; public static void main(String[] args) { EntityManagerFactory factory = Persistence.createEntityManagerFactory( PERSISTENCE_NAME, System.getProperties()); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); // 根据薪水 salary 递减排序 TypedQuery<User> query = em.createQuery( "Select u from User u order by u.salary desc", User.class); for(User user : query.getResultList()) { System.out.println(user); } em.getTransaction().commit(); factory.close(); System.out.println("finished."); } }
输出 SQL 语句如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 ORDER BY t0.salary DESC User{id=6, name='用户-5', age=73, salary=8892.097} User{id=2, name='用户-1', age=80, salary=7253.2427} User{id=1, name='用户-0', age=56, salary=7251.132} User{id=9, name='用户-8', age=79, salary=7199.551} User{id=10, name='用户-9', age=26, salary=6327.472} User{id=3, name='用户-2', age=44, salary=5855.36} User{id=7, name='用户-6', age=77, salary=5494.797} User{id=4, name='用户-3', age=10, salary=4105.1895} User{id=8, name='用户-7', age=80, salary=3047.556} User{id=5, name='用户-4', age=53, salary=2927.5222}
实例2:先根据用户薪水降序排序,然后根据用户年龄升序排序。代码如下:
public class JPQLDemo7 { /** 持久化单元名称 */ private static final String PERSISTENCE_NAME = "openJPA2"; public static void main(String[] args) { EntityManagerFactory factory = Persistence.createEntityManagerFactory( PERSISTENCE_NAME, System.getProperties()); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); // 先根据用户薪水降序排序,然后根据用户年龄排序 TypedQuery<User> query = em.createQuery( "Select u from User u order by u.salary desc, u.age asc", User.class); for(User user : query.getResultList()) { System.out.println(user); } em.getTransaction().commit(); factory.close(); System.out.println("finished."); } }
输出 SQL 语句如下:
SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 ORDER BY t0.salary DESC, t0.age ASC User{id=6, name='用户-5', age=73, salary=8892.097} User{id=2, name='用户-1', age=80, salary=7253.2427} User{id=1, name='用户-0', age=56, salary=7251.132} User{id=9, name='用户-8', age=79, salary=7199.551} User{id=10, name='用户-9', age=26, salary=6327.472} User{id=3, name='用户-2', age=44, salary=5855.36} User{id=7, name='用户-6', age=77, salary=5494.797} User{id=4, name='用户-3', age=10, salary=4105.1895} User{id=8, name='用户-7', age=80, salary=3047.556} User{id=5, name='用户-4', age=53, salary=2927.5222}