从零搭建SpringBoot后台框架(七)——集成Mybatis Plus

462 阅读1分钟

一、简介

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

二、添加依赖

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

mybatis-plus-boot-starter3.5.3.2mybatis-spring-boot-starter2.2.0存在冲突,升级mybatis-spring-boot-starter版本到2.3.1

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.3.1</version>
</dependency>

三、配置分页插件

package com.example.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.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));
        //如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
        //interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

四、使用

UserInfoMapper

修改UserInfoMapper继承BaseMapper

package com.example.demo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Param;

import java.util.*;

public interface UserInfoMapper extends BaseMapper<UserInfo> {
    UserInfo selectById(@Param("id") Long id);

    List<UserInfo> selectAll();
}

UserInfoService

Page<UserInfo> selectPage(Integer pageNum, Integer pageSize);

UserInfoServiceImpl

@Override
public Page<UserInfo> selectPage(Integer pageNum, Integer pageSize) {
    return userInfoMapper.selectPage(new Page<>(pageNum, pageSize), null);
}

UserInfoController

@PostMapping("/selectAll")
@ApiOperation(value = "查询所有用户信息")
@ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "当前页码", dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页显示条数", dataType = "Integer", paramType = "query")
})
public RestResult<PageInfo<UserInfo>> selectAll(@RequestParam(defaultValue = "0") Integer pageNum,
                                                @RequestParam(defaultValue = "0") Integer pageSize) {
    PageInfo<UserInfo> pageInfo = userInfoService.selectAll(pageNum, pageSize);
    return RestResult.getSuccessResult(pageInfo);
}

五、测试

访问http://localhost:8080/userInfo/selectPage?pageSize=1&pageNum=2返回如下:

{
    "code": "000000",
    "msg": "操作成功",
    "data": {
        "records": [
            {
                "id": 2,
                "userName": "2",
                "password": null
            }
        ],
        "total": 2,
        "size": 1,
        "current": 2,
        "orders": [],
        "optimizeCountSql": true,
        "searchCount": true,
        "maxLimit": null,
        "countId": null,
        "pages": 2
    }
}

七、项目地址

gitee

PS:可以通过tag下载本文对应的代码版本

八、结尾

mybaits plus集成已完成,有问题可以联系chenzhenlindx@qq.com

九、参考文章

  1. 从零搭建自己的SpringBoot后台框架(八)
  2. mybatis plus官网:baomidou.com/