Mybatis-plus 分页及条件构造器的使用


项目中要用到MP,为了做需求时不至于手忙脚乱,所以这几天也都在学习Mybatis-plus。今天主要说条件构造器与 3 版本中MP自带的分页插件,具体从 3.几 开始我没查,不过我用的是3.3.1。

一、导入依赖

<dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>3.3.1</version>
</dependency>

二、书写分页拦截器

package com.exec.studymp.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@Configuration
@MapperScan("com.exec.studymp.user.mapper")
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}

三、使用new Page<>(current,pageSize)实现分页。

  1、实体类User

package com.exec.studymp.user.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.springframework.stereotype.Component;

@Data
@Component
@TableName(value = "user")
public class User {
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

  2、UserMapper

package com.exec.studymp.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.exec.studymp.user.entity.User;

public interface UserMapper extends BaseMapper<User> {
}

  3、UserController

  @GetMapping("/query")
    public Map<String, Object> query(@RequestParam Integer curPage, @RequestParam Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByDesc("id");

        Page<User> page = new Page<>(curPage, pageSize);
        IPage<User> ipage = userMapper.selectPage(page, queryWrapper);
        map.put("total", ipage.getRecords());
        map.put("current", ipage.getCurrent());
        map.put("pages", ipage.getPages());
        System.out.println("map---" + map);
        return map;
   }

--------------------------------以上是分页的使用--------------------------------------


 

四、条件构造器

    public PageBody query(User user) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        if (user.getId() != null) {
            qw.eq("id", user.getId());
        }
        if (user.getName() != null) {
            qw.eq("name", user.getName());
        }
        if (user.getAge != null) {
            qw.eq("age", user.getAge());
        }
        if (user.getEmail() != null) {
            qw.eq("email", user.getEmail());
        }
        qw.orderByAsc("id").orderByDesc("age");

        Page<User> page = new Page<>(user.getCurrent(), user.getPageSize());
        IPage<User> allPages = userMapper.selectPage(page, qw);

        PageBody pageBody = new PageBody();
        pageBody.setCount(allPages.getTotal());
        pageBody.setValue(allPages.getRecords());
        return pageBody;
    }

eq 是equal的缩写,相当于sql语句:where id=user.getId() and name=user.getName().....

orderByAsc 按升序排序

orderByDesc 按降序排序

在sql查询用到的组件可以在Mybatis-plus的官网 <条件构造器> 一节看到,官网地址:https://mp.baomidou.com/guide/wrapper.html#abstractwrapper

 

最后要说的是,要系统学习Mybatis-plus的话,就去看看官网,我这只是举了一些栗子~~

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM