在 JPA 中,可以通过 setFirstResult() 和 setMaxResult() 方法实现分页查询。
Query setFirstResult(int startPosition):该方法指定从哪一条开始查询数据,第一个结果的位置,从 0 开始编号。例如:setFirstResult(1) 表示从第二条数据开始读取数据
Query setMaxResult(int maxResult):该方法指定查询最多查询到多少条,例如:setMaxResult(5) 一次最多查询5条数据
(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; }
(3)客户端代码
import com.hxstrive.openjpa.entity2.User; import javax.persistence.*; import java.util.List; public class JPQLDemo4 { /** 持久化单元名称 */ private static final String PERSISTENCE_NAME = "openJPA2"; private static void pageQuery(EntityManager em, int pageNum, int pageSize) { TypedQuery<User> query = em.createQuery("select t from User t", User.class); // 设置查询位置的开始位置 query.setFirstResult((pageNum - 1) * pageSize); // 设置每次查询的最大数据条数 query.setMaxResults(pageSize); List<User> userList = query.getResultList(); for (User user : userList) { System.out.println(user); } } public static void main(String[] args) { EntityManagerFactory factory = Persistence.createEntityManagerFactory( PERSISTENCE_NAME, System.getProperties()); EntityManager em = factory.createEntityManager(); System.out.println("第一页"); pageQuery(em, 1, 3); System.out.println("第二页"); pageQuery(em, 2, 3); em.close(); factory.close(); System.out.println("finished."); } }
执行客户端代码输出的 SQL 语句或日志如下:
第一页 SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 LIMIT ?, ? [params=?, ?] User{id=1, name='用户-0', age=56, salary=7251.132} User{id=2, name='用户-1', age=80, salary=7253.2427} User{id=3, name='用户-2', age=44, salary=5855.36} 第二页 SELECT t0.id, t0.age, t0.name, t0.salary FROM User t0 LIMIT ?, ? [params=?, ?] User{id=4, name='用户-3', age=10, salary=4105.1895} User{id=5, name='用户-4', age=53, salary=2927.5222} User{id=6, name='用户-5', age=73, salary=8892.097}