【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的依赖

xml 复制代码
      <!-- 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文件,配置数据源

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

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

java 复制代码
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)

新增

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

删除

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

        userMapper.deleteById(1480751909521403906L);
    }

更新

java 复制代码
    /**
     * 更新
     */
    @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
    }

查询全部

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

条件查询

java 复制代码
    /**
     * 条件查询
     */
    @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
java 复制代码
@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. 获取分页结果
java 复制代码
    @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());
    }
相关推荐
摇滚侠25 分钟前
Spring Boot 3零基础教程,整合Redis,笔记12
spring boot·redis·笔记
荣淘淘25 分钟前
互联网大厂Java求职面试全景实战解析(涵盖Spring Boot、微服务及云原生技术)
java·spring boot·redis·jwt·cloud native·microservices·interview
吃饭最爱35 分钟前
spring高级知识概览
spring boot
该用户已不存在43 分钟前
MySQL 与 PostgreSQL,该怎么选?
数据库·mysql·postgresql
舒克日记1 小时前
基于springboot针对老年人的景区订票系统
java·spring boot·后端
川石课堂软件测试1 小时前
自动化测试之 Cucumber 工具
数据库·功能测试·网络协议·测试工具·mysql·单元测试·prometheus
沐雨橙风ιε1 小时前
Spring Boot整合Apache Shiro权限认证框架(实战篇)
java·spring boot·后端·apache shiro
lang201509282 小时前
MySQL数据类型存储全解析
mysql
一线大码2 小时前
开发 Java 项目时的命名规范
java·spring boot·后端