原book对象

 package com.shaying.domain;

 import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table; import lombok.Data; @Data//可省略get、set方法,后续可直接使用get、set方法
@Entity
@Table(name="books")
public class Book {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column()
private String title;
@Column()
private Integer type;
@Column()
private double price;
public Book(){}
public Book(String title, double price) {
this.title = title;
this.price = price;
} public String toString() {
return "Book [id=" + id + ", title=" + title + ", type=" + type + ", price=" + price + "]";
}
}

BookInfo对象

 package com.shaying.domain;

 import lombok.Data;

 @Data
public class BookInfo {
private Integer type;
private double maxPrice;
private double sumPrice; public BookInfo(){}
public BookInfo(Integer type, double maxPrice, double sumPrice) {
this.type = type;
this.maxPrice = maxPrice;
this.sumPrice = sumPrice;
} public String toString() {
return "BookInfo [type=" + type + ", maxPrice=" + maxPrice + ", sumPrice=" + sumPrice + "]";
}
}

组建条件分组查询语句,返回分页查询结果

 package com.shaying.service;

 import java.util.List;

 import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import com.shaying.domain.Book;
import com.shaying.domain.BookInfo; @Service
public class BookQueryService { @Autowired
private EntityManager entityManager; /**
* select type,max(price) maxPrice,sum(price) sumPrice from books group by type
*/
public Page<BookInfo> groupBy(int index, int pageSize){
//新建一个页面,存放页面信息
Pageable page = new PageRequest(index, pageSize);
//criteriaBuilder用于构建CriteriaQuery的构建器对象
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
//criteriaQuery包含查询语句的各个部分,如where、max、sum、groupBy、orderBy等
CriteriaQuery<BookInfo> criteriaQuery = criteriaBuilder.createQuery(BookInfo.class);
//获取查询实例的属性,select * from books
Root<Book> root = criteriaQuery.from(Book.class);
//相当于select type,max(price) maxPrice,sum(price) sumPrice from books中select 与 from之间的部分
criteriaQuery.multiselect(root.get("type"), criteriaBuilder.max(root.get("price")), criteriaBuilder.sum(root.get("price")));
//where type = 1
criteriaQuery.where(criteriaBuilder.equal(root.get("type"), 1));
//group by type
criteriaQuery.groupBy(root.get("type"));
//criteriaQuery拼成的sql是select type,max(price) maxPrice,sum(price) sumPrice from books group by type;查询出的列与对象BookInfo的属性对应
//记录当前sql查询结果总条数
List<BookInfo> counts = entityManager.createQuery(criteriaQuery).getResultList();
//sql查询对象
TypedQuery<BookInfo> createQuery = entityManager.createQuery(criteriaQuery);
//设置分页参数
createQuery.setFirstResult(index*pageSize);
createQuery.setMaxResults(pageSize);
//返回查询的分页结果,createQuery.getResultList()为分页查询的结果对象,counts.size()为设置分页参数之前查询的总数
return new PageImpl<BookInfo>(createQuery.getResultList(), page, counts.size());
}
}

