Spring data jpa 条件查询-按时间段查询
@Override
public Page< 泛型> findRecordList ( int couponDetailId, int pageNum, int pageSize, String startTime, String endTime ) {
try {
Sort sort = new Sort ( new Sort. Order ( Sort. Direction. DESC , "createTime" ) ) ;
PageRequest pageRequest = new PageRequest ( pageNum - 1 , pageSize, sort) ;
Specification specification = new Specification ( ) {
@Override
public Predicate toPredicate ( Root root, CriteriaQuery query, CriteriaBuilder cb ) {
Predicate predicate = cb. conjunction ( ) ;
predicate. getExpressions ( ) . add ( cb. equal ( root. get ( "cardId" ) , couponDetailId) ) ;
if ( startTime != null && ! startTime. trim ( ) . equals ( "" ) ) {
predicate. getExpressions ( ) . add ( cb. greaterThanOrEqualTo ( root. get ( "createTime" ) . as ( String. class) , startTime) ) ;
}
if ( endTime != null && ! endTime. trim ( ) . equals ( "" ) ) {
predicate. getExpressions ( ) . add ( cb. lessThanOrEqualTo ( root. get ( "createTime" ) . as ( String. class) , endTime) ) ;
}
return predicate;
}
} ;
Page all = discountCouponRecordDao. findAll ( specification, pageRequest) ;
return all;
}