Mybatis-Plus中的分页

Mybatis-Plus中的分页,第1张

目录

一、环境配置

1、pom.xml

2、编辑yml

3、启动类

二、编辑代码

1、配置类

2、pojo对象

3、前端交互VO类

4、mapper层

5、service层

6、统一返回结果

7、controller层

三、测试结果


一、环境配置 1、pom.xml

添加mybatis-plus的依赖

需要注意将原来mybatis的依赖去掉,防止以来之间冲突

        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.3
        

2、编辑yml

添加服务信息以及数据库和mybatis-plus相关信息

#SpringBoot数据源配置
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root


#SpringBoot整合Mybatis-plus
mybatis-plus:
  #定义别名包 作用: 以后封装POJO对象时自动进行拼接
  type-aliases-package: com.jtdemo.pojo
  #引入mapper配置文件
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true


server:
  port: 8090
  servlet:
    #项目默认发布路径/根目录
    context-path: /

3、启动类

添加@MapperScan注解

写入mapper层的路径

package com.jtdemo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.jtdemo.mapper")
public class SpringbootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDemoApplication.class, args);
    }

}

二、编辑代码

 

1、配置类

编辑MybatisPlusConfig配置类

版本比较高的使用这个

package com.jtdemo.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.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration //标识配置类
public class MybatisPlusConfig {
    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }


}

2、pojo对象

编辑User

package com.jtdemo.pojo;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@TableName("user") //让对象与表名一一对应
public class User {

    @TableId(type = IdType.AUTO) //标识主键且主键自增
    @TableField("id")
    private Integer id;
    @TableField("name") //如果属性与字段名称一致(包含驼峰规则),则注释可以省略
    private String name;
    private Integer age;
    private String sex;
}

3、前端交互VO类

编辑EasyUITable

对于使用分页的业务,通常使用此类对象作为返回的交互对象,返回查询的总数以及查询的记录

package com.jtdemo.vo;

import com.jtdemo.pojo.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class EasyUITable {
    //定义总数
    private Long totals;

    //定义记录
    private List rows;
}

4、mapper层

编辑UserMapper类

package com.jtdemo.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jtdemo.pojo.User;
import org.apache.ibatis.annotations.Mapper;


import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper {//泛型一定要添加



}

5、service层

UserService和UserServiceImpl

package com.jtdemo.service;

import com.jtdemo.pojo.User;
import com.jtdemo.vo.EasyUITable;


import java.util.List;


public interface UserService {

    //MP分页
    EasyUITable selectUserPage(Integer page,Integer rows);
}
package com.jtdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jtdemo.mapper.UserMapper;
import com.jtdemo.pojo.User;
import com.jtdemo.service.UserService;
import com.jtdemo.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public EasyUITable selectUserPage(Integer page, Integer rows) {
        //通过IPage对象将page和rows封装
        IPage iPage=new Page(page,rows);
        QueryWrapper queryWrapper=new QueryWrapper();
        iPage=userMapper.selectPage(iPage,queryWrapper);
        long total= iPage.getTotal();
        List userList=iPage.getRecords();
        return new EasyUITable(total,userList);
    }


}
6、统一返回结果

编辑JsonResult类

package com.jtdemo.jsonResult;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class JsonResult {
    /**
     * 成功
     */
    public static final int SUCCESS = 200;

    /**
     * 没有登录
     */
    public static final int NOT_LOGIN = 400;

    /**
     * 发生异常
     */
    public static final int EXCEPTION = 401;

    /**
     * 系统错误
     */
    public static final int SYS_ERROR = 402;

    /**
     * 参数错误
     */
    public static final int PARAMS_ERROR = 403;

    /**
     * 不支持或已经废弃
     */
    public static final int NOT_SUPPORTED = 410;

    /**
     * AuthCode错误
     */
    public static final int INVALID_AUTHCODE = 444;

    /**
     * 太频繁的调用
     */
    public static final int TOO_FREQUENT = 445;

    /**
     * 未知的错误
     */
    public static final int UNKNOWN_ERROR = 499;

    private int code;
    private String msg;
    private T data;


    public static JsonResult build() {
        return new JsonResult();
    }

    public static JsonResult build(int code) {
        return new JsonResult().code(code);
    }

    public static JsonResult build(int code, String msg) {
        return new JsonResult().code(code).msg(msg);
    }

    public static  JsonResult build(int code, T data) {
        return new JsonResult().code(code).data(data);
    }

    public static  JsonResult build(int code, String msg, T data) {
        return new JsonResult().code(code).msg(msg).data(data);
    }

    public JsonResult code(int code) {
        this.code = code;
        return this;
    }

    public JsonResult msg(String msg) {
        this.msg = msg;
        return this;
    }

    public JsonResult data(T data) {
        this.data = data;
        return this;
    }


    public static JsonResult ok() {
        return build(SUCCESS);
    }

    public static JsonResult ok(String msg) {
        return build(SUCCESS, msg);
    }

    public static  JsonResult ok(T data) {
        return build(SUCCESS, data);
    }

    public static JsonResult err() {
        return build(EXCEPTION);
    }

    public static JsonResult err(String msg) {
        return build(EXCEPTION, msg);
    }


}
7、controller层

编辑UserController类

package com.jtdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jtdemo.jsonResult.JsonResult;
import com.jtdemo.pojo.User;
import com.jtdemo.service.UserService;
import com.jtdemo.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@RestController //1.返回数据是JSON数据  2.表示Ajax请求的结束 3.返回值的是字符串本身
// 4.不会执行SpringMVC中的组件 视图解析器
//@Controller     //跳转到指定的页面中 会执行视图解析器 进行路径的拼接 前缀+后缀


@RestController
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * MP分页,分页查询用户信息
     * http://localhost:8090/selectUserPage?page=1&rows=5
     * */
    @GetMapping("/selectUserPage")
    public JsonResult selectUserPage(Integer page,Integer rows){
        EasyUITable easyUITable=userService.selectUserPage(page,rows);
        return new JsonResult<>().ok().data(easyUITable);
    }


}

三、测试结果

输入路径:

http://localhost:8090/selectUserPage?page=1&rows=5

结果展示: 

共36条数据,查询到5条

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/729203.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存