Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package org.kee.ssf.service.sys; import org.kee.ssf.entity.Resource; import org.kee.ssf.repository.ResourceDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.springside.modules.persistence.DynamicSpecifications; import org.springside.modules.persistence.SearchFilter; import java.util.Map; // Spring Bean. @Component // public?. @Transactional public class ResourceService { private ResourceDao resourceDao; public Page<Resource> get(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType) { PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType); Specification<Resource> spec = buildSpecification(searchParams); return resourceDao.findAll(spec, pageRequest); } /** * . */ private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) { Sort sort = null; if ("auto".equals(sortType)) { sort = new Sort(Sort.Direction.DESC, "id"); } else if ("title".equals(sortType)) { sort = new Sort(Sort.Direction.ASC, "title"); } return new PageRequest(pageNumber - 1, pagzSize, sort); } /** * ???. */ private Specification<Resource> buildSpecification(Map<String, Object> searchParams) { Map<String, SearchFilter> filters = SearchFilter.parse(searchParams); Specification<Resource> spec = DynamicSpecifications.bySearchFilter(filters.values(), Resource.class); return spec; } @Autowired public void setResourceDao(ResourceDao resourceDao) { this.resourceDao = resourceDao; } }