Spring Data Jpa-动态查询条件

    /**
     * 
     * 查看日志列表-按照时间倒序排列
     * 
     * @author: wyc
     * @createTime: 2017年4月20日 下午4:24:43
     * @history:
     * @return List<SecurityAuditLog>
     */
    public Page<SecurityAuditLog> pageLogList(final SecurityAuditLog log,int page,int size){
        Sort s=new Sort(Sort.Direction.DESC, "operateTime");
        PageRequest p=new PageRequest(page-1, size, s);
        
         Specification querySpecifi = new Specification<SecurityAuditLog>() {
                @Override
                public Predicate toPredicate(Root<SecurityAuditLog> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                    List<Predicate> predicates = new ArrayList<>();
                    if(null != log.getLoginAccount()&&!"".equals(log.getLoginAccount())){
                        predicates.add(criteriaBuilder.like(root.<String>get("loginAccount"), "%"+log.getLoginAccount()+"%"));
                    }
                    
                    if(null != log.getQuerySign()&&!"".equals(log.getQuerySign())){
                        predicates.add(criteriaBuilder.like(root.<String>get("querySign"), "%"+log.getQuerySign()+"%"));
                    }
                    
                    if(null != log.getQueryType()&&!"".equals(log.getQueryType())){
                        predicates.add(criteriaBuilder.equal(root.<String>get("queryType"), log.getQueryType()));
                    }
                    
                    if(null != log.getOperatePlatform()&&!"".equals(log.getOperatePlatform())){
                        predicates.add(criteriaBuilder.equal(root.<String>get("operatePlatform"), log.getOperatePlatform()));
                    }
                    
                    if(null != log.getOperateModule()&&!"".equals(log.getOperateModule())){
                        predicates.add(criteriaBuilder.equal(root.<String>get("operateModule"), log.getOperateModule()));
                    }
                    
                    
                    return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            };
        Page<SecurityAuditLog> list= securityAuditLogDao.findAll(querySpecifi,p);
        return list;
    }

 

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;public interface ISecurityAuditLogDao extends PagingAndSortingRepository<SecurityAuditLog, String>,JpaSpecificationExecutor<SecurityAuditLog>{

}

 

posted @ 2017-07-14 16:49  伍叶春  阅读(1565)  评论(0编辑  收藏  举报