MyBatis-Plus 实现2种分页方法(QueryWrapper查询分页、SQL查询分页)

 1 MyBatisPlusConfig

MyBatisPlus配置类。

package com.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.*;

/**
 * MyBatisPlus配置类
 */
@Configuration
public class MyBatisPlusConfig {

    /**
     * MyBatisPlus拦截器(用于分页)
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //添加MySQL的分页拦截器
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

2 UserPagination

 用户查询条件类。

package com.entity;

import lombok.Data;

/**
 * 查询条件
 */
@Data
public class UserPagination {
    /**
     * 当前页号
     */
    private int currentPage;
    /**
     * 每页显示条数
     */
    private int pageSize;
}

3 Mapper

3.1 UserMapper.java

package com.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entity.UserEntity;
import com.entity.UserPagination;
import org.apache.ibatis.annotations.Mapper;

/**
 * 用户信息dao层
 */
@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {
    /**
     * 获取用户信息(SQL查询分页)
     *
     * @param page 分页条件
     * @return
     */
    Page<UserEntity> getUserListBySQLPage(Page<UserEntity> page);
}

3.2 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="com.mapper.UserMapper">
    <select id="getUserListBySQLPage" resultType="com.entity.UserEntity">
        SELECT *
        from users
    </select>
</mapper>

4 Service

4.1 UserService

package com.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.entity.*;


public interface UserService extends IService<UserEntity> {
    /**
     * 获取用户信息(QueryWrapper查询分页)
     *
     * @param pagination 查询条件
     * @return
     */
    Page<UserEntity> getUserListByQueryWrapperPage(UserPagination pagination);

    /**
     * 获取用户信息(SQL查询分页)
     *
     * @param pagination 查询条件
     * @return
     */
    Page<UserEntity> getUserListBySQLPage(UserPagination pagination);
}

4.2 UserServiceImpl

package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entity.*;
import com.mapper.UserMapper;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {
   @Autowired
   private UserMapper userMapper;
    /**
     * 获取用户信息(QueryWrapper查询分页)
     *
     * @param pagination 查询条件
     * @return
     */
    public Page<UserEntity> getUserListByQueryWrapperPage(UserPagination pagination) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        Page<UserEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
        return this.page(page, queryWrapper);
    }

    /**
     * 获取用户信息(SQL查询分页)
     *
     * @param pagination 查询条件
     * @return
     */
    @Override
    public Page<UserEntity> getUserListBySQLPage(UserPagination pagination) {
        Page<UserEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
        return userMapper.getUserListBySQLPage(page);
    }
}

5 UserController

调试代码。

package com.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entity.*;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 获取用户信息(QueryWrapper查询分页)
     *
     * @return
     */
    @GetMapping("/getUserListByQueryWrapperPage")
    public Page<UserEntity> getUserListByQueryWrapperPage(UserPagination pagination) {
        return userService.getUserListByQueryWrapperPage(pagination);
    }

    /**
     * 获取用户信息(SQL查询分页)
     *
     * @return
     */
    @GetMapping("/getUserListBySQLPage")
    public Page<UserEntity> getUserListBySQLPage(UserPagination pagination) {
        return userService.getUserListBySQLPage(pagination);
    }

}

6 调试结果 

6.1 QueryWrapper查询分页

6.2 SQL查询分页 

 

 注:

更多MyBatis-Plus的配置请查看以下博客。

Spring Boot 配置MyBatis-Plus(实现查询、新增、更新、删除)

您好!关于您的问题,我可以帮您解答。针对springboot整合mybatis-plus实现多表分页查询实现,可以按照以下步骤进行: 1.在pom.xml文件中添加Mybatis-Plus和Pagehelper的依赖,如下: ``` <!-- Mybatis-Plus依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> <!-- Pagehelper依赖 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency> ``` 2.在Mybatis-Plus的配置文件中,指定分页插件。如下: ``` @Configuration @MapperScan("com.example.mapper") public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } } ``` 3.编写Mapper和对应的Mapper.xml文件,进行多表联合查询,并在Mapper接口方法上添加分页参数。如下: ``` //在Mapper接口方法上添加分页参数 public interface UserMapper extends BaseMapper<User> { List<User> selectUserPage(Page<User> page); } <!-- 在Mapper.xml中编写多表联合查询SQL语句 --> <select id="selectUserPage" resultMap="BaseResultMap"> select u.*, r.role_name from user u left join user_role ur on u.id = ur.user_id left join role r on ur.role_id = r.id <where> <if test="username != null and username != ''"> and u.username like concat('%',#{username},'%') </if> </where> </select> ``` 4.在Controller层中,接受分页参数并返回分页结果。如下: ``` @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public Page<User> selectUserPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, String username) { Page<User> p = new Page<>(page, size); p = userMapper.selectUserPage(p, username); return p; } } ``` 以上就是整合Mybatis-Plus和Pagehelper实现多表分页查询的具体步骤,希望能对您有所帮助!如果您有其他问题,欢迎继续提问。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值