spring data jpa Specification动态查询
package com.ytkj.entity; import javax.persistence.*;
import java.io.Serializable; /**
* @Entity
* 作用:指定当前类是实体类。
* @Table
* 作用:指定实体类和表之间的对应关系。
* 属性:
* name:指定数据库表的名称
* @Id
* 作用:指定当前字段是主键。
* @GeneratedValue
* 作用:指定主键的生成方式。。
* 属性:
* strategy :指定主键生成策略。
* @Column
* 作用:指定实体类属性和数据库表之间的对应关系
* 属性:
* name:指定数据库表的列名称。
* unique:是否唯一
* nullable:是否可以为空
* inserttable:是否可以插入
* updateable:是否可以更新
* columnDefinition: 定义建表时创建此列的DDL
* secondaryTable: 从表名。如果此列不建在主表上(默认建在主表),该属性定义该列所在从表的名字搭建开发环境[重点]
*
* 客户实体类
* 配置映射关系
* 实体类和表映射
* 实体类属性和表字段映射
*/
@Entity
@Table(name = "cst_customer")
public class Customer implements Serializable {
/**
* 声明主键配置
*/
@Id
/**
* 配置主键的生成策略
*/
@GeneratedValue(strategy = GenerationType.IDENTITY)
/**
* 指定实体类属性和数据库表之间的对应关系
*/
@Column(name ="cust_id")
private Long custId;//客户主键
@Column(name = "cust_name")
private String custName;//客户名称
@Column(name ="cust_source" )
private String custSource;//客户来源
@Column(name = "cust_industry")
private String custIndustry;//客户行业
@Column(name ="cust_level")
private String custLevel;//客户级别
@Column(name ="cust_address")
private String custAddress;//客户地址
@Column(name = "cust_phone")
private String custPhone;//客户电话 public Long getCustId() {
return custId;
} public void setCustId(Long custId) {
this.custId = custId;
} public String getCustName() {
return custName;
} public void setCustName(String custName) {
this.custName = custName;
} public String getCustSource() {
return custSource;
} public void setCustSource(String custSource) {
this.custSource = custSource;
} public String getCustIndustry() {
return custIndustry;
} public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
} public String getCustLevel() {
return custLevel;
} public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
} public String getCustAddress() {
return custAddress;
} public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
} public String getCustPhone() {
return custPhone;
} public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
} @Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custSource='" + custSource + '\'' +
", custIndustry='" + custIndustry + '\'' +
", custLevel='" + custLevel + '\'' +
", custAddress='" + custAddress + '\'' +
", custPhone='" + custPhone + '\'' +
'}';
}
}
Specifications动态查询
JpaSpecificationExecutor 方法列表
T findOne(Specification<T> spec); //查询单个对象
List<T> findAll(Specification<T> spec); //查询列表
//查询全部,分页
//pageable:分页参数
//返回值:分页pageBean(page:是springdatajpa提供的)
Page<T> findAll(Specification<T> spec, Pageable pageable);
//查询列表
//Sort:排序参数
List<T> findAll(Specification<T> spec, Sort sort);
long count(Specification<T> spec);//统计查询
* Specification :查询条件
自定义我们自己的Specification实现类
实现
//root:查询的根对象(查询的任何属性都可以从根对象中获取)
//CriteriaQuery:顶层查询对象,自定义查询方式(了解:一般不用)
//CriteriaBuilder:查询的构造器,封装了很多的查询条件
Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb); //封装查询条件
Demo
/**
* JpaRepository<实体类类型,主键类型>:用来完成基本CRUD操作
* JpaSpecificationExecutor<实体类类型>:用于复杂查询(分页等查询操作)
*/
public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> { }
import com.ytkj.dao.CustomerDao;
import com.ytkj.entity.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.persistence.criteria.*;
import java.util.List; /**
* Specifications动态查询
*/
@RunWith(SpringJUnit4ClassRunner.class)//声明spring提供的单元测试环境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息
public class SpringdatajpaSpecification { @Autowired
CustomerDao customerDao; /**
*
* 根据单个条件查询
*/
@Test
public void test(){
//匿名内部类
/**
* 自定义查询条件
* 1:实现Specification接口(提供泛型,查询对象的类型)
* 2.实现Specification接口中的toPredicate方法(构造查询条件)
* 3.根据方法中的两个参数:
* root:获取需要查询对象的属性名称
* criteriaBuilder:构造查询条件,内部封装了很多查询条件
* 根据名称查询
*
*/
Specification<Customer> specification=new Specification<Customer>() {
@Override
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
//1.获取查询属性名称
Path<Object> custName = root.get("custName");
//2.构造查询条件 select * from cst_customer where cust_name='者超超'
/**
* 第一个参数:需要比较的属性
* 第二个参数:当前需要比较的取值
*/
Predicate predicate = criteriaBuilder.equal(custName, "者超超");//精准匹配
return predicate;
}
};
Customer customer = customerDao.findOne(specification);
System.out.println(customer);
} /**
*
* 根据多个条件查询
*/
@Test
public void test2(){
//匿名内部类
/**
* 自定义查询条件
* 1:实现Specification接口(提供泛型,查询对象的类型)
* 2.实现Specification接口中的toPredicate方法(构造查询条件)
* 3.根据方法中的两个参数:
* root:获取需要查询对象的属性名称
* criteriaBuilder:构造查询条件,内部封装了很多查询条件
* 根据名称和地址查询
*
*/
Specification<Customer> specification=new Specification<Customer>() {
@Override
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
//1.获取查询属性名称
Path<Object> custName = root.get("custName");
Path<Object> custAddress = root.get("custAddress");
//2.构造查询条件
/**
* 第一个参数:path对象需要比较的属性
* 第二个参数:当前需要比较的取值
*/
Predicate predicate = criteriaBuilder.equal(custName, "者超超");//精准匹配
Predicate predicate2 = criteriaBuilder.equal(custAddress, "昆明");//精准匹配
//3.将多个查询条件组合起来:组合(根据自己的业务而定)比如:满足条件一并且满足条件二,满足条件一或者满足条件二
Predicate and = criteriaBuilder.and(predicate, predicate2);
return and;
}
};
Customer customer = customerDao.findOne(specification);
System.out.println(customer);
} /**
* 更具电话号码模糊查询
* equal :直接的到path对象(属性),然后进行比较即可
* gt,lt,ge,le,like : 得到path对象,根据path指定比较的参数类型,再去进行比较
* 指定参数类型:path.as(类型的字节码对象)
*/
@Test
public void test3(){
Specification<Customer> specification=new Specification<Customer>() {
@Override
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
//1.获取查询对象的属性
Path<Object> custPhone = root.get("custPhone");
//2.拼接查询条件
Predicate like = criteriaBuilder.like(custPhone.as(String.class), "18%");
return like;
}
};
List<Customer> list = customerDao.findAll(specification);
for (Customer customer : list) {
System.out.println(customer);
} } /**
* 排序查询
* 更具电话号码模糊 降序查询
* equal :直接的到path对象(属性),然后进行比较即可
* gt,lt,ge,le,like : 得到path对象,根据path指定比较的参数类型,再去进行比较
* 指定参数类型:path.as(类型的字节码对象)
*/
@Test
public void test4(){
Specification<Customer> specification=new Specification<Customer>() {
@Override
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
//1.获取查询对象的属性
Path<Object> custPhone = root.get("custPhone");
//2.拼接查询条件
Predicate like = criteriaBuilder.like(custPhone.as(String.class), "18%");
return like;
}
};
//添加排序
//创建排序对象,需要调用构造方法实例化sort对象
//第一个参数:排序的顺序(倒序,正序)
// Sort.Direction.DESC:倒序
// Sort.Direction.ASC : 升序
//第二个参数:排序的属性名称
Sort sort=new Sort(Sort.Direction.DESC,"custId");
List<Customer> list = customerDao.findAll(specification, sort);
for (Customer customer : list) {
System.out.println(customer);
}
} /**
* 根据id分页查询
* Specification: 查询条件
* Pageable:分页参数
* 分页参数:查询的页码,每页查询的条数
* findAll(Specification,Pageable):带有条件的分页
* findAll(Pageable):没有条件的分页
* 返回:Page(springDataJpa为我们封装好的pageBean对象,数据列表,共条数)
*/
@Test
public void test5(){
//匿名内部类
/**
* 自定义查询条件
* 1:实现Specification接口(提供泛型,查询对象的类型)
* 2.实现Specification接口中的toPredicate方法(构造查询条件)
* 3.根据方法中的两个参数:
* root:获取需要查询对象的属性名称
* criteriaBuilder:构造查询条件,内部封装了很多查询条件
* 根据名称查询
*
*/
Specification<Customer> specification=new Specification<Customer>() {
@Override
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
//1.获取查询属性名称
Path<Object> custId = root.get("custId");
//2.构造查询条件 select * from cst_customer where custId>3 limit 0,5
/**
* 第一个参数:需要比较的属性
* 第二个参数:当前需要比较的取值
*/
Predicate predicate = criteriaBuilder.gt(custId.as(Long.class), 3L);//精准匹配
return predicate;
}
}; /**
* public PageRequest(int page, int size) {
* this(page, size, null);
* }
*/
Pageable pageable=new PageRequest(0,2);
//分页查询
Page<Customer> page = customerDao.findAll(specification, pageable);
List<Customer> list = page.getContent();
for (Customer customer : list) {
System.out.println(customer);
}
System.out.println(page.getContent()); //得到数据集合列表
System.out.println(page.getTotalElements());//得到总条数
System.out.println(page.getTotalPages());//得到总页数 } }
spring data jpa Specification动态查询的更多相关文章
- Spring data jpa 复杂动态查询方式总结
一.Spring data jpa 简介 首先我并不推荐使用jpa作为ORM框架,毕竟对于负责查询的时候还是不太灵活,还是建议使用mybatis,自己写sql比较好.但是如果公司用这个就没办法了,可以 ...
- spring data jpa Specification 复杂查询+分页查询
当Repository接口继承了JpaSpecificationExecutor后,我们就可以使用如下接口进行分页查询: /** * Returns a {@link Page} of entitie ...
- spring data jpa hql动态查询案例
目的:根据入参条件不同,动态组装hql里的where语句. 1. 实现代码 public List<WrapStatis> queryStatisCriteriaBuilder(Strin ...
- 【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 多对多查询
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 ...
随机推荐
- C. Trailing Loves (or L'oeufs?) (质因数分解)
C. Trailing Loves (or L'oeufs?) 题目传送门 题意: 求n!在b进制下末尾有多少个0? 思路: 类比与5!在10进制下末尾0的个数是看2和5的个数,那么 原题就是看b进行 ...
- centos6中安装RabbitMQ
一.安装环境步骤需知 第一步 安装erlang环境 第二步 安装RabbitMQ 二.安装erlang环境 1)安装编译环境,和基础依赖包 yum -y install make gcc gcc-c+ ...
- 一网打尽 @ExceptionHandler、HandlerExceptionResolver、@controlleradvice 三兄弟!
把 @ExceptionHandler.HandlerExceptionResolver.@controlleradvice 三兄弟放在一起来写更有比较性.这三个东西都是用来处理异常的,但是它们使用的 ...
- shell 单行多行注释
1. 单行注释 众所周知,# 比如想要注释:echo “ni” # echo "ni" 2. 多行注释: 法一: : << ! 语句1 语句2 语句3 语句4 ! 法 ...
- 应用程序无法正常启动(0xc000007b)。请单击“确定”关闭应用程序
一开始是报缺少dll,随便在电脑里找个同名的dll放下面就报这个错误,网上查的都没有用.后来又找了一个dll,问题就解决了,所以是dll不对造成的.
- loj2542 「PKUWC2018」随机游走 MinMax 容斥+树上高斯消元+状压 DP
题目传送门 https://loj.ac/problem/2542 题解 肯定一眼 MinMax 容斥吧. 然后问题就转化为,给定一个集合 \(S\),问期望情况下多少步可以走到 \(S\) 中的点. ...
- BZOJ3207 花神的嘲讽计划I
Time Limit: 10 Sec Memory Limit: 128 MB Summary 给你一个模式串P,q个询问,对每个询问回答从Pl到Pr是否存在与给定串相同的子串,同时有所有的给定串长度 ...
- 常用生物信息 ID 及转换方法
众多不同的数据库所采用的对 Gene 和 Protein 编号的 ID 也是不同的, 所以在使用不同数据库数据的时候需要进行 ID 转换. 常用数据库 ID ID 示例 ID 来源 ENSG00000 ...
- DOS基础使用专题(强烈推荐)2
DOS下硬件设备的使用与设置 由于电脑的普及和应用的日益深入,为了满足人们的需要,电脑的功能随着它的发展变得越来越强大,硬件设备也越来越多,如从原来的ISA及PCI声卡.调制解调器等到现在的USB硬盘 ...
- linux 文件及目录结构体系
linux 目录的特点: 1). /是所有目录的顶点 2).目录结构像一颗倒挂的树 3).目录和磁盘分区是没有关联的 4)./下不同的目录可能对应不同的分区或磁盘 5).所有的目录都是按照一定的类别有 ...