mybatisPlus advanced articles

map

  • The reason why the Mybatis framework can simplify database operations is because of the internal mapping mechanism, which encapsulates data through automatic mapping. As long as we comply with the mapping rules, we can quickly and efficiently complete the realization of SQL operations.
  • Since MybatisPlus is an enhancement tool based on Mybatis, it also has such mapping rules.

Automatic Mapping Rules

  1. Table name and entity class name mapping -> table name user entity class name User
  2. Field name and entity class attribute name mapping -> field name name entity class attribute name name
  3. Field name underline naming method and entity class attribute lower hump naming method mapping -> field name user_email entity class attribute name userEmail
  • MybatisPlus supports this mapping rule, which can be set through configuration
map-underscore-to-camel-case: true #表示支持下划线到驼峰的映射
map-underscore-to-camel-case: false #表示不支持下划线到驼峰的映射
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true

table mapping

  • Specify the mapped database table name through the @TableName() annotation, and it will be mapped according to the specified table name. For example: at this time, change the table name of the database to powershop_user. To complete the mapping between the table name and the entity class name, you need to specify the entity class name as powershop_user
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("powershop_user")
public class User {
    
    
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
  • If there are many entity classes corresponding to many tables in the database, we don’t need to configure each one in turn, we only need to configure a global setting, which will add a specified prefix to each entity class name, here we demonstrate the global configuration Effect
mybatis-plus:
  global-config:
    db-config:
      table-prefix: powershop_

field mapping

  1. When the attributes of the database fields and the table entity classes are inconsistent, @TableField()annotations can be used to change the mapping between fields and attributes, so that the names in the annotations are consistent with the table fields. For example: at this time, we change the name of the database field to username. When splicing SQL according to the attributes of the entity class, it will use the name username specified in @TableField() to splice and complete the query.
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    
    
    @TableField("username")
    private String name;
   }
  • SQL statement at this time
SELECT id,username AS name,email FROM powershop_user

  1. The attributes of database fields and table entity classes are consistent. When splicing SQL statements, the framework will use attribute names to splice SQL statements directly, for example:
SELECT  id,username AS name,age,email,desc  FROM powershop_user

When this statement is directly queried, an error will occur

Error querying database.  Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version 
for the right syntax to use near 'desc  FROM powershop_user' at line 1
  • Reason: descIt is a keyword and cannot be directly used in sql query. To solve this problem, you need to descadd the `` symbol to the field to make it not a keyword to complete the query. The root of this problem is to change the generated SQL The field name of the statement, that is, we need to @TableField()change the attribute name of the entity class, will descbecome desc, you can solve this problem
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    
    
    @TableField("`desc`")
    private String desc;
}

insert image description here

field failure

  • When there is a field in the database that we don’t want to be queried, we can @TableField(select = false)hide this field, so that when splicing SQL statements, this field will not be spliced
  • For example: if you do not want to display age information, you can add this annotation to the age attribute to hide this field
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    
    
    @TableField(select = false)
    private Integer age;
}
  • The generated SQL statement is as follows
    insert image description here

view properties

  • In actual development, some fields do not need to be stored in the database, but they need to be displayed. The need to display means that this field needs to exist in the entity class. We call these fields that exist in the entity class but do not exist in the database, called view fields.

  • According to previous experience, the framework will splice the attributes in the entity class as query fields by default. Can't such a view field be used as a query condition? Because there is no such field in the database, if the query field contains this field, the SQL statement will have problems. We use @TableField(exist = false)to remove this field and not let it be used as a query field.

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    
    
    @TableField(exist = false)
    private Integer online;
}
  • You can see that this field is not included in the query results
    insert image description here

conditional constructor

Introduction to conditional constructors

  • If you want to use MybatisPlus to complete conditional queries, based on object-oriented thinking, everything is an object, so query conditions also need to use objects to complete the encapsulation. Clarify the classes related to conditions and their relationships in MybatisPlus, and it is more helpful to clarify ideas when passing condition objects.
  1. Wrapper——Abstract class, the top layer of the condition class, provides some methods for obtaining and judging
  2. AbstractWrapper——Abstract class, Wrappera subclass that provides all condition-related methods
  3. AbstractLambdaWrapper——abstract class, AbstractWrappersubclass, determine the field parameter as the method reference type
  4. QueryWrapper——A subclass of AbstractWrapper, if you need to pass Stringthe field information of the type, create this object
  5. LambdaQueryWrapper——A subclass of AbstractLambdaWrapper, if you need to pass the field information of the method reference method, create this object

