GROUP BY子句用於從一個或多個表中收集資料並將它們排列在一個分組中。 在Criteria API中,AbstractQuery介面的groupBy()
方法用於過濾記錄並對它們進行分組。
在這裡,我們將在student
表上執行多個GROUP 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.yiibai.jpa
包下建立了StudentEntity.java
類檔案。 該類包含三個屬性:s_id
,s_name
,s_age
以及所有必需的註解。
檔案:StudentEntity.java -
package com.yiibai.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.yiibai.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 檔案中,可以通過以下方式執行不同型別的GROUP BY
排序操作 -
在這裡,我們將通過一個簡單的範例演示如何從資料庫指定GROUP BY條件查詢獲取分組後的資料。
檔案:StudentGroup.java -
package com.yiibai.jpa.jpql;
import com.yiibai.jpa.StudentEntity;
import javax.persistence.*;
import javax.persistence.criteria.*;
import java.util.*;
public class StudentGroup {
public static void main(String args[]) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Student_details");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class);
Root<StudentEntity> stud = cq.from(StudentEntity.class);
cq.multiselect(stud.get("s_age"), cb.count(stud)).groupBy(stud.get("s_age"));
System.out.print("s_age");
System.out.println("\t Count");
List<Object[]> list = em.createQuery(cq).getResultList();
for (Object[] object : list) {
System.out.println(object[0] + " " + object[1]);
}
em.getTransaction().commit();
em.close();
emf.close();
}
}
執行上面程式碼,得到以下輸出結果 -
s_age Count
20 1
21 1
22 1
24 1
26 1