大家知道Hibernate可以很轻松的根据提供条件进行动态筛选查询,那个JPA怎么实现呢,其中最为简单的就是使用Specification实现JPA的动态查询,本人也是初步接触JPA,第一次使用JPA实现动态查询,因为其动态查询参数传递等不是太明白,所以耗费了一点时间。
首先JPA类需要继承JpaSpecificationExecutor类,代码如下:
package com.ax.zs.springbootweb.hj.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.hj.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<PRPSTAXPARAMVALUECONFIG,Long>,JpaSpecificationExecutor<PRPSTAXPARAMVALUECONFIG>
,Serializable {
@Cacheable(value = "config")
@Query(value = "select * from prpstaxparamvalueconfig where comparamid=?1 and configcomcode=?2",nativeQuery = true)
public List <PRPSTAXPARAMVALUECONFIG> findByCondition(long comparamid, String comCode);
@Cacheable(value = "upComeCode")
@Query(value = "select t.uppercomcode from prpstaxcompany t where t.comcode=?1",nativeQuery = true)
public List<String> getUpperComCode(String comCode);
}
package com.ax.zs.springbootweb.hj.service;
import com.ax.zs.springbootweb.hj.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<PRPSTAXPARAMVALUECONFIG> getSysConfigByCondition(long comparamid, String comCode);
public Page<PRPSTAXPARAMVALUECONFIG> getSysConfigByConditions(long comparamid, String comCode, Pageable pageable);
}
可以通过Specification类看到其中提供的查询接口
然后在查询的时候选择实现findAll方法即可
impl实现类代码如下:
实现分页:
@Override
public Page<PRPSTAXPARAMVALUECONFIG> getSysConfigByConditions(long comparamid, String configcomcode, Pageable pageable){
Page<PRPSTAXPARAMVALUECONFIG> resultList = null;
Specification querySpecifi = new Specification<PRPSTAXPARAMVALUECONFIG>() {
public Predicate toPredicate(Root<PRPSTAXPARAMVALUECONFIG> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> 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<PrpsTaxRequestLog> findAllByCondition(String requestapp, String requestappname, Date startDate , Date endDate){
List<PrpsTaxRequestLog> resultList = null;
Specification querySpecifi = new Specification<PrpsTaxRequestLog>() {
public Predicate toPredicate(Root<PrpsTaxRequestLog> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> 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.<Date>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分页 需要提供分页参数
分页页面数据显示
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!--Created by Hj on 2018-10-24.-->
<head></head>
<body>
<!-- 分页查询Start-->
<div th:fragment="pager">
<div class="text-right" th:with="baseUrl=${#httpServletRequest.getRequestURL().toString()},pars=${#httpServletRequest.getQueryString() eq null ? '' : new String(#httpServletRequest.getQueryString().getBytes('iso8859-1'), 'UTF-8')}">
<ul style="margin:0px;" class="pagination" th:with="newPar=${new java.lang.String(pars eq null ? '' : pars).replace('page='+(datas.number), '')},
curTmpUrl=${baseUrl+'?'+newPar},
curUrl=${curTmpUrl.endsWith('&') ? curTmpUrl.substring(0, curTmpUrl.length()-1):curTmpUrl}">
<!--<li th:text="${pars}"></li>-->
<li><a href="#" th:href="@{${curUrl}(page=0)}">首页</a></li>
<li th:if="${datas.hasPrevious()}"><a href="#" th:href="@{${curUrl}(page=${datas.number-1})}">上一页</a></li>
<!--总页数小于等于10-->
<div th:if="${(datas.totalPages le 10) and (datas.totalPages gt 0)}" th:remove="tag">
<div th:each="pg : ${#numbers.sequence(0, datas.totalPages - 1)}" th:remove="tag">
<span th:if="${pg eq datas.getNumber()}" th:remove="tag"> <li class="active"><span class="current_page line_height" th:text="${pg+1}">${pageNumber}</span></li> </span>
<span th:unless="${pg eq datas.getNumber()}" th:remove="tag"> <li><a href="#" th:href="@{${curUrl}(page=${pg})}" th:text="${pg+1}"></a></li> </span>
</div>
</div>
<!-- 总数数大于10时 -->
<div th:if="${datas.totalPages gt 10}" th:remove="tag">
<li th:if="${datas.number-2 ge 0}"><a href="#" th:href="@{${curUrl}(page=${datas.number}-2)}" th:text="${datas.number-1}"></a></li>
<li th:if="${datas.number-1 ge 0}"><a href="#" th:href="@{${curUrl}(page=${datas.number}-1)}" th:text="${datas.number}"></a></li>
<li class="active"><span class="current_page line_height" th:text="${datas.number+1}"></span></li>
<li th:if="${datas.number+1 lt datas.totalPages}"><a href="#" th:href="@{${curUrl}(page=${datas.number}+1)}" th:text="${datas.number+2}"></a></li>
<li th:if="${datas.number+2 lt datas.totalPages}"><a href="#" th:href="@{${curUrl}(page=${datas.number}+2)}" th:text="${datas.number+3}"></a></li>
</div>
<li th:if="${datas.hasNext()}"><a href="#" th:href="@{${curUrl}(page=${datas.number+1})}">下一页</a></li>
<!--<li><a href="#" th:href="@{${curUrl}(page=${datas.totalPages-1})}">尾页</a></li>-->
<li><a href="#" th:href="${datas.totalPages le 0 ? curUrl+'page=0':curUrl+'&page='+(datas.totalPages-1)}">尾页</a></li>
<li><span th:utext="'共'+${datas.totalPages}+'页 / '+${datas.totalElements}+' 条'"></span></li>
</ul>
</div>
</div>
<!-- 分页查询end-->
<div th:fragment="pagers">
<div class="text-right" style="height: 34px">
</div>
</div>
</body>
</html>