• 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
    
  • 相关阅读:
    (转载)悟透JavaScript
    (转载)详解Javascript中prototype属性(推荐)
    A股委托类型
    深交所开盘步骤
    转:SpringMVC 4.1 新特性(二)内容协商视图
    Fidessa
    Spring框架是一种非侵入式的轻量级框架
    在Spring中配置jdbc为什么不能用${username}问题
    windows安装mysql
    新股定价谁说了算?一文读懂中国IPO询价制度
  • 原文地址:https://www.cnblogs.com/yizhixiang/p/13657830.html
Copyright © 2020-2023  润新知