原book對象java
1 package com.shaying.domain; 2 3 import javax.persistence.Column; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.GenerationType; 7 import javax.persistence.Id; 8 import javax.persistence.Table; 9 10 import lombok.Data; 11 12 @Data//可省略get、set方法,後續可直接使用get、set方法 13 @Entity 14 @Table(name="books") 15 public class Book { 16 @Id 17 @GeneratedValue(strategy=GenerationType.IDENTITY) 18 private Integer id; 19 @Column() 20 private String title; 21 @Column() 22 private Integer type; 23 @Column() 24 private double price; 25 public Book(){} 26 public Book(String title, double price) { 27 this.title = title; 28 this.price = price; 29 } 30 31 public String toString() { 32 return "Book [id=" + id + ", title=" + title + ", type=" + type + ", price=" + price + "]"; 33 } 34 }
BookInfo對象spring
1 package com.shaying.domain; 2 3 import lombok.Data; 4 5 @Data 6 public class BookInfo { 7 private Integer type; 8 private double maxPrice; 9 private double sumPrice; 10 11 public BookInfo(){} 12 public BookInfo(Integer type, double maxPrice, double sumPrice) { 13 this.type = type; 14 this.maxPrice = maxPrice; 15 this.sumPrice = sumPrice; 16 } 17 18 public String toString() { 19 return "BookInfo [type=" + type + ", maxPrice=" + maxPrice + ", sumPrice=" + sumPrice + "]"; 20 } 21 }
組建條件分組查詢語句,返回分頁查詢結果sql
1 package com.shaying.service; 2 3 import java.util.List; 4 5 import javax.persistence.EntityManager; 6 import javax.persistence.TypedQuery; 7 import javax.persistence.criteria.CriteriaBuilder; 8 import javax.persistence.criteria.CriteriaQuery; 9 import javax.persistence.criteria.Root; 10 11 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.data.domain.Page; 13 import org.springframework.data.domain.PageImpl; 14 import org.springframework.data.domain.PageRequest; 15 import org.springframework.data.domain.Pageable; 16 import org.springframework.stereotype.Service; 17 18 import com.shaying.domain.Book; 19 import com.shaying.domain.BookInfo; 20 21 @Service 22 public class BookQueryService { 23 24 @Autowired 25 private EntityManager entityManager; 26 27 /** 28 * select type,max(price) maxPrice,sum(price) sumPrice from books group by type 29 */ 30 public Page<BookInfo> groupBy(int index, int pageSize){ 31 //新建一個頁面,存放頁面信息 32 Pageable page = new PageRequest(index, pageSize); 33 //criteriaBuilder用於構建CriteriaQuery的構建器對象 34 CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); 35 //criteriaQuery包含查詢語句的各個部分,如where、max、sum、groupBy、orderBy等 36 CriteriaQuery<BookInfo> criteriaQuery = criteriaBuilder.createQuery(BookInfo.class); 37 //獲取查詢實例的屬性,select * from books 38 Root<Book> root = criteriaQuery.from(Book.class); 39 //至關於select type,max(price) maxPrice,sum(price) sumPrice from books中select 與 from之間的部分 40 criteriaQuery.multiselect(root.get("type"), criteriaBuilder.max(root.get("price")), criteriaBuilder.sum(root.get("price"))); 41 //where type = 1 42 criteriaQuery.where(criteriaBuilder.equal(root.get("type"), 1)); 43 //group by type 44 criteriaQuery.groupBy(root.get("type")); 45 //criteriaQuery拼成的sql是select type,max(price) maxPrice,sum(price) sumPrice from books group by type;查詢出的列與對象BookInfo的屬性對應 46 //記錄當前sql查詢結果總條數 47 List<BookInfo> counts = entityManager.createQuery(criteriaQuery).getResultList(); 48 //sql查詢對象 49 TypedQuery<BookInfo> createQuery = entityManager.createQuery(criteriaQuery); 50 //設置分頁參數 51 createQuery.setFirstResult(index*pageSize); 52 createQuery.setMaxResults(pageSize); 53 //返回查詢的分頁結果,createQuery.getResultList()爲分頁查詢的結果對象,counts.size()爲設置分頁參數以前查詢的總數 54 return new PageImpl<BookInfo>(createQuery.getResultList(), page, counts.size()); 55 } 56 }