MybatisPlus分頁條件查詢


MybatisPlus分頁條件查詢

前提:先導入MybatisPlus相關依賴

1、配置分頁插件(不需要修改)

PaginationInterceptor

package com.wagn.s.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PageConfig {
    /**
     * 分頁插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

2、將查詢條件封裝成類

VoQuery

package com.wagn.s.vo;

import lombok.Data;

@Data
public class VoQuery {
    private String name;
    private String gender;
}

3、接口使用get請求,傳入需要的參數

query接口

package com.wagn.s.controller;


import com.wagn.s.entity.UserTable;
import com.wagn.s.service.UserTableService;
import com.wagn.s.vo.VoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.nio.file.Watchable;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-12
 */
@RestController
@RequestMapping("/s/user-table")
public class UserTableController {

    @Autowired
    private UserTableService userTableService;

    @GetMapping("query/{current}/{limit}")
    public List<UserTable> query(@PathVariable Long current, @PathVariable Long limit,
                                 VoQuery voQuery){
        //current為當前頁,limit為每頁顯示個數,voQuery為封裝的查詢條件
        List<UserTable> rs = userTableService.myquery(current,limit,voQuery);
        return rs;
    }

}

4、獲得條件后,執行多條件分頁查詢

myquery()方法

package com.wagn.s.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wagn.s.entity.UserTable;
import com.wagn.s.mapper.UserTableMapper;
import com.wagn.s.service.UserTableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wagn.s.vo.VoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服務實現類
 * </p>
 *
 * @author testjava
 * @since 2020-09-12
 */
@Service
public class UserTableServiceImpl extends ServiceImpl<UserTableMapper, UserTable> implements UserTableService {

    @Autowired
    private UserTableService userTableService;
    @Override
    public List<UserTable> myquery(Long current, Long limit, VoQuery voQuery) {


        //初始化page
        Page<UserTable> page = new Page<>(current,limit);

        //設置條件
        QueryWrapper<UserTable> wrapper =new QueryWrapper<>();
        //eq是等於,ge是大於等於,gt是大於,le是小於等於,lt是小於,like是模糊查詢
        
        if(!StringUtils.isEmpty(voQuery.getName())){
            wrapper.like("name",voQuery.getName());
        }
        if(!StringUtils.isEmpty(voQuery.getGender())) {
            wrapper.eq("gender", voQuery.getGender());
        }

        //執行查詢
        userTableService.page(page,wrapper);


        long total = page.getTotal();//總數
        List<UserTable> rs = page.getRecords();//結果
        return rs;
    }
}

(5、控制台查看底層執行的SQL)

配置屬性

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM