【spring-boot】分页类使用

2021-01-28 22:13

阅读:720

标签:override   not   default   enc   get   实现   list   pack   dem   

引入分页类

pom.xml中

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

公共层 - 分页DTO 

pageDTO.java

package com.example.pagehelperdemo.common;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author komiles@163.com
 * @date 2020-04-30 18:54
 */
@Data
@AllArgsConstructor
public class PageDTO {

    private Long Total;

    private Integer pageSize;

    private Integer pageNum;

    private List list;
}

 

服务层 - 用户服务类

UserService.java

package com.example.pagehelperdemo.service;

import com.example.pagehelperdemo.common.PageDTO;
import com.example.pagehelperdemo.dao.User;

/**
 * @author komiles@163.com
 * @date 2020-04-30 18:49
 */
public interface UserService {

    User getOne(Integer id);

    PageDTO getList(Integer pageNum, Integer pageSize);
}

  UserServiceImpl.java 实现类 

package com.example.pagehelperdemo.service.impl;

import com.example.pagehelperdemo.common.PageDTO;
import com.example.pagehelperdemo.dao.User;
import com.example.pagehelperdemo.dao.UserExample;
import com.example.pagehelperdemo.mapper.UserMapper;
import com.example.pagehelperdemo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author komiles@163.com
 * @date 2020-04-30 18:50
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User getOne(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageDTO getList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        UserExample userExample = new UserExample();
        userExample.createCriteria();

        List userList = userMapper.selectByExample(userExample);

        PageInfo pageInfo = new PageInfo(userList);

        PageDTO result = new PageDTO(
                pageInfo.getTotal(),
                pageInfo.getPageNum(),
                pageInfo.getPageSize(),
                pageInfo.getList());

        return result;
    }
}

用户层 - controller

UserController.java

package com.example.pagehelperdemo.controller;

import com.example.pagehelperdemo.common.PageDTO;
import com.example.pagehelperdemo.dao.User;
import com.example.pagehelperdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author komiles@163.com
 * @date 2020-04-30 18:47
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/one")
    public User getOne(@RequestParam("id") Integer id){
        return userService.getOne(id);
    }

    @GetMapping("/list")
    public PageDTO getList(@RequestParam(value = "page",defaultValue = "1") Integer page, 
            @RequestParam(value = "limit",defaultValue = "10") Integer limit){
        return userService.getList(page,limit);
    }

}

 

启动文件

访问地址:http://127.0.0.1:6066/user/list?page=1&limit=10

 

【spring-boot】分页类使用

标签:override   not   default   enc   get   实现   list   pack   dem   

原文地址:https://www.cnblogs.com/wangkongming/p/12835312.html


评论


亲,登录后才可以留言!