【MyBatisPlus】MyBatisPlus介绍与使用

【MyBatisPlus】MyBatisPlus介绍与使用

文章目录
1、什么MyBatisPlus

MyBatisPlus(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发、提高效率

MyBatisPlus特性:

  • 无侵入:只做增强不做改变,不会对现有工程产生影响
  • 强大的 CRUD 操作:内置通用 Mapper,少量配置即可实现单表CRUD 操作(如果只做单表增删查改不需要你写任何的sql)
  • 支持 Lambda:编写查询条件无需担心字段写错
  • 支持主键自动生成
  • 内置分页插件
  • ......

常见的开发方式:

  • 基于MyBatis使用MyBatisPlus
  • 基于Spring使用MyBatisPlus
  • 基于SpringBoot使用MyBatisPlus
2、MyBatisPlus的CRUD操作

使用mybatisplus的步骤

复制代码
1. 导入mp的启动器
2. 编写application.yml文件,配置数据源,打印日志
3. 编写mapper接口,Mapper接口需要基础BaseMapper接口,BaseMapper接口需要指定操作的是哪个实体类。
4. 在启动类中扫描的Mapper包
5. 测试使用

正如官网所言:mybatis-plus在mybatis的基础上只做增强不做改变,因此只需把mybatis的依赖换成mybatis-plus的依赖

复制代码
      <!-- mybatis-plus的驱动包 -->
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.4.2</version>
</dependency>

**注意:**这是mybatis-plus依赖,真正使用起来肯定是根据项目添加,如:mysql驱动、lombok等。

编写application.yml文件,配置数据源

复制代码
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true

编写mapper接口,Mapper接口需要基础BaseMapper接口,BaseMapper接口需要指定操作的是哪个实体类。

复制代码
public interface UserMapper extends BaseMapper<User> { //这里操作的是User这个实体类
     
}

mybatis-plus为我们提供了一些标准数据层的CRUD功能,这些功能省去了我们自定义接口,也与我们自定义接口有部分差异,如下表:

功能

自定义接口

MP接口

新增

boolean save(T t)

int insert(T t)

删除

boolean delete(int id)

int deleteById(Serializable id)

修改

boolean update(T t)

int updateById(T t)

根据id查询

T getById(int id)

T selectById(Serializable id)

查询全部

List getAll()

List selectList()

分页查询

PageInfo getAll(int page, int size)

IPage selectPage(IPage page)

按条件查询

List getAll(Condition condition)

IPage selectPage(Wrapper queryWrapper)

新增

复制代码
    /**
     * 增加
     */
    @Test
    public void testInsert(){
        User user = new User();
        user.setName("小林");
        user.setGender("男");
        user.setPassword("root");
        user.setAge(19);
        user.setTel("18000110011");
        userMapper.insert(user);
    }

删除

复制代码
    /**
     * 删除
     */
    @Test
    public void testRemove(){

        userMapper.deleteById(1480751909521403906L);
    }

更新

复制代码
    /**
     * 更新
     */
    @Test
    public void testUpdate(){
        User user =new User();
        user.setId(7L);
        user.setName("张小炮");  //注意: 生成update语句设置的字段为非空字段。
        userMapper.updateById(user); //update user set xx=xx ,xxx=xx ,xx=xx where id =xx
    }

查询全部

复制代码
    @Test
    public void testUserList(){
        List<User> users = userMapper.selectList(null);
        for (User user : users) {
            System.out.println(user);
        }
    }

条件查询

复制代码
    /**
     * 条件查询
     */
    @Test
    public void testFindByCondition(){
        //QueryWrapper代表就是条件
        QueryWrapper<User> queryWrapper  = new QueryWrapper<>();
        //添加条件
        //queryWrapper.lt()  greater than 小于
        //queryWrapper.le()  greater equal 小于等于
        //queryWrapper.ge()  greater equal 大于等于
        //queryWrapper.eq()  equal 等于
        //queryWrapper.gt()  greater than 大于
        queryWrapper.gt("age",18);
        List<User> userList = userMapper.selectList(queryWrapper);
        System.out.println("用户列表:"+ userList);
    }
3、MyBatisPlus分页使用

功能

MP接口

分页查询

IPage selectPage(IPage page)

如果需要使用到mybatis-plus的分页功能,必须存在一个配置类该配置类创建Mybatis的拦截器,这个拦截器的作用就是在你执行selectPage的方法的时候对sql进行拦截,然后拼接limit语句实现分页。

设置分页拦截器作为Spring管理的bean

  1. 在config包下创建一个配置类:MybatisPlusConfig

  2. 在类上添加@Configuration

  3. 编写方法

    1. 方法上使用@Bean注解:添加MybatisPlusInterceptor对象到容器中
    2. 创建MybatisPlusInterceptor拦截器对象
    3. 添加内部分页拦截器:创建PaginationInnerInterceptor

    @Configuration

    public class MybatisPlusInterceptorConfig {

    复制代码
    /**
     * 创建MybatisPlusInterceptor拦截器封装里面分页拦截器PaginationInnerInterceptor
     * 作用:实现对mp内置分页方法拦截增强,实现分页2条sql语句原理
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }

    }

在测试类中执行分页查询

  1. 创建分页对象,前面是接口IPage,后面是实现类Page(第几页,每页大小)

  2. 调用selectPage方法,传入page对象,无需接收返回值

  3. 获取分页结果

    复制代码
    @Test
    public void testPage(){
        //查询第一页,每页3条数据
    
        //1、创建Page对象,构造函数传入当前页码和每页显示多少条数据
        Page<User> page = new Page<>(1, 3); //当前页1  页面大小是3
        //2、执行selectPage方法
        page = userMapper.selectPage(page,null);
    
        //3、获取分页数据
        System.out.println("总条数:" + page.getTotal());
        List<User> records = page.getRecords();
        for (User record : records) {
            System.out.println(record);
        }
    
        //4、获取当前页
        System.out.println("当前页:" + page.getCurrent());
        System.out.println("每页大小:" + page.getSize());
    }
相关推荐
GitLqr15 分钟前
Flutter 3.44 插件内置 Kotlin (KGP) 双向兼容适配指南
android·flutter·dart
Coder_Shenshen1 小时前
西门子S7CommPlus协议鉴权算法原理与流程详解
网络·后端·算法
大圣编程1 小时前
Python中continue语句的用法是什么?
开发语言·前端·python
yuhaiqiang1 小时前
随手 vibecoding 的浏览器插件已经 6000 多次下载,聊聊他的产品设计
前端·后端·面试
之歆2 小时前
Vue商品详情与放大镜组件
前端·javascript·vue.js
再吃一根胡萝卜3 小时前
如何把小米 MiMo 接入 CodeBuddy,打造私有 Agent
前端
geovindu3 小时前
python: Functional Options Pattern
开发语言·后端·python·设计模式·惯用法模式·函数式选项模式
负责的蛋挞4 小时前
异步HttpModule的实现方式
java·服务器·前端
随遇丿而安4 小时前
第11周:Activity 跳转与传值 + 跳转优化
android
卷无止境5 小时前
C++ 存储类说明符(Storage Class Specifier)大横评
c++·后端