spring data jpa条件分组查询及分页的更多相关文章

  1. 【Spring Data 系列学习】Spring Data JPA @Query 注解查询

    [Spring Data 系列学习]Spring Data JPA @Query 注解查询 前面的章节讲述了 Spring Data Jpa 通过声明式对数据库进行操作,上手速度快简单易操作.但同时 ...

  2. Spring data JPA 理解(默认查询 自定义查询 分页查询)及no session 三种处理方法

    简介:Spring Data JPA 其实就是JDK方式(还有一种cglib的方式需要Class)的动态代理 (需要一个接口 有一大堆接口最上边的是Repository接口来自org.springfr ...

  3. Spring Data JPA 条件查询的关键字

    Spring Data JPA 为此提供了一些表达条件查询的关键字,大致如下: And --- 等价于 SQL 中的 and 关键字,比如 findByUsernameAndPassword(Stri ...

  4. Spring data jpa 复杂动态查询方式总结

    一.Spring data jpa 简介 首先我并不推荐使用jpa作为ORM框架,毕竟对于负责查询的时候还是不太灵活,还是建议使用mybatis,自己写sql比较好.但是如果公司用这个就没办法了,可以 ...

  5. spring data jpa 多对多查询

    package com.ytkj.dao; import com.ytkj.entity.Customer; import com.ytkj.entity.Role; import org.sprin ...

  6. spring data jpa 一对多查询

    在一对多关系中,我们习惯把一的一方称之为主表,把多的一方称之为从表.在数据库中建立一对多的关系,需要使用数据库的外键约束. 什么是外键? 指的是从表中有一列,取值参照主表的主键,这一列就是外键. pa ...

  7. Spring Data JPA应用 之查询分析

    在Spring Data JPA应用之常规CRUD操作初体验 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)尾附上了JpaRepository接口继承关系及方法,可以知道JpaRepos ...

  8. 记: Spring Data Jpa @OneToMany 级联查询被动触发的问题

    I have encountered a bug in using Spring Data Jpa. Specifically,when @OneToMany was used to maintain ...

  9. Spring Data Jpa (三)定义查询方法

    本章详细讲解如何利用方法名定义查询方法(Defining Query Methods) (1)定义查询方法的配置方法 由于Spring JPA Repository的实现原理是采用动态代理的机制,所以 ...

随机推荐

  1. Django 2.0 学习(15):Web框架

    Web框架的本质 对于学习Python的同学,相信对Flask.Django.Web.py等不会陌生,这些都是Python语言的web框架.那么问题来了,web服务器是什么?它和web框架有什么关系? ...

  2. java垃圾回收 - 为什么要进行垃圾回收

    1.为什么要进行垃圾回收:      在C++中,对象所占的内存在程序结束运行之前一直被占用,在明确释放之前不能分配给其它对象:而在Java中,当没有对象引用指向原先分配给某个对象 的内存时,该内存便 ...

  3. 🔺Count on a tree SPOJ - COT (无能为力。。。)

    https://cn.vjudge.net/problem/SPOJ-COT 插上 大佬的代码 和 我的...以后再看吧... Count on a tree 大佬:http://www.cnblog ...

  4. Hbase(二)hbase建表

    一.建表高级属性 下面几个 shell 命令在 hbase 操作中可以起到很到的作用,且主要体现在建表的过程中,看 下面几个 create 属性 1.bloomfilter 布隆过滤器 默认是 NON ...

  5. 【字符串】manacher算法

    Definition 定义一个回文串为从字符串两侧向中心扫描时,左右指针指向得字符始终相同的字符串. 使用manacher算法可以在线性时间内求解出一个字符串的最长回文子串. Solution 考虑回 ...

  6. 驱动之NandFlash的介绍与应用20170209

    本文主要介绍的是NAND FLASH的介绍与应用,直接看个人笔记即可:

  7. 驱动之SPI,UART,I2C的介绍与应用20170118

    这篇文章主要介绍基本的驱动也是用的最多的协议类驱动中的SPI,I2C和UART.首先从最简单的UART也就是串口讲起: 1.UART UART由两根线也就是TX,RX以及波特率产生器组成,操作比较简单 ...

  8. oracle 数据库记录

    /*----------------------------------------------------------------------------*/ 问题1[--------] Selec ...

  9. php使用时间戳保存时间的意义

    时间戳记录的是格林尼治时间,使用date格式化的时候会根据你程序设置的不同时区显示不同的时间. 如果使用具体时间,则还需要进行多一步转换.

  10. lightoj 1010 (水题,找规律)

    lightoj 1010 Knights in Chessboard 链接:http://lightoj.com/volume_showproblem.php?problem=1010 题意:国际象棋 ...