Spring datajpa example condition paging query instance

Keywords: Spring Database Hibernate

The article refers to: https://blog.csdn.net/long476964/article/details/79677526   . This article introduces the use of the Example class.

Spring datajpa official document: https://docs.spring.io/spring-data/jpa/docs/2.0.9.RELEASE/reference/html/#query-by-example (English tycoon moves here)

spring data jpa Example condition paging query instance

 public Result  listPageClaimAccount( String userId ,int pageNo ,int pageSize ,ClaimAccount claimAccount ){
        if ( pageNo <=0 ) pageNo=1;
        if ( pageSize <=0 ) pageSize=1;
        pageNo-- ; //page parameter of JPA starts from 0
        try {
           
            Pageable pageable = PageRequest.of(pageNo, pageSize, new Sort(Sort.Direction.DESC, "id") );
            Example<ClaimAccount>  example = Example.of( claimAccount );
            Page<ClaimAccount>  pageResult = claimAccountRepository.findAll( example, pageable );
            return Result.success( pageResult );
        }catch ( Exception e ){
            e.printStackTrace();
            log.error("Database operation error",e);

            return Result.fail("Database operation error");
        }
    }

 

The JpaRepository abstract interface inherits a large number of interfaces. What is actually called in the above code is

findAll method defined in org.springframework.data.repository.query.QueryByExampleExecutor interface

	/**
	 * Returns a {@link Page} of entities matching the given {@link Example}. In case no match could be found, an empty
	 * {@link Page} is returned.
	 *
	 * @param example must not be {@literal null}.
	 * @param pageable can be {@literal null}.
	 * @return a {@link Page} of entities matching the given {@link Example}.
	 */
	<S extends T> Page<S> findAll(Example<S> example, Pageable pageable);

org.springframework.data.repository.query.QueryByExampleExecutor

/**
 * Interface to allow execution of Query by Example {@link Example} instances.
 *
 * @param <T>
 * @author Mark Paluch
 * @author Christoph Strobl
 * @since 1.12
 */
public interface QueryByExampleExecutor<T> {

The of(T probe) method of org.springframework.data.domain.Example uses the equal comparator by default, and takes the non empty property of the parameter as the equal judgment parameter of the where part

/**
 * Support for query by example (QBE). An {@link Example} takes a {@code probe} to define the example. Matching options
 * and type safety can be tuned using {@link ExampleMatcher}.
 *
 * @author Christoph Strobl
 * @author Mark Paluch
 * @author Oliver Gierke
 * @param <T> the type of the probe.
 * @since 1.12
 */
public interface Example<T> {

	/**
	 * Create a new {@link Example} including all non-null properties by default.
	 *
	 * @param probe must not be {@literal null}.
	 * @return
	 */
	static <T> Example<T> of(T probe) {
		return new TypedExample<>(probe, ExampleMatcher.matching());
	}

Console output after code run (info level)

Hibernate: 
    select
        claimaccou0_.id as id1_0_,
        claimaccou0_.case_service_man_phone as case_ser2_0_,
        claimaccou0_.create_by as create_b3_0_,
        claimaccou0_.create_date as create_d4_0_,
        claimaccou0_.insurance_company as insuranc5_0_,
        claimaccou0_.insurance_date as insuranc6_0_,
        claimaccou0_.insurance_man as insuranc7_0_,
        claimaccou0_.insurance_man_phone as insuranc8_0_,
        claimaccou0_.insurance_reason as insuranc9_0_,
        claimaccou0_.insurance_type as insuran10_0_,
        claimaccou0_.loss_amount as loss_am11_0_,
        claimaccou0_.loss_institution as loss_in12_0_,
        claimaccou0_.pay_amount as pay_amo13_0_,
        claimaccou0_.pay_date as pay_dat14_0_,
        claimaccou0_.pay_progress as pay_pro15_0_,
        claimaccou0_.remark as remark16_0_,
        claimaccou0_.report_date as report_17_0_,
        claimaccou0_.report_no as report_18_0_,
        claimaccou0_.update_by as update_19_0_,
        claimaccou0_.update_date as update_20_0_ 
    from
        a_claim_account claimaccou0_ 
    where
        claimaccou0_.insurance_company=? 
        and claimaccou0_.loss_institution=? 
        and claimaccou0_.insurance_type=? 
    order by
        claimaccou0_.id desc limit ?
Hibernate: 
    select
        count(claimaccou0_.id) as col_0_0_ 
    from
        a_claim_account claimaccou0_ 
    where
        claimaccou0_.insurance_company=? 
        and claimaccou0_.loss_institution=? 
        and claimaccou0_.insurance_type=?

 

 

 

 

 

Posted by robertaccettura on Wed, 01 Jan 2020 03:45:28 -0800