jpa之org.springframework.data.jpa.domain.Specification

 

jpa之org.springframework.data.jpa.domain.Specification

 

 

Specification

interface TaskRepository extends JpaRepository<Task, Integer>, JpaSpecificationExecutor<Task> 
/**
org.springframework.data.jpa.domain.Specification
    Predicate toPredicate(Root<T> var1, CriteriaQuery<?> var2, CriteriaBuilder var3);
        #Root : 实体类引用, 获取任何实体的属性
        #CriteriaQuery: 顶层查询条件,自定义查询 (where)(order by)  过滤掉关键字:from, where, order by等
        #CriteriaBuilder: 底层查询条件>构建查询条件 (like,equal,and,in,greaterThanOrEqualTo,lessThanOrEqualTo)  构建条件查询的语句

criteria 规则
criteria query 查询规则
criteria builder 构建起规则
restriction  限制
**/

Specification五个核心方法

    Optional<T> findOne(@Nullable Specification<T> var1);

    List<T> findAll(@Nullable Specification<T> var1);

    Page<T> findAll(@Nullable Specification<T> var1, Pageable var2);

    List<T> findAll(@Nullable Specification<T> var1, Sort var2);

    long count(@Nullable Specification<T> var1);

 

无lambda表达式

@Test
    public void test4(){
        Specification<Task> spec = new Specification<>() {
            @Override
            public Predicate toPredicate(Root<Task> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                Predicate p1 = builder.equal(root.get("status"), TaskStatusEnum.CREATED);
                return query.where(p1).getRestriction();
            }
        };
        this.taskRepository.findAll(spec);
    }

有lambda表达式

@Test
    public void test4(){
        Specification<Task> spec = (root, query, builder) -> {
            Predicate p1 = builder.equal(root.get("status"), TaskStatusEnum.CREATED);
            return query.where(p1).getRestriction();
        };
        this.taskRepository.findAll(spec);
    }

分页查询

@Transactional
    @Test
    public void test5(){
        Specification<Task> spec = (root, query, builder) -> query.where().getRestriction();
        Page<Task> pageResult = this.taskRepository.findAll(spec, PageRequest.of(0, 10));
        pageResult.getContent().forEach(System.out::println);
        System.out.println("总页数:" + pageResult.getTotalPages());
        System.out.println("页面记录数:" + pageResult.getSize());
        System.out.println("当前页:" + pageResult.getNumber() + 1);
        System.out.println("总记录数:" + pageResult.getTotalElements());
    }

简单order by

@Test
    public void test6(){
        Specification<Task> spec = (root, query, builder) -> {
            root.fetch("owner");    //inner join user
            root.fetch("creator");  //inner join user
            root.fetch("model");    //inner join model
            root.fetch("client");   //inner join client

            Predicate p1 = builder.equal(root.get("name"), "上海福新");   //name=?
            Predicate p2 = builder.in(root.get("type")).value(TaskTypeEnum.BLACKOUT_APPLICATION).value(TaskTypeEnum.HIGH_USER_CHECK); //type in (?,?)
            Predicate p3 = builder.like(root.get("name"), "%" + "上海福新" + "%"); //name like %上海福新%

            return query.where(p1, p2, p3).getRestriction();
        };
        this.taskRepository.findAll(spec, Sort.by("name").descending()); //简单的order by
    }

复杂order by

@Test
    public void test7(){
        Specification<Task> spec = (root, query, builder) -> {

            root.fetch("owner");    //inner join user
            root.fetch("creator");  //inner join user
            root.fetch("model");    //inner join model
            root.fetch("client");   //inner join client

            //复杂自定义排序
            List<Order> orders = new ArrayList<>();
            orders.add(builder.asc(
                    builder.selectCase()
                            .when(builder.equal(root.get("status").as(TaskStatusEnum.class), TaskStatusEnum.CREATED), 1)
                            .when(builder.equal(root.get("status").as(TaskStatusEnum.class), TaskStatusEnum.FINISHED), 2)
                            .otherwise(3)
            ));
            query.orderBy(orders);


            Predicate p1 = builder.equal(root.get("name"), "上海福新");   //name=?
            Predicate p2 = builder.in(root.get("type")).value(TaskTypeEnum.BLACKOUT_APPLICATION).value(TaskTypeEnum.HIGH_USER_CHECK); //type in (?,?)
            Predicate p3 = builder.like(root.get("name"), "%" + "上海福新" + "%"); //name like %上海福新%

            return query.where(p1, p2, p3).getRestriction();
        };
        //this.taskRepository.findAll(spec, Sort.by("name").descending()); //简单的order by  query的排序和外面的排序同时存在时, 外面的会覆盖里面的排序
        this.taskRepository.findAll(spec);
    }

 

上一篇:预定义的函数式接口 Predicate


下一篇:15-5 Gateway核心之Predicate讲解及自定义演示