spring data jpa条件分组查询及分页
原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条件分组查询及分页的更多相关文章
- 【Spring Data 系列学习】Spring Data JPA @Query 注解查询
[Spring Data 系列学习]Spring Data JPA @Query 注解查询 前面的章节讲述了 Spring Data Jpa 通过声明式对数据库进行操作,上手速度快简单易操作.但同时 ...
- Spring data JPA 理解(默认查询 自定义查询 分页查询)及no session 三种处理方法
简介:Spring Data JPA 其实就是JDK方式(还有一种cglib的方式需要Class)的动态代理 (需要一个接口 有一大堆接口最上边的是Repository接口来自org.springfr ...
- Spring Data JPA 条件查询的关键字
Spring Data JPA 为此提供了一些表达条件查询的关键字,大致如下: And --- 等价于 SQL 中的 and 关键字,比如 findByUsernameAndPassword(Stri ...
- Spring data jpa 复杂动态查询方式总结
一.Spring data jpa 简介 首先我并不推荐使用jpa作为ORM框架,毕竟对于负责查询的时候还是不太灵活,还是建议使用mybatis,自己写sql比较好.但是如果公司用这个就没办法了,可以 ...
- spring data jpa 多对多查询
package com.ytkj.dao; import com.ytkj.entity.Customer; import com.ytkj.entity.Role; import org.sprin ...
- spring data jpa 一对多查询
在一对多关系中,我们习惯把一的一方称之为主表,把多的一方称之为从表.在数据库中建立一对多的关系,需要使用数据库的外键约束. 什么是外键? 指的是从表中有一列,取值参照主表的主键,这一列就是外键. pa ...
- Spring Data JPA应用 之查询分析
在Spring Data JPA应用之常规CRUD操作初体验 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)尾附上了JpaRepository接口继承关系及方法,可以知道JpaRepos ...
- 记: Spring Data Jpa @OneToMany 级联查询被动触发的问题
I have encountered a bug in using Spring Data Jpa. Specifically,when @OneToMany was used to maintain ...
- Spring Data Jpa (三)定义查询方法
本章详细讲解如何利用方法名定义查询方法(Defining Query Methods) (1)定义查询方法的配置方法 由于Spring JPA Repository的实现原理是采用动态代理的机制,所以 ...
随机推荐
- 【JavaScript】20款漂亮的css字体
样式一: body { margin: 0; padding: 0; line-height: 1.5em; font-family: "Times New Roman", Tim ...
- vue使用过程中的一些小技巧
这些也是自己平时项目中遇到过的一些问题,看到有人整理了出来,也就转载保存一下 文章内容总结: 组件style的scoped Vue 数组/对象更新 视图不更新 vue filters 过滤器的使用 列 ...
- 聊聊flink的AsyncWaitOperator
序本文主要研究一下flink的AsyncWaitOperator AsyncWaitOperatorflink-streaming-java_2.11-1.7.0-sources.jar!/org/a ...
- Splay 的区间操作
学完Splay的查找作用,发现和普通的二叉查找树没什么区别,只是用了splay操作节省了时间开支. 而Splay序列之王的称号可不是白给的. Splay真正强大的地方是他的区间操作. 怎么实现呢? 我 ...
- 十五分钟介绍 Redis数据结构--学习笔记
下面是一个对Redis官方文档<A fifteen minute introduction to Redis data types>一文的翻译,如其题目所言,此文目的在于让一个初学者能通过 ...
- 【loj2639】[Tjoi2017]不勤劳的图书管理员
#2639. 「TJOI2017」不勤劳的图书管理员 题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产 ...
- python基础----多态与多态性、super函数用法、继承原理
一.多态与多态性 ㈠多态: 多态指的是一类事物有多种形态, ...
- 安装黑苹果的config.plist
前提条件:有mac真机.目前在测试虚拟机可行性 第一步:制作U盘启动盘 1.在 app store 下载 mac OS sierra 镜像 2.格式化 U 盘,gpt 格式 3.执行以下命令(具体名称 ...
- python访问需要登录的网页
有些网页需要你登录之后才可以访问,你需要提供账户和密码. 只要在发送http请求时,带上含有正常登陆的cookie就可以了. 1.首先我们要先了解cookie的工作原理. Cookie是由服务器端生成 ...
- docker-api
__author__ = 'zxp' import docker import sys class DockerManager_Slave(object): def __init__(self): s ...