zoukankan      html  css  js  c++  java
  • 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>{
    
    }
  • 相关阅读:
    [组合][DP]luogu P3643 [APIO2016]划艇
    [倍增]luogu P4155 [SCOI2015]国旗计划
    [并查集][线段树]luogu P3273 [SCOI2011]棘手的操作
    pytest警告DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
    docker-compose5分钟搭建wordpress博客网站
    Docker安装入门
    Windows10安装wget命令
    CRC (Cyclic Redundancy Check)
    Linux学习笔记
    Linux学习笔记
  • 原文地址:https://www.cnblogs.com/wuyechun/p/7171312.html
Copyright © 2011-2022 走看看