spring boot中实现手动分页

手动分页

  1. UserMapper.xml
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
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
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
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
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);
    }
}
相关推荐
陈平安Java and C1 小时前
MyBatisPlus
java
秋野酱2 小时前
如何在 Spring Boot 中实现自定义属性
java·数据库·spring boot
安的列斯凯奇2 小时前
SpringBoot篇 单元测试 理论篇
spring boot·后端·单元测试
Bunny02122 小时前
SpringMVC笔记
java·redis·笔记
架构文摘JGWZ3 小时前
FastJson很快,有什么用?
后端·学习
BinaryBardC3 小时前
Swift语言的网络编程
开发语言·后端·golang
feng_blog66883 小时前
【docker-1】快速入门docker
java·docker·eureka
邓熙榆3 小时前
Haskell语言的正则表达式
开发语言·后端·golang
枫叶落雨2224 小时前
04JavaWeb——Maven-SpringBootWeb入门
java·maven
m0_748232395 小时前
SpringMVC新版本踩坑[已解决]
java