我用的是spring-boot+spring-data(hibernate-jpa),如果用jpa默认的findAll分页方法是没问题的,直接在方法上加入缓存注解即可。
@Cacheable(cacheNames = "xqblog")
Page<XqBlog> findAll(Pageable pageable);但是现在我还有一个方法是包含条件查询的分页,具体代码如下:Page<XqBlog> objPage = xqBlogDao.findAll(new Specification<XqBlog>()
{
@Override
public Predicate toPredicate(Root<XqBlog> root,
CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder)
{
List<Predicate> lstPredicates = new ArrayList<Predicate>();

String typeStr = typeId;
if(StringUtils.isNotBlank(typeStr))
{
request.getSession().setAttribute("nowTypeId", typeStr);
} Object nowTypeIdObj = request.getSession().getAttribute("nowTypeId");
if(nowTypeIdObj != null)
{
typeStr = nowTypeIdObj+"";
}

// 是否根据文章标题进行条件查询
if (StringUtils.isNotBlank(search) && !search.equals("null") && !search.equals("undefined"))
{
request.setAttribute("search", search);
request.getSession().setAttribute("search", search);
lstPredicates.add(criteriaBuilder.like(root.get("title").as(String.class), "%" + search + "%"));
}
else
{
request.getSession().setAttribute("search", null);
}

if (typeStr!=null && !typeStr.equals("0"))
{
request.getSession().setAttribute("nowTypeId", typeStr);

//级联查询:使用root对象的join制定要关联的对象实体
Join<XqBlog, XqBlogType> join = root.join("blogType");
            Path<String> exp3 = join.get("typeId"); 
            lstPredicates.add(criteriaBuilder.equal(exp3, typeStr));
}
else
{
request.getSession().setAttribute("nowTypeId", 0);
}
Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
}
}, pageable);这时候缓存注解是没法直接放到内部方法的xqBlogDao.findAll()上的,请问这种情况下怎样实现缓存查询?谢谢!