4、spring 增加pagehelper分页插件

一、增加插件


    com.github.pagehelper
    pagehelper-spring-boot-starter
    1.2.10

二、封装插件

加服务类

package com.ouer.hetao.service.base;

import com.github.pagehelper.PageHelper;
import com.ouer.hetao.dal.mapper.BaseMapper;
import com.ouer.hetao.model.base.BaseEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

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

/**
 * @author hetao
 */
public abstract class AbstractBaseService implements BaseService {
    protected Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    protected BaseMapper mapper;

    @Override
    public T get(ID id) {
        return mapper.get(id);
    }
    @Override
    public int insert(T entity) {
        return mapper.insert(entity);
    }


    @Override
    public int delete(ID id) {
        return mapper.delete(id);
    }

    @Override
    public int update(T entity) {
        return mapper.update(entity);
    }

    @Override
    public List list() {
        return mapper.list(null);
    }

    @Override
    public List list(T entity) {
        return mapper.list(entity);
    }

    public T selectOne(T entity) {
        PageHelper.startPage(0, 1);
        return limitOne(mapper.list(entity));
    }

    protected T limitOne(List list) {
        return CollectionUtils.isNotEmpty(list)?list.get(0):null;
    }

    @Override
    public Page page(T entity, Pageable pageable) {
        Sort sort = pageable.getSort();
        if(sort != null) {
            PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize(), getSort(sort));
        } else {
            PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
        }

        List content = mapper.list(entity);
        Long total = mapper.countList(entity);

        return new PageImpl(content, pageable, total);
    }

    @Override
    public List list(T entity, Pageable pageable) {

        Sort sort = pageable.getSort();
        if(sort != null) {
            PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize(), getSort(sort));
        } else {
            PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
        }

        return mapper.list(entity);
    }

    private String getSort(Sort sort) {
        StringBuilder sb = new StringBuilder();
        for (Sort.Order order : sort) {
            sb.append(order.getProperty()).append(" ").append(order.getDirection().name());
        }
        return sb.toString();

    }
}

对UserMapper封装

@Service
public class UserServiceImpl extends AbstractBaseService implements UserService {

}

三、测试

修改测试代码

    @RequestMapping("/getUser")
    public ResponseObject> getUser() {
        return new ResponseObject<>(userService.list(new User(), PageRequest.of(3,2, Sort.Direction.DESC, "id")));
    }

4、spring 增加pagehelper分页插件_第1张图片

项目传送门

你可能感兴趣的:(spring)