mybaits-plus lambdaQuery() 和 lambdaUpdate() 比较常见的使用方法

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

mybaits-plus lambdaQuery() 和 lambdaUpdate() 比较常见的使用方法

文章目录


简介

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

愿景
我们的愿景是成为 MyBatis 最好的搭档就像 魂斗罗 中的 1P、2P基友搭配效率翻倍。

在这里插入图片描述

  • 无侵入只做增强不做改变引入它不会对现有工程产生影响如丝般顺滑
  • 损耗小启动即会自动注入基本 CURD性能基本无损耗直接面向对象操作
  • 强大的 CRUD 操作内置通用 Mapper、通用 Service仅仅通过少量配置即可实现单表大部分 CRUD 操作更有强大的条件构造器满足各类使用需求
  • 支持 Lambda 形式调用通过 Lambda 表达式方便的编写各类查询条件无需再担心字段写错支持主键自动生成支持多达 4 种主键策略内含分布式唯一 ID 生成器 - Sequence可自由配置完美解决主键问题
    支持 ActiveRecord 模式支持 ActiveRecord 形式调用实体类只需继承 Model 类即可进行强大的 CRUD 操作支持自定义全局通用操作支持全局通用方法注入 Write once, use anywhere
  • 内置代码生成器采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码支持模板引擎更有超多自定义配置等您来使用
  • 内置分页插件基于 MyBatis 物理分页开发者无需关心具体操作配置好插件之后写分页等同于普通 List 查询
  • 分页插件支持多种数据库支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
  • 内置性能分析插件可输出 SQL 语句以及其执行时间建议开发测试时启用该功能能快速揪出慢查询
  • 内置全局拦截插件提供全表 delete 、 update 操作智能分析阻断也可自定义拦截规则预防误操作

在这里插入图片描述

前言

采用简明的拉姆达表达式查询数据


提示以下是本篇文章正文内容下面案例可供参考

学生类

@Data
public class Student {

    /*学号*/
    private Integer sno;
    /*名称*/
    private String name;
    /*年龄*/
    private int age;
    /*班级名称*/
    private String className;
    /*地址*/
    private String address;

}

mybaits-plus 简单明了的开发中比较常见的使用方式

根据id查询

Student byId = studentService.getById(1);

带条件的查询 根据id查询对象

Student one = studentService.lambdaQuery().eq(Student::getSno, 1).one();

查询学生集合

List<Student> list = studentService.list();

带条件的集合查询 根据地址查询所在上海的学生

List<Student> studentList = studentService.lambdaQuery().eq(Student::getAddress, "上海").list();

常见的分页查询

        String name = "张三";
        Integer current = 1;
        Integer size = 10;
        IPage<Student> studentIPage = studentService.page(new Page(current,size),new QueryWrapper<Student>()
        .like(StrUtil.isNotBlank(name),"name",name));

根据id删除

studentService.removeById(1);

带条件的删除 删除名称为张三 年龄等于15的学生

studentService.lambdaUpdate().eq(Student::getName,"张三").eq(Student::getAge,15).remove();

修改 根据id修改

        Student student = new Student();
        student.setSno(1);
        student.setAddress("上海");
        student.setClassName("一年级一班");
        studentService.updateById(student);

修改 将学号为1的学生的地址修改为湖南

studentService.lambdaUpdate().set(Student::getAddress,"湖南").eq(Student::getSno,1).update();
     <    <=  >    >=    <>
    lt() le() gt() ge() ne()

查询年龄小于20的学生集合 其他以此内推

List<Student> list1 = studentService.lambdaQuery().lt(Student::getAge, 20).list();
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6