insert image description here

  • The figure shows the above various relationships. When writing code, you only need to pay attention to QueryWrapper andLambdaQueryWrapper

  • It is necessary to focus on mastering QueryWrapperand LambdaQueryWrapperthese two classes. In general, most of them are selected LambdaQueryWrapper, because they choose to pass parameters in this way, so there is no need to worry about spelling mistakes.

Equivalent query

eq

  • Use the QueryWrapper object to build query conditions
@Test
void eq(){
    
    
    //1.创建QueryWrapper对象
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    //2.设置条件,指定String字段名称和值
    queryWrapper.eq("name","Jack");
    //3.使用条件完成查询
    User user = userMapper.selectOne(queryWrapper);
    System.out.println(user);
}

Test effect
insert image description here


  • If you write the field name yourself every time, there may be a case where the name is wrongly written. To avoid this situation, you can use the LambdaQueryWrapper object. When constructing the field, use the method reference to select the field. This can Avoid problems with misspelled fields.
    code show as below:
@Test
void eq2(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定String字段名称和值
    lambdaQueryWrapper.eq(User::getName,"Jack");
    //3.使用条件完成查询
    User user = userMapper.selectOne(lambdaQueryWrapper);
    System.out.println(user);
}
  • Sometimes it is not sure that all the conditions have values, and some conditions may not be passed by the user, so the condition is null. If the condition is null, it is not necessary to splicing the query conditions, otherwise the following situation will occur, the condition of null will be spliced, and the result cannot be queried after filtering

  • To solve this problem, you can first judge whether it is empty, and choose whether to splice the field according to the judgment result

@Test
void isNull2(){
    
    
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    String name = null;
    lambdaQueryWrapper.eq(name != null,User::getName,name);
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}

allEq

  • First demonstrate how to build a multi-condition query through multiple eqs
@Test
void allEq1(){
    
    
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(User::getName,"Jone");
    lambdaQueryWrapper.eq(User::getAge,18);

    User user = userMapper.selectOne(lambdaQueryWrapper);
    System.out.println(user);
}
  • If there are multiple conditions that need to be judged at the same time, you can put these multiple conditions into the Map collection, which is more convenient
@Test
void allEq2(){
    
    
    //1.创建QueryWrapper对象
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();

    //2.构建条件Map
    HashMap<String, Object> hashMap = new HashMap<>();
    hashMap.put("name","Jone");
    hashMap.put("age",null);

    //3.使用条件完成查询
    queryWrapper.allEq(hashMap,false);
    User user = userMapper.selectOne(queryWrapper);
    System.out.println(user);
}
allEq(Map<R, V> params, boolean null2IsNull)
  • Parameter params: Indicates the passed Map collection
  • Parameter null2IsNull: Indicates whether to judge isNull for the null condition

it is

