SpringDataJPA Example条件分页查询实例

文章参考了:https://blog.csdn.net/long476964/article/details/79677526  。该文章介绍了 Example 类的用法(中文不好看过来)。

SpringDataJPA官方文档:https://docs.spring.io/spring-data/jpa/docs/2.0.9.RELEASE/reference/html/#query-by-example (英文大佬移步这里)

spring data jpa Example条件分页查询 实例

 public Result  listPageClaimAccount( String userId ,int pageNo ,int pageSize ,ClaimAccount claimAccount ){
        if ( pageNo <=0 ) pageNo=1;
        if ( pageSize <=0 ) pageSize=1;
        pageNo-- ; //JPA的page参数从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("数据库操作出现错误",e);

            return Result.fail("数据库操作出现错误");
        }
    }

JpaRepository 抽象接口继承了一大堆接口。上面代码中实际调用的是 

org.springframework.data.repository.query.QueryByExampleExecutor  接口中定义的 findAll 方法

	/**
	 * 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> {

org.springframework.data.domain.Example 的 of(T probe) 方法默认用的 等于 的比较器,会将参数的非空属性 作为where部分的相等判断参数

/**
 * 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());
	}

代码运行后的控制台输出(info 级别)

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=?

猜你喜欢

转载自blog.csdn.net/u012688704/article/details/82463817