Bootstrap

spring boot中实现手动分页

手动分页

  1. UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.mayanan.restapi.mapper.UserMapper">
    <!-- 在复杂的sql查询中,使用resultMap是最佳实践, 它能清晰的定义嵌套对象的映射规则 -->
    <resultMap id="userResultMap" type="cn.mayanan.restapi.bean.UserBean">
        <id column="id" property="id" />
        <result column="name" property="username" />
        <result column="password" property="password" />
    </resultMap>

    <!-- 查询所有用户信息 -->
    <select id="getAllUsers" parameterType="map" resultMap="userResultMap">
        select id, name, password from user
        order by id
        limit #{pageSize}
        offset #{offset}
    </select>
</mapper>
  1. UserMapper.java
package cn.mayanan.restapi.mapper;

import org.apache.ibatis.annotations.Mapper;
import cn.mayanan.restapi.bean.UserBean;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {
    List<UserBean> getAllUsers(Map<String, Object> params);
}
  1. UserService.java
package cn.mayanan.restapi.service;

import cn.mayanan.restapi.bean.UserBean;

import java.util.List;
import java.util.Map;

public interface UserService {
    UserBean queryUserById(int id);

    List<UserBean> queryAllUser(Map<String, Object> params);
}
  1. UserServiceImpl.java
package cn.mayanan.restapi.serviceImpl;

import cn.mayanan.restapi.bean.UserBean;
import cn.mayanan.restapi.mapper.UserMapper;
import cn.mayanan.restapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    // 将Dao注入Service层
    private final UserMapper userMapper;
    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    // 查询所有用户信息
    @Override
    public List<UserBean> queryAllUser(Map<String, Object> params) {
        return userMapper.getAllUsers(params);
    }
}
  1. UserController.java
package cn.mayanan.restapi.controller;

import cn.mayanan.restapi.bean.UserBean;
import cn.mayanan.restapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

// @RestController是一个方便的注解,它结合了@Controller和@ResponseBody注解
@RestController  // 标注为Rest控制器
@RequestMapping("/api")  // 定义基础路径
public class MyRestController {
    // 将Service注入Controller层
    private final UserService userService;
    @Autowired
    public MyRestController(UserService userService) {
        this.userService = userService;
    }

    // 查询所有用户信息
    @GetMapping("/users")
    public List<UserBean> showUsers(
            @RequestParam(value = "pageSize", defaultValue = "1") int pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum
    ) {
        System.out.println("pageSize: " + pageSize + ", pageNum: " + pageNum);
        // 手动分页
        Map<String, Object> map = new HashMap<>();
        int offset = (pageNum - 1) * pageSize;
        map.put("pageSize", pageSize);
        map.put("offset", offset);
        return userService.queryAllUser(map);
    }
}
;