惠州JPA如何查询分页?_北大青鸟IT学校
作者:邓华发布时间:2021-05-08分类:Java技术浏览:2473
你知道JPA如何查询分页吗?那么下面惠州北大青鸟老师给大家科普一下JPA如何查询分页,希望对大家学习Java有帮助。
PersonDaoImpl在下面的部分显示如何逐页显示结果。
实例
下面的代码来自Department.java。
package cn.w3cschool.common;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entitypublic class Department {
@Id private int id; private String name;
@OneToMany(mappedBy="department") private Collection<Employee> employees; public Department() {
employees = new ArrayList<Employee>();
}
public void setId(int id) {
this.id = id;
} public void setName(String name) {
this.name = name;
} public void setEmployees(Collection<Employee> employees) {
this.employees = employees;
} public int getId() {
return id;
}
public String getName() {
return name;
}
public Collection<Employee> getEmployees() {
return employees;
} public String toString() {
return "Department no: " + getId() +
", name: " + getName();
}
}
下面的代码来自PersonDaoImpl.java。
package cn.w3cschool.common;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.transaction.annotation.Transactional;
@Transactionalpublic class PersonDaoImpl { public void test() {
Employee emp = new Employee();
emp.setName("Tom");
emp.setSalary(123);
emp.setStartDate(new Date());
emp.setId(1);
Project pro = new Project();
pro.setName("Design");
pro.getEmployees().add(emp);
Department dept = new Department();
dept.setName("Dept");
dept.getEmployees().add(emp);
emp.setDepartment(dept);
emp.getProjects().add(pro);
em.persist(dept);
em.persist(pro);
em.persist(emp);
} private String reportQueryName; private int currentPage; private int maxResults; private int pageSize;
public int getPageSize() {
return pageSize;
}
public int getMaxPages() {
return maxResults / pageSize;
} public void init(int pageSize, String countQueryName,
String reportQueryName) {
this.pageSize = pageSize;
this.reportQueryName = reportQueryName;
maxResults = ((Long) em.createNamedQuery(countQueryName)
.getSingleResult()).intValue();
currentPage = 0;
}
public List getCurrentResults() {
return em.createNamedQuery(reportQueryName)
.setFirstResult(currentPage * pageSize)
.setMaxResults(pageSize)
.getResultList();
}
public void next() {
currentPage++;
}
public void previous() {
currentPage--; if (currentPage < 0) {
currentPage = 0;
}
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
@PersistenceContext private EntityManager em;
}
以下代码来自Project.java。
package cn.w3cschool.common;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entitypublic class Project {
@Id protected int id; protected String name;
@ManyToMany(mappedBy="projects") private Collection<Employee> employees; public Project() {
employees = new ArrayList<Employee>();
} public void setId(int id) {
this.id = id;
} public void setName(String name) {
this.name = name;
} public void setEmployees(Collection<Employee> employees) {
this.employees = employees;
} public int getId() {
return id;
}
public String getName() {
return name;
}
public Collection<Employee> getEmployees() {
return employees;
}
public String toString() {
return "Project id: " + getId() + ", name: " + getName();
}
}
以下代码来自Employee.java。
package cn.w3cschool.common;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@NamedQueries({
@NamedQuery(name="findEmployeesAboveSal",
query="SELECT e " + "FROM Employee e " + "WHERE e.department = :dept AND " + " e.salary > :sal"),
@NamedQuery(name="findHighestPaidByDepartment",
query="SELECT e " + "FROM Employee e " + "WHERE e.department = :dept AND " + " e.salary = (SELECT MAX(e2.salary) " + " FROM Employee e2 " + " WHERE e2.department = :dept)")
})public class Employee {
@Id private int id; private String name; private long salary;
@Temporal(TemporalType.DATE) private Date startDate;
@ManyToOne private Employee manager;
@OneToMany(mappedBy="manager") private Collection<Employee> directs;
@ManyToOne private Department department;
@ManyToMany
private Collection<Project> projects; public Employee() {
projects = new ArrayList<Project>();
directs = new ArrayList<Employee>();
} public void setId(int id) {
this.id = id;
} public void setName(String name) {
this.name = name;
} public void setSalary(long salary) {
this.salary = salary;
} public void setStartDate(Date startDate) {
this.startDate = startDate;
} public void setManager(Employee manager) {
this.manager = manager;
} public void setDirects(Collection<Employee> directs) {
this.directs = directs;
} public void setDepartment(Department department) {
this.department = department;
} public void setProjects(Collection<Project> projects) {
this.projects = projects;
} public int getId() {
return id;
}
public String getName() {
return name;
} public long getSalary() {
return salary;
} public Date getStartDate() {
return startDate;
}
public Department getDepartment() {
return department;
}
public Collection<Employee> getDirects() {
return directs;
}
public Employee getManager() {
return manager;
} public Collection<Project> getProjects() {
return projects;
}
public String toString() {
return "Employee " + getId() +
": name: " + getName() + ", salary: " + getSalary() + ", dept: " + ((getDepartment() == null) ? null : getDepartment().getName());
}
}
上面的代码生成以下结果。
以下是数据库转储。
Table Name: DEPARTMENT
Row:
Column Name: ID,
Column Type: INTEGER:
Column Value: 0
Column Name: NAME,
Column Type: VARCHAR:
Column Value: Dept
Table Name: EMPLOYEE
Row:
Column Name: ID,
Column Type: INTEGER:
Column Value: 1
Column Name: NAME,
Column Type: VARCHAR:
Column Value: Tom
Column Name: SALARY,
Column Type: BIGINT:
Column Value: 123
Column Name: STARTDATE,
Column Type: DATE:
Column Value: 2014-12-29
Column Name: DEPARTMENT_ID,
Column Type: INTEGER:
Column Value: 0
Column Name: MANAGER_ID,
Column Type: INTEGER:
Column Value: null
Table Name: EMPLOYEE_PROJECT
Row:
Column Name: EMPLOYEES_ID,
Column Type: INTEGER:
Column Value: 1
Column Name: PROJECTS_ID,
Column Type: INTEGER:
Column Value: 0
Table Name: PROJECT
Row:
Column Name: ID,
Column Type: INTEGER:
Column Value: 0
Column Name: NAME,
Column Type: VARCHAR:
Column Value: Design
想了解更多关于Java的资讯吗?可以来惠州北大青鸟新方舟校区了解一下。
Java
标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学校北大青鸟IT软件学校北大青鸟IT学校
- Java技术排行
- 标签列表
-
- Java (3694)
- 北大青鸟 (3713)
- 软件开发 (3613)
- JAVA (3413)
- UI设计入门 (2093)
- 惠州北大青鸟 (4375)
- 惠州IT培训 (2558)
- UI设计培训 (2090)
- 惠州UI设计培训 (2095)
- 惠州UI设计培训学校 (2090)
- 惠州计算机软件培训 (6260)
- 惠州计算件软件开发 (6260)
- 惠州计算机软件基础 (6261)
- 惠州计算机JAVA培训 (3574)
- 惠州计算机Java软件开发 (3620)
- 惠州计算机JAVA软件开发 (4645)
- 惠州计算机JAVA软件开发学校 (3338)
- 惠州计算机Java软件开发培训 (3338)
- 北大青鸟IT计算机学校 (5048)
- 北大青鸟IT软件学校 (5062)
- 北大青鸟IT学校 (5059)
- 惠州计算机UI设计软件开发 (2088)
- UI设计基础教程 (2088)
- UI设计是什么 (2088)
- UI设计教程 (2088)
- 网站分类
-
- 计算机教程
- 计算机入门
- 职业学校
- 新闻动态
- 专业课程
- 热门技术
- SEO
- 培训教程
- windows
- linux教程
- 系统集成
- 网站开发
- Html5
- 办公软件
- 师资力量
- 热点问答
- 联系我们
- 计算机学校
- 惠州计算机学校
- 河源计算机学校
- 广州计算机学校
- 深圳计算机学校
- 湛江计算机学校
- 佛山计算机学校
- IT计算机培训信息
- 设计专业
- UI
- 影视特效
- 游戏动漫设计
- Photoshop
- AI设计
- 软件教程
- Java技术
- C语言/C++语言培训
- C#
- Python技术
- PHP
- 数据库
- SQL Server
- 网络教程
- 网络安全
- 网络营销
- 软件专业
- 大数据专业
- 前端开发专业
- 软件测试专业
- Python专业
- 软件实施
- 珠海计算机学校
- 初中生学什么好
- 计算机认证
- 文章归档
-
- 2025年2月 (26)
- 2024年12月 (15)
- 2024年11月 (45)
- 2024年10月 (32)
- 2024年9月 (29)
- 2024年8月 (68)
- 2024年7月 (59)
- 2024年6月 (43)
- 2024年5月 (48)
- 2024年4月 (80)
- 2024年3月 (65)
- 2024年2月 (54)
- 2024年1月 (25)
- 2023年12月 (12)
- 2023年11月 (73)
- 2023年10月 (134)
- 2023年9月 (34)
- 2023年8月 (3)
- 2023年7月 (3)
- 2023年6月 (12)
- 2023年5月 (30)
- 2023年4月 (72)
- 2023年3月 (11)
- 2023年2月 (34)
- 2023年1月 (37)
- 2022年12月 (78)
- 2022年11月 (359)
- 2022年6月 (1193)
- 2022年5月 (570)
- 2022年4月 (1567)
- 2022年3月 (982)
- 2022年2月 (54)
- 2022年1月 (182)
- 2021年9月 (308)
- 2021年8月 (1704)
- 2021年7月 (2423)
- 2021年6月 (1806)
- 2021年5月 (1569)
- 2021年4月 (1380)
- 2021年3月 (1255)
- 2021年2月 (709)
- 2021年1月 (1521)
- 2020年12月 (3626)
- 2020年11月 (1646)
- 2020年10月 (1046)
- 2020年9月 (592)
- 最近发表
-
- 清远信息:南粤春暖共赴就业新征程清远市举办春季大型招聘活动|||计算机培训学校招生
- 江门信息:为什么要参加企业养老保险DeepSeek告诉你|||大学生计算机培训学校
- 东莞信息:香港劳工及福利局等代表团莅临东莞共促区域人才交流合作新发展|||大学生计算机培训学校
- 东莞信息:莞城街道2025年春风行动暨零工市场招聘活动|||计算机网络培训学校
- 汕头信息:招聘会开进高铁站汕头市南粤春暖专场招聘助力开门红|||计算机网络培训学校
- 江门信息:2025年江门市就业创业政策汇总|||广州计算机软件培训
- 梅州信息:2025年春暖梅州助力稳就业惠民生促发展现场招聘活动|||中专学计算机平面设计女生可以学计算机网络技术好吗
- 湛江信息:2024年乡村振兴人才驿站活动回顾之二赤坎篇|||中专学计算机平面设计女生可以学计算机网络技术好吗
- 汕头信息:新春送岗就在汕头!2025年首场就业洽谈会邀您参加内附参会企业名单|||广州计算机编程培训
- 茂名信息:150企业10000高薪好岗!茂名人社新春大型现场招聘会年初十盛大启幕!内附企业岗位名单|||计算机网络培训学校