SoFunction
Updated on 2025-03-02

Detailed explanation of the usage of LambdaQueryWrapper of MybatisPlus

LambdaQueryWrapper<Tag>  is a powerful query constructor in the MyBatis-Plus framework. It is used to build SQL query conditions, especially for Lambda expression support, making the code more concise and type-safe. In this example, LambdaQueryWrapper<Tag>  is used to build query conditions for the Tag entity class.

Basic usage

LambdaQueryWrapper allows you to add query conditions in chained calls. Each conditional method accepts a Lambda expression, which specifies the fields in the Tag entity class. The advantage of this is that when writing query conditions, you can directly reference the properties of the entity class through the field name (rather than a string). This not only reduces runtime errors caused by misspelling of field names, but also improves the readability and maintainability of the code.

Example

Suppose the Tag entity class has the following fields: id, name, description.

1. Query all records

Although LambdaQueryWrapper is mainly used to build queries with conditions, if you want to query all records, you can simply not add any conditions, or call ("*") (although this is usually the default).

List<Tag> tags = (new LambdaQueryWrapper<>());

2. Query records for specific conditions

LambdaQueryWrapper&lt;Tag&gt; queryWrapper = new LambdaQueryWrapper&lt;&gt;();  
(Tag::getName, "exampleName"); // Query records whose name field is equal to "exampleName"List&lt;Tag&gt; tags = (queryWrapper);

3. Combination conditions

You can also combine multiple conditions to build more complex queries.

LambdaQueryWrapper&lt;Tag&gt; queryWrapper = new LambdaQueryWrapper&lt;&gt;();  
(Tag::getName, "exampleName")  
            .or() // Logical or            .like(Tag::getDescription, "exampleDescription");  
// Query the record whose name field is equal to "exampleName" or description field contains "exampleDescription"List&lt;Tag&gt; tags = (queryWrapper);

Things to note

When using LambdaQueryWrapper, make sure your version of MyBatis-Plus supports Lambda expressions.

When you use LambdaQueryWrapper, all conditional methods (such as eq, like, gt, etc.) accept a Lambda expression as a parameter, which should refer to fields in the entity class.

MyBatis-Plus provides a rich method of conditioning that you can choose to use according to your needs.

In short, LambdaQueryWrapper<Tag>  is a very useful tool in MyBatis-Plus, which makes building SQL query conditions easier, intuitive and type safe.

This is the article about the detailed explanation of the usage of MybatisPlus LambdaQueryWrapper. For more related contents of MybatisPlus LambdaQueryWrapper, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!