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.Brand; import org.kee.ssf.repository.BrandDao; 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 BrandService { private BrandDao brandDao; public Page<Brand> get(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType) { PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType); Specification<Brand> spec = buildSpecification(searchParams); return brandDao.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<Brand> buildSpecification(Map<String, Object> searchParams) { Map<String, SearchFilter> filters = SearchFilter.parse(searchParams); Specification<Brand> spec = DynamicSpecifications.bySearchFilter(filters.values(), Brand.class); return spec; } @Autowired public void setBrandDao(BrandDao brandDao) { this.brandDao = brandDao; } }