当前位置: 首页 > news >正文

【MyBatisPlus】MyBatisPlus介绍与使用

【MyBatisPlus】MyBatisPlus介绍与使用

文章目录
      • 【MyBatisPlus】MyBatisPlus介绍与使用
        • 1、什么MyBatisPlus
        • 2、MyBatisPlus的CRUD操作
        • 3、MyBatisPlus分页使用
1、什么MyBatisPlus

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

  • 官网:https://baomidou.com/

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());
    }
    

相关文章:

  • spring boot + thymeleaf整合完整例子
  • ES6-Symbol
  • CTF题目《高明的黑客》(强网杯 2019)Write up
  • 靶场(十八)---小白心得思路分享---shenzi
  • 数据库三级填空+应用(2)
  • 使用 gone.WrapFunctionProvider 快速接入第三方服务
  • vue数字公式篇 Tinymce结合使用(二)
  • Python图像处理——基于CSRNet的人群密度检测系统(Pytorch框架)
  • 【Spring AI】基于专属知识库的RAG智能问答小程序开发——功能优化:用户鉴权相关工具类代码
  • 基于深度学习的图像识别技术在智能安防中的应用
  • 设计模式代码
  • 【sylar-webserver】5 协程调度模块
  • Django之旅:第六节--mysql数据库操作增删改查(二)
  • 【程序人生】我的holland原书版职业测试分析
  • 鸿蒙Flutter实战:19-Flutter集成高德地图,跳转页面方式
  • LLM(大语言模型)的算子融合技术
  • 精品推荐-2025全固态电池会议演讲嘉宾(脱敏)PPT合集(30份).zip
  • PyTorch量化技术教程:第三章 PyTorch模型构建与训练
  • PyTorch中的Tensor
  • HarmonyOS(扩展篇三):车联网操作系统
  • 人社部:将制定提前领取个人养老金相关办法
  • 五一假期如何躺赚利息?来看国债逆回购操作攻略
  • 龚正会见委内瑞拉副总统罗德里格斯
  • 日本大米价格连续16周上涨,再创最高纪录
  • 李公明|一周画记:哈佛打响第一枪
  • 美国政府将暂时恢复部分受影响留学生的合法身份,并将制订新标准