背景
分页,在我们日常查询中使用频率非常高,这里就简单介绍一下springboot中常用的几种分页手段。
一、仅使用Mybatis
1、在Mapper接口中定义分页查询方法
在您的Mapper接口中,定义两个方法:一个用于查询分页数据,另一个用于查询总记录数。例如:
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user LIMIT #{offset}, #{limit}")
List<User> findAllWithPagination(@Param("offset") int offset, @Param("limit") int limit);
@Select("SELECT COUNT(*) FROM user")
int countTotalRecords();
}
2、在Service层中调用Mapper方法
在Service层中,调用Mapper方法并计算分页参数。例如:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> findAllWithPagination(int pageNum, int pageSize) {
int offset = (pageNum - 1) * pageSize;
return userMapper.findAllWithPagination(offset, pageSize);
}
public int countTotalRecords() {
return userMapper.countTotalRecords();
}
}
3、在Controller层中调用Service方法
在Controller层中,调用Service方法并返回分页结果。例如:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public Map<String, Object> getUsers(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
@RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
List<User> users = userService.findAllWithPagination(pageNum, pageSize);
int totalRecords = userService.countTotalRecords();
Map<String, Object> result = new HashMap<>();
result.put("users", users);
result.put("totalRecords", totalRecords);
return result;
}
}
二、使用Mybatis以及插件PageHelper
1、添加PageHelper依赖
在您的pom.xml文件中添加PageHelper依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>YourVersion</version>
</dependency>
2、配置PageHelper
在application.properties或application.yml文件中配置PageHelper:
# application.properties
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
或
# application.yml
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
3、在Mapper接口中使用PageHelper
在您的Mapper接口中,使用@Select注解编写查询语句,并在方法参数中添加RowBounds参数。例如:
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.RowBounds;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user")
List<User> findAllWithPagination(RowBounds rowBounds);
}
4、在Service层中调用Mapper方法
在Service层中,使用PageHelper.startPage()方法设置分页参数,然后调用Mapper方法。例如:
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public PageInfo<User> findAllWithPagination(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> users = userMapper.findAllWithPagination();
return new PageInfo<>(users);
}
}
5、在Controller层中调用Service方法
在Controller层中,调用Service方法并返回分页结果。例如
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public PageInfo<User> getUsers(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
@RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
return userService.findAllWithPagination(pageNum, pageSize);
}
}
三、使用MybatisPlus
1、在Mapper中继承BaseMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface UserEntityMapper extends BaseMapper<UserEntity> {
// 可以自定义查询方法
}
2、在Service类中注入该Mapper,并使用Mybatis Plus提供的Page类来进行分页查询。
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserEntityService {
@Autowired
private UserEntityMapper userEntityMapper;
public IPage<UserEntity> listWithPage(int pageNum, int pageSize) {
Page<UserEntity> page = new Page<>(pageNum, pageSize);
return userEntityMapper.selectPage(page, null);
}
}
3、最后,在Controller中调用Service的方法来获取分页数据,并将其返回给前端:
@RestController
public class UserEntityController {
@Autowired
private UserEntityService userEntityService;
@GetMapping("/list")
public IPage<UserEntity> listWithPage(
@RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
@RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
return userEntityService.listWithPage(pageNum, pageSize);
}
}
最后
以上就是Springboot的常用几种分页,简单记录一下。