JPA paging multi-condition query and paging linked table query

1. Paging multi-condition query – rewrite the toPredicate method of Specification

Spring Data JPA supports Criteria query of JPA2.0, and the corresponding interface is JpaSpecificationExecutor.

Criteria query: is a type-safe and more object-oriented query.

This interface is basically defined around the Specification interface. The Specification interface only defines the following method:

toPredicate method in Specification class

Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb); 

The Repo layer interface inherits the JpaSpecificationExecutor interface and can use the Specification for dynamic query

Only the use of the toPredicate method in the Specification class is introduced here. For a more general tutorial on Criteria query, see here

Criteria query basic concepts

Criteria queries are based on the concept of a metamodel, which is defined for managed entities of a specific persistence unit, which can be entity classes, embedded classes, or mapped parent classes.

  • Root<T>
    Root is an interface, representing the query object, T represents the returned Entity type

  • CriteriaQuery
    CriteriaQuery interface: Describes the conditions of the query, such as: select, from, where, group by, order by, etc. It is generally not necessary to rewrite toPredicate

  • CriteriaBuilder
    is used to generate query conditions, that is, Predicate objects (returned objects), which can combine multiple query conditions

Statements corresponding to each method in CriteriaBuilder

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

Example

Paging query device to achieve the effect of the following sql statement

SELECT * FROM device WHERE user_id=1 AND (id = 1 OR  id =2);
id userId
1 1
2 1
3 1
Sort sort = new Sort(Sort.Direction.DESC, "id");
Pageable pageable = new PageRequest(pageIndex - 1, pageSize, sort);
Page<ElectronicTagsPO> lTagsPOS = mElectronicTagsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {

            List<Predicate> predicates1 = Lists.newArrayList();
            predicates1.add(criteriaBuilder.equal(root.get("userId"), 1));
            Predicate lPredicate = criteriaBuilder.and(predicates1.toArray(new Predicate[predicates1.size()]));

            List<Predicate> predicates = Lists.newArrayList();
            int assetsIdsNum = ids.length;
            predicates.add(criteriaBuilder.equal(root.get("id"), 1));
            predicates.add(criteriaBuilder.equal(root.get("id"), 2));

            return criteriaBuilder.and(lPredicate,criteriaBuilder.or(predicates.toArray(new Predicate[predicates.size()])));

        }, pageable);

Reference:
Spring-Data-JPA criteria query
jpa multi-condition query overrides the toPredicate method of the Specification

2. Paging table query

This is very simple, that is
1. First single-table paging query, get the paging result Page< T> page
2. Call < S> Page< S> map(Converter< ? super T, ? extends S> var1);

Example:

Page<Entity> page = mEntity.findEntityByUserId(userId, pageable);
Page<EntityResult> lTagsInfoDTOPage = page.map(Entity-> {
            EntityResultl entityResult = new EntityResult();
            //其他的数据库进行查询
            //设置entityResult 的值
            return entityResult ;
        });

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324844187&siteId=291194637