ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Specifications动态查询

2020-02-23 12:00:12  阅读:392  来源: 互联网

标签:Specifications cb Specification 查询 filed 动态 root spec


有时我们在查询某个实体的时候,给定的条件是不固定的,这时就需要动态构建相应的查询语句,在Spring Data JPA中可以通过JpaSpecificationExecutor接口查询。相比JPQL,其优势是类型安全,更加的面向对象。

 

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

/**
 *    JpaSpecificationExecutor中定义的方法
 **/
 public interface JpaSpecificationExecutor<T> {
       //根据条件查询一个对象
     T findOne(Specification<T> spec);    
       //根据条件查询集合
     List<T> findAll(Specification<T> spec);
       //根据条件分页查询
     Page<T> findAll(Specification<T> spec, Pageable pageable);
       //排序查询查询
     List<T> findAll(Specification<T> spec, Sort sort);
       //统计查询
     long count(Specification<T> spec);
}

 

对于JpaSpecificationExecutor,这个接口基本是围绕着Specification接口来定义的。我们可以简单的理解为,Specification构造的就是查询条件。

 

Specification接口中只定义了如下一个方法:

 

//构造查询条件
    /** 
    *    root    :Root接口,代表查询的根对象,可以通过root获取实体中的属性
    *    query    :代表一个顶层查询对象,用来自定义查询
    *    cb        :用来构建查询,此对象里有很多条件方法
    **/
    public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb);

 

使用Specifications完成条件查询

//依赖注入customerDao
    @Autowired
    private CustomerDao customerDao;    
    @Test
    public void testSpecifications() {
          //使用匿名内部类的方式,创建一个Specification的实现类,并实现toPredicate方法
        Specification <Customer> spec = new Specification<Customer>() {
            public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //cb:构建查询,添加查询方式   like:模糊匹配
                //root:从实体Customer对象中按照custName属性进行查询
                return cb.like(root.get("Name").as(String.class), "上海堡%");
            }
        };
        Customer customer = customerDao.findOne(spec);
        System.out.println(customer);
    }

基于Specifications的分页查询

 

 @Test
    public void testPage() {
        //构造查询条件
        Specification<Customer> spec = new Specification<Customer>() {
            public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                return cb.like(root.get("Name").as(String.class), "上海堡%");
            }
        };
/**
 * 构造分页参数
 * Pageable : 接口
 * PageRequest实现了Pageable接口,调用构造方法的形式构造
 * 第一个参数:页码(从0开始)
 * 第二个参数:每页查询条数
 */
 Pageable pageable = new PageRequest(0, 5);
        
 /**
 * 分页查询,封装为Spring Data Jpa 内部的page bean
 * 此重载的findAll方法为分页方法需要两个参数
 * 第一个参数:查询条件Specification
 * 第二个参数:分页参数
 */
 Page<Customer> page = customerDao.findAll(spec,pageable);
        
    }

 

3 方法对应关系

方法名称

Sql对应关系

equle

filed = value

gt(greaterThan )

filed > value

lt(lessThan )

filed < value

ge(greaterThanOrEqualTo )

filed >= value

le( lessThanOrEqualTo)

filed <= value

notEqule

filed != value

like

filed like value

notLike

filed not like value

 

标签:Specifications,cb,Specification,查询,filed,动态,root,spec
来源: https://www.cnblogs.com/yangzhixue/p/12349051.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有