SpringBoot JPA(JpaRepository)动态查询 分页展示

大家知道Hibernate可以很轻松的根据提供条件进行动态筛选查询,那个JPA怎么实现呢,其中最为简单的就是使用Specification实现JPA的动态查询,本人也是初步接触JPA,第一次使用JPA实现动态查询,因为其动态查询参数传递等不是太明白,所以耗费了一点时间。

首先JPA类需要继承JpaSpecificationExecutor类,代码如下:

package com.ax.zs.springbootweb.sinosoft.jpa;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import com.ax.zs.springbootweb.sinosoft.entity.PRPSTAXPARAMVALUECONFIG;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Hj on 2018-08-03.
 */

@CacheConfig(cacheNames = "prpstaxparamvalueconfig")
public interface SysConfigJPA extends JpaRepository,JpaSpecificationExecutor
        ,Serializable {

@Cacheable(value = "config")
@Query(value = "select * from prpstaxparamvalueconfig  where comparamid=?1 and  configcomcode=?2",nativeQuery = true)
public List  findByCondition(long comparamid, String comCode);

    @Cacheable(value = "upComeCode")
    @Query(value = "select t.uppercomcode from prpstaxcompany t where t.comcode=?1",nativeQuery = true)
    public List getUpperComCode(String comCode);

}
package com.ax.zs.springbootweb.sinosoft.service;

import com.ax.zs.springbootweb.sinosoft.entity.PRPSTAXPARAMVALUECONFIG;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * Created by hj on 2018/8/1.
 */
public interface SysConfigService {
    /**
     * 根据传入id查找配置
     * */
    public List getSysConfigByCondition(long comparamid, String comCode);
    public Page getSysConfigByConditions(long comparamid, String comCode, Pageable pageable);
}

 

可以通过Specification类看到其中提供的查询接口

SpringBoot JPA(JpaRepository)动态查询 分页展示_第1张图片

 

然后在查询的时候选择实现findAll方法即可

SpringBoot JPA(JpaRepository)动态查询 分页展示_第2张图片

impl实现类代码如下:

实现分页:

    @Override
    public Page getSysConfigByConditions(long comparamid, String configcomcode, Pageable pageable){

        Page resultList = null;
        Specification querySpecifi = new Specification() {
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List predicates = new ArrayList<>();
                predicates.add(criteriaBuilder.equal(root.get("comparamid"), comparamid));
                if(null != configcomcode){
                    predicates.add(criteriaBuilder.like(root.get("configcomcode"), "%"+configcomcode+"%"));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        resultList =  sysConfigJPA.findAll(querySpecifi,pageable);
        return resultList;
    }

不分页 :

    @Override
    public List findAllByCondition(String requestapp, String requestappname, Date startDate , Date endDate){
        List resultList = null;
        Specification querySpecifi = new Specification() {
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List predicates = new ArrayList<>();
                if(null != requestapp){
                    predicates.add(criteriaBuilder.like(root.get("requestapp"), "%"+requestapp+"%"));
                }
                if(null != requestappname){
                    predicates.add(criteriaBuilder.like(root.get("requestappname"), "%"+requestappname+"%"));
                }
                if(null != startDate){
                    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("requestdate"), startDate));
                }
                if(null != endDate){
                    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("requestdate"), endDate));
                }

                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        resultList =  taxRequestLogJPA.findAll(querySpecifi);
        return resultList;
    }
Controller分页 需要提供分页参数

SpringBoot JPA(JpaRepository)动态查询 分页展示_第3张图片

分页页面数据显示









 

你可能感兴趣的:(java后台,JPA,JPA,动态查询,jpa动态查询分页)