码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis-Plus 分页查询

时间:2021-06-25 17:27:00      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:gets   context   get   ide   ann   dem   bean   main   app   

新建MybatisPlusConfig 配置文件

package com.luna.demo.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhouhao
 * @version 1.0
 * @description: TODO
 * @date 2021/4/27 8:44
 */
//Spring boot方式
@Configuration
@MapperScan("com.luna.demo.*.mapper*")
public class MybatisPlusConfig {
    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }
}

Service

package com.luna.demo.service;

import cn.hutool.json.JSONObject;
import com.luna.demo.domain.SysUser;
import com.baomidou.mybatisplus.extension.service.IService;

/**
 *
 */
public interface SysUserService extends IService<SysUser> {
    JSONObject selectSysUserPage(String name, Integer page, Integer size);

    JSONObject selectSysUserMapsPage(String name, Integer page, Integer size);
}

ServiceImpl

package com.luna.demo.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luna.demo.domain.SysUser;
import com.luna.demo.service.SysUserService;
import com.luna.demo.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
/**
 *
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    // 分页查询 selectPage()
    @Override
    public JSONObject selectSysUserPage(String name, Integer page, Integer size) {
        JSONObject object = new JSONObject();
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = Wrappers.lambdaQuery();
        sysUserLambdaQueryWrapper.like(SysUser::getName, name);
        Page<SysUser> userPage = new Page<>(page, size);
        Page<SysUser> pages = sysUserMapper.selectPage(userPage, sysUserLambdaQueryWrapper);
        object.put("当前页码", pages.getCurrent());
        object.put("当前页码", pages.getOrders());
        object.put("当前页的记录数据", pages.getRecords());
        object.put("当前页的记录条数", pages.getSize());
        object.put("总记录条数", pages.getTotal());
        object.put("总页数", pages.getPages());
        return object;
    }

    // 分页查询 selectMapsPage()
    @Override
    public JSONObject selectSysUserMapsPage(String name, Integer page, Integer size) {
        JSONObject object = new JSONObject();
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = Wrappers.lambdaQuery();
        sysUserLambdaQueryWrapper.like(SysUser::getName, name);
        Page<Map<String, Object>> mapPage = new Page<>(page, size, false);
        Page<Map<String, Object>> mapPage1 = sysUserMapper.selectMapsPage(mapPage, sysUserLambdaQueryWrapper);
        object.put("当前页码", mapPage1.getCurrent());
        object.put("当前页码", mapPage1.getOrders());
        object.put("当前页的记录数据", mapPage1.getRecords());
        object.put("当前页的记录条数", mapPage1.getSize());
        object.put("总记录条数", mapPage1.getTotal());
        object.put("总页数", mapPage1.getPages());
        return object;
    }
}

Controller

package com.luna.demo.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luna.demo.domain.SysUser;
import com.luna.demo.mapper.SysUserMapper;
import com.luna.demo.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhouhao
 * @version 1.0
 * @description: TODO
 * @date 2021/4/26 14:57
 */
@RestController
@Slf4j
@RequestMapping("/CURD1")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    //分页
    @RequestMapping("/page")
    public JSONObject pageDemo(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "2") int size, String name) {
        return sysUserService.selectSysUserPage(name,page,size);
    }

    //分页
    @RequestMapping("/mappage")
    public JSONObject pageMapDemo(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "2") int size, String name) {
        return sysUserService.selectSysUserMapsPage(name, page, size);
    }
}

MyBatis-Plus 分页查询

标签:gets   context   get   ide   ann   dem   bean   main   app   

原文地址:https://www.cnblogs.com/bfsz/p/14930929.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!