@Test
void ne(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定String字段名称和值
    String name = "Jone";
    lambdaQueryWrapper.ne(User::getName,name);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows:
    insert image description here

range query

gt

@Test
void gt(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    Integer age = 18;
    lambdaQueryWrapper.gt(User::getAge,age);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

ge(>=)

@Test
void ge(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    Integer age = 18;
    lambdaQueryWrapper.ge(User::getAge,age);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}

The stitched SQL is as follows
insert image description here

lt(<)

@Test
void lt(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    Integer age = 18;
    lambdaQueryWrapper.lt(User::getAge,age);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}

The stitched SQL is as follows
insert image description here

the(<=)

@Test
void le(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    Integer age = 18;
    lambdaQueryWrapper.le(User::getAge,age);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}

The stitched SQL is as follows
insert image description here

between

@Test
void between(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    lambdaQueryWrapper.between(User::getAge,18,30);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}

The stitched SQL is as follows
insert image description here

notBetween

@Test
void notBetween(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    lambdaQueryWrapper.notBetween(User::getAge,18,30);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

fuzzy query

like

@Test
void like(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    lambdaQueryWrapper.like(User::getName,"J");
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

notLike

@Test
void notLike(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    lambdaQueryWrapper.notLike(User::getName,"J");
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}

The stitched SQL is as follows
insert image description here

likeLeft

@Test
void likeLeft(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    lambdaQueryWrapper.likeLeft(User::getName,"e");
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

likeRight

@Test
void likeRight(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    lambdaQueryWrapper.likeLeft(User::getName,"J");
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

Null query

isNull

@Test
void isNull(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称
    lambdaQueryWrapper.isNull(User::getName);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

isNotNull

@Test
void isNotNull(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称
    lambdaQueryWrapper.isNotNull(User::getName);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

contains query

in

@Test
void in(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    ArrayList<Integer> arrayList = new ArrayList<>();
    Collections.addAll(arrayList,18,20,21);
    lambdaQueryWrapper.in(User::getAge,arrayList);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
@Test
void in2(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    lambdaQueryWrapper.in(User::getAge,18,20,21);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}

The stitched SQL is as follows
insert image description here

swimming

@Test
void notIn(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    ArrayList<Integer> arrayList = new ArrayList<>();
    Collections.addAll(arrayList,18,20,21);
    lambdaQueryWrapper.notIn(User::getAge,arrayList);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
@Test
void notIn2(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    lambdaQueryWrapper.notIn(User::getAge,18,20,21);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

inSql

@Test
void inSql(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    lambdaQueryWrapper.inSql(User::getAge,"18,20,22");
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here
@Test
void inSql2(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    lambdaQueryWrapper.inSql(User::getAge,"select age from powershop_user where age > 20");
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

notInSql

@Test
void notInSql(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    lambdaQueryWrapper.notInSql(User::getAge,"18,20,21");
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here
@Test
void notInSql2(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定字段名称和值
    lambdaQueryWrapper.notInSql(User::getAge,"select age from powershop_user where age > 20");
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

Group query

groupBy

@Test
void groupBy(){
    
    
    //1.创建QueryWrapper对象
   QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    //2.设置条件,指定字段名称和值
    queryWrapper.groupBy("age");
    queryWrapper.select("age,count(*) as field_count");
    //3.使用条件完成查询
    List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
    System.out.println(maps);
}
  • The stitched SQL is as follows
    insert image description here
  • actual query result
    insert image description here
  • Package result
    insert image description here

aggregation query

having

@Test
void having(){
    
    
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    //分组字段
    queryWrapper.groupBy("age");
    //查询字段
    queryWrapper.select("age,count(*) as field_count");
    //聚合条件筛选
    queryWrapper.having("field_count = 1");
    List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
    System.out.println(maps);
}
  • The stitched SQL is as follows
    insert image description here

Sort query

orderByAsc

@Test
void orderByAsc(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定升序排序字段
    lambdaQueryWrapper.orderByAsc(User::getAge,User::getId);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

orderByDesc

@Test
void orderByDesc(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定降序排序字段
    lambdaQueryWrapper.orderByDesc(User::getAge,User::getId);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

orderBy

@Test
void orderBy(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.设置条件,指定降序排序字段
    lambdaQueryWrapper.orderBy(true,true,User::getId);
    lambdaQueryWrapper.orderBy(true,false,User::getAge);
    //3.使用条件完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

func query

func

@Test
void func(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.构建逻辑判断语句
    lambdaQueryWrapper.func(i -> {
    
    
        if(true) {
    
    
            i.eq(User::getId, 1);
        }else {
    
    
            i.ne(User::getId, 1);
        }
    });
    //3.完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

logical query

and

  • Normal splicing defaults to and
@Test
void and(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.构建条件查询语句
    lambdaQueryWrapper.gt(User::getAge,22).lt(User::getAge,30);
    //3.完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here
  • and can also be nested
@Test
void and2(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.构建条件查询语句
    lambdaQueryWrapper.eq(User::getName,"wang").and(i -> i.gt(User::getAge,26).or().lt(User::getAge,22));
    //3.完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

or

@Test
void or(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.构建条件查询语句
    lambdaQueryWrapper.lt(User::getAge,20).or().gt(User::getAge,23);
    //3.完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

  • OR nested

@Test
void or2(){
    
    
     LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
     lambdaQueryWrapper.eq(User::getName,"wang").or(i -> i.gt(User::getAge,22).lt(User::getAge,26));
     List<User> users = userMapper.selectList(lambdaQueryWrapper);
     System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

nested

@Test
void nested(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.构建条件查询语句
    lambdaQueryWrapper.nested(i -> i.eq(User::getName, "Billie").ne(User::getAge, 22));
    //3.完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

Custom condition query

apply

@Test
void apply(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.构建条件查询语句
    lambdaQueryWrapper.apply("id = 1");
    //3.完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

last query

last

@Test
void last(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.构建条件查询语句
    lambdaQueryWrapper.last("limit 0,2");
    //3.完成查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

exists query

exists

@Test
void exists(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.构建查询条件
    lambdaQueryWrapper.exists("select id from powershop_user where age = 18");
    //3.查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

notExists

@Test
void notExists(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.构建查询条件
    lambdaQueryWrapper.notExists("select id from powershop_user where age = 33");
    //3.查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

field query

select

@Test
void select(){
    
    
    //1.创建QueryWrapper对象
    LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //2.构建查询条件
    lambdaQueryWrapper.select(User::getId,User::getName);
    //3.查询
    List<User> users = userMapper.selectList(lambdaQueryWrapper);
    System.out.println(users);
}
  • The stitched SQL is as follows
    insert image description here

Guess you like

Origin blog.csdn.net/yang2330648064/article/details/131941099