使用分页查询可以按照以下步骤:

1.在pom.xml文件中添加mybatis-plus的依赖项:

<!-- Mybatis Plus -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>2.1.9</version>
</dependency>

2.在Mapper接口中添加方法,使用Mybatis-Plus提供的分页查询方法:

public interface UserMapper extends BaseMapper<User> {

    /**
     * 分页查询用户
     *
     * @param page 分页参数
     * @return 分页结果集
     */
    IPage<User> selectUserPage(Page<User> page);
}

3.在Service中调用Mapper的分页查询方法:

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public IPage<User> getUserList(Page<User> page) {
        return userMapper.selectUserPage(page);
    }
}

4.在Controller中调用Service的分页查询方法,返回分页结果集:

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public Result<List<User>> getUserList(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<User> page = new Page<>(pageNum, pageSize);
        IPage<User> userPage = userService.getUserList(page);
        return Result.success(userPage.getRecords(), userPage.getTotal());
    }
}

以上就是使用Mybatis-Plus实现分页查询的步骤

spring43已经集成了mybatis-plus219如何使用分页查询

原文地址: https://www.cveoy.top/t/topic/fGNT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录