Common usage of mybaits-plus lambdaQuery() and lambdaUpdate()

Common usage of mybaits-plus lambdaQuery() and lambdaUpdate()


Introduction

MyBatis-Plus (opens new window) (referred to as MP) is an enhancement tool for MyBatis (opens new window), based on MyBatis, only enhancements are made without changes, and it is born to simplify development and improve efficiency.

Vision
Our vision is to become the best partner of MyBatis, just like 1P and 2P in Contra, with basic friends and double the efficiency.

insert image description here

  • Non-invasive: only make enhancements without changing, introducing it will not affect existing projects, smooth as silk
  • Low loss: Basic CURD is automatically injected at startup, performance is basically lossless, and direct object-oriented operation
  • Powerful CRUD operations: built-in general mapper and general service, most CRUD operations on a single table can be realized with only a small amount of configuration, and more powerful conditional constructors to meet various usage needs
  • Support Lambda form invocation: Through Lambda expressions, you can easily write various query conditions, no need to worry about writing wrong fields Support primary key automatic generation: Support up to 4 primary key strategies (including distributed unique ID generator - Sequence) Free configuration, perfect solution to the primary key problem
    Support ActiveRecord mode: Support ActiveRecord form call, entity classes can perform powerful CRUD operations only by inheriting the Model class Support custom global general operations: Support global general method injection (Write once, use anywhere)
  • Built-in code generator: Use code or Maven plugin to quickly generate Mapper , Model , Service , Controller layer code, support template engine, and more custom configurations waiting for you to use
  • Built-in paging plug-in: Based on MyBatis physical paging, developers do not need to care about specific operations. After configuring the plug-in, writing paging is equivalent to ordinary List query
  • The paging plugin supports multiple databases: MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Postgre, SQLServer and other databases
  • Built-in performance analysis plug-in: It can output SQL statements and their execution time. It is recommended to enable this function during development and testing, which can quickly identify slow queries
  • Built-in global interception plug-in: provides intelligent analysis and blocking of delete and update operations on the entire table, and can also customize interception rules to prevent misoperation

insert image description here

foreword

Querying data using concise lambda expressions


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

student class

@Data
public class Student {
    
    

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

}

mybaits-plus is simple and clear, the more common way to use it in development

query by id

Student byId = studentService.getById(1);

Conditional query to query objects by id

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

query student collection

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

Conditional set query Search for students in Shanghai based on their address

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

Common paging queries

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

delete by id

studentService.removeById(1);

Conditional delete delete student whose name is Zhang San and age equal to 15

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

Modify modify according to id

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

Modify the address of the student whose student number is 1 to Hunan

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

Query the set of students whose age is less than 20

List<Student> list1 = studentService.lambdaQuery().lt(Student::getAge, 20).list();

Guess you like

Origin blog.csdn.net/Susan003/article/details/126732053