Location>code7788 >text

Third, MyBatis-Plus "super detailed description" of the various queries, such as (equal value query, range query, fuzzy query ...)

Popularity:127 ℃/2024-09-26 19:28:47

Third, MyBatis-Plus "super detailed description" of the various queries, such as (equal value query, range query, fuzzy query ...)

@

catalogs
  • Third, MyBatis-Plus "super detailed description" of the various queries, such as (equal value query, range query, fuzzy query ...)
  • 1. Introduction to conditional constructors
  • 2. Preparatory work:
  • 3. Equivalent queries
    • 3.1 eq (Conditional filter attribute = ?)
    • 3.2 allEq (filtering of values that satisfy multiple condition fields)
    • 3.3 ne (not equal)
  • 4. Scope queries
    • 4.1 gt( > greater than range)
    • 4.2 ge(>= range greater than or equal to)
    • 4.3 lt(
    • 4.4 le (range less than or equal to)
    • 4.5 between (in the range of the interval)
    • 4.6 notBetween (not in the range of the interval)
  • 5. Fuzzy queries
    • 5.1 like( % value %)
    • 5.2 notLike (does not satisfy % value %)
    • 5.3 likeLeft (% value)
    • 5.4 likeRight (value %)
  • 6. Empty queries
    • 6.1 isNUll(determine if it is Null)
    • 6.2 isNotNull
  • 7. Inclusion of queries
    • 7.1 in (the field containing the content)
    • 7.2 notIn (fields that do not contain this content)
    • 7.3 inSql (the field containing the content)
    • 7.4 notInSql (fields that do not contain this content)
  • 8. Grouping queries
    • 8.1 groupBy (grouped fields)
  • 9. Aggregation queries
    • 9.1 having (aggregated fields)
  • 10. Sorting queries
    • 10.1 orderByAsc (ascending order)
    • 10.2 orderByDesc (descending order)
    • 10.3 orderBy (multi-field sort definition)
  • 11. func queries
  • 12. Logical queries
    • 12.1 and (with)
    • 12.2 or (or)
    • 12.3 nested (not)
  • 13. Customized conditional queries
    • 13.1 apply (custom query conditions)
  • 14. last query (splice "string" at the end of the sql statement)
  • 15. exists query
    • 15.1 exists (existence query)
    • 15.2 notExists (non-existent queries)
  • 16. Field queries
    • 16.1 select (field query)
  • 17. Finally:


1. Introduction to conditional constructors

In the actual development needs of the conditional query is very common, we will explain how to use MyBatis Plus to complete the conditional query.

First, you want to use MyBatis Plus to accomplish conditional queries, based on object-oriented thinking.Everything is an object. Then the query conditions, also need to use the object to complete the package. We can first look at, in MyBatis Plus and the conditions related to what the class, what is the relationship between them, sorted out this, we are in the passing of the conditional object, it is very clear.

Wrapper Abstract class, the top level of the Conditional class, provides a number of get and judgment related methods.

在这里插入图片描述

AbstractWrapper Abstract class, subclass of Wrapper, providing all condition-related methods.

在这里插入图片描述

AbstractLambdaWrapper Abstract class, subclass of AbstractWrapper, identifies field parameters as method reference types.

在这里插入图片描述

QueryWrapper class, a subclass of AbstractWrapper, is created if we need to pass information about a field of type String.

在这里插入图片描述

LambdaQueryWrapper class, a subclass of AbstractLambdaWrapper, creates this object if we need to pass information about a field in a method reference fashion.

在这里插入图片描述

The diagram shows the relationship between the above categories, and when we write the code, we only need to focus on theQueryWrapper cap (a poem)LambdaQueryWrapper

在这里插入图片描述

在这里插入图片描述


2. Preparatory work:

introduction of relevantjar Dependencies. In the file;

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance"
         xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId></groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId></groupId>
    <artifactId>mp03</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mp03</name>
    <description>mp03</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <>8</>
    </properties>
    <dependencies>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- spring boot web dependencies-->
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- mysql 驱动dependencies-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>



        <!-- lombok 的dependencies-->
        <dependency>
            <groupId></groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- druid-->
        <dependency>
            <groupId></groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>

        <!-- mybatis-plus 的dependencies-->
        <dependency>
            <groupId></groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId></groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

</project>

Write a scene starter for the project:

在这里插入图片描述

package ;

import ;
import ;

@SpringBootApplication
public class Mp03Application {

    public static void main(String[] args) {
        (, args);
    }

}

Database tables and structures created. Below:

在这里插入图片描述

Write a Java bean class object that corresponds to the data table.

在这里插入图片描述

package ;


import ;
import ;
import ;
import ;
import ;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

    private Long id;
    //@TableField(value = "username")
    private String name;

    //@TableField(select = false) // Upon inquiry,queerage Fields to query
    private Integer age;
    private String email;

    @TableField(value = "`desc`") // take note of:There are two layers.,But which one of them isn't a quotation mark?
    private String desc;


    @TableField(exist = false) // indicate// ,deny online causality,act as SQLThe query field in the statement
    private Integer online;

}

Under the resources class path, create a file named yaml file that writes the content of the information for connecting to the database.

在这里插入图片描述

spring.
  datasource.
    driver-class-name: jdbc:mysql://localhost:3306/mybatisplus?
    url: jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
    username: root
    password: MySQL123
  main: banner-mode: off # spring-mode: off
    banner-mode: off #Disable the spring boot icon at the command line.


mybatis-plus.
  global-config: banner: false # Turn off mybatis-plus icon display on the command line.
    banner: false # Turn off the display of the mybatis-plus icon at the command line.
    db-config.
      table-prefix: rainbowsea_ # You can also add prefixes in a uniform way:

  configuration.
    log-impl: # Enable log message printing.
    map-underscore-to-camel-case: true # Enable hump, underscore mapping rules.

Write a configuration class to replace the database connection pool with a Druid database connection pool by means of a configuration class.

在这里插入图片描述

package ;


import ;
import ;
import ;
import ;

import ;

@Configuration // Labeling Configuration Classes
public class DruidDataSourceConfig {

    @Bean
    @ConfigurationProperties(value = "")
    public DataSource getDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();

        return druidDataSource;
    }
}

Run a test to see if it switches to Druid database connection pooling

在这里插入图片描述

package ;

import ;
import ;
import ;
import ;

import ;

@SpringBootTest
class Mp03ApplicationTests {


    @Resource
    //@Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    void getDataSourceTest() {
        (().getClass());
    }

}

在这里插入图片描述

Write: mapper method for this User table.

在这里插入图片描述

package ;

import ;
import ;
import ;


@Mapper // Packet Scan Path
public interface UserMapper extends BaseMapper<User> {


}

3. Equivalent queries

3.1 eq (Conditional filter attribute = ?)

Using the QueryWrapper object, build the query condition

Equivalent queries are used:()


default Children eq(R column, Object val) {
   return eq(true, column, val);
}

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {
    public class QueryTest { @Autowired
    private UserMapper userMapper;

    @Test
    void eq() {
        // 1. Create the conditional query object
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        // 2. Set the query conditions, specifying the fields to query and the values to match.
        QueryWrapper<User> eq = ("name", "Jone");

        // 3. Perform a conditional query
        User user = (eq);
        (user);
    }
}

在这里插入图片描述

We think about if every time is to carry out their own field name writing, there is a possibility that the name will be written incorrectly, how to avoid this situation, we can use the LambdaQueryWrapper object, in the construction of the field, the use of method references to select the field, this can be avoided to avoid the problem of misspelling of the field.

The code is as follows:

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {
    public class QueryTest { @Autowired
    private UserMapper userMapper;

    // Use: LambdaQueryWrapper for query referencing to prevent errors.
    @Test
    void eq2() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<> ();
        LambdaQueryWrapper<User> queryWrapper = (User::getName, "Jone");

        User user = (queryWrapper).
        (user);
    }
}

在这里插入图片描述

There is one more situation to consider, where do the conditions we build come from? It should come from the client that sends it in via a request.

received by the server. In the site will generally have more than one condition entry, the user can choose one or more conditions for the query, then this time in the request, we can not be sure that all the conditions have a value, part of the conditions may be the user did not pass the value, then the condition is null.

For example, in an e-commerce site, you can select multiple query conditions.

在这里插入图片描述

That is null conditions, we do not need to query conditions splicing, otherwise the following situation will occur, will be null conditions for splicing, screening can not query the results.

在这里插入图片描述

Of course, we want to solve this problem, you can first determine whether it is empty, according to the results of the judgment to choose whether to splice the field, this function does not require us to write, by the MybatisPlus method has provided a good.

在这里插入图片描述

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;


@SpringBootTest // Note that this test must have a scenario class to work,Otherwise it won't work.。If the tested classes are different,It also needs to be specified
// contract (to or for)
public class QueryTest {
    @Autowired
    private UserMapper userMapper;

    // The field name is (negative prefix)null(used form a nominal expression)
    @Test
    void isNull2() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String name = null;
        //String name = "Jone";

        // public Children eq(boolean condition, R column, Object val) {
        (name != null, User::getName, name);
        //User user = (lambdaQueryWrapper);
        List<User> users = (lambdaQueryWrapper);
        (users);

    }
}

在这里插入图片描述

3.2 allEq (filtering of values that satisfy multiple condition fields)

Let's start by demonstrating how to pass multipleeq, constructing multiconditional queries.

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {

    public class QueryTest { @Autowired
    private UserMapper userMapper;

    // and satisfy multiple conditions
    @Test
    void allEql() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<> ();
        (User::getName, "Tom");
        (User::getAge, 18);
        
        (user);
    }
}

If there are multiple conditions that need to be judged at the same time, we can put these multiple conditions into a Map collection, which is much more convenient


allEq(Map<R, V> params, boolean null2IsNull)
Parameters params: the set of Map to be passed.
Parameter null2IsNull: indicates whether to judge isNull for null condition.

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {

    public class QueryTest { @Autowired
    private UserMapper userMapper;

    // Query the field if the attribute value is null; do not query the field if the attribute value is not Null.
    @Test
    void allEq2() {

        HashMap<String, Object> hashMap = new HashMap<> ();

        ("name", "Tom");
        ("age", 18);
        //("age", null); // Attribute values that are null are not queried.

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        //(hashMap, false); // // A property value of null will not be queried.
        (hashMap, true); // for null, (name = ? AND age IS NULL) Query

        User user = (userQueryWrapper); // If the value of a property is null, it will not be queried.
        (user);
    }
}

3.3 ne (not equal)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {

    public class QueryTest { @Autowired
    private UserMapper userMapper;

    // not equal to a numeric value
    @Test
    void ne() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<> ();
        (User::getName, "Tom");
        List<User> users = (lambdaQueryWrapper); (User::getName, "Tom"); (Users::getName, "Tom")
        (users);

    }
}

4. Scope queries

4.1 gt( > greater than range)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {

    public class QueryTest { @Autowired
    private UserMapper userMapper;

    // > Query
    @Test
    void gt() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer age = 18;
        (User::getAge, age);;
        List<User> users = (lambdaQueryWrapper).
        (users);
    }
}

4.2 ge(>= greater than or equal to)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {

    public class QueryTest { @Autowired
    private UserMapper userMapper;

    // >= Query
    @Test
    void ge() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer age = 18;
        (User::getAge, age);;
        List<User> users = (lambdaQueryWrapper).
        (users);
    }
}

4.3 lt(< range less than)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {

    public class QueryTest { @Autowired
    private UserMapper userMapper;


    // <
    @Test
    void lt() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer age = 20;
        (User::getAge, age);;
        List<User> users = (lambdaQueryWrapper).
        (users);
    }
}

4.4 le (range less than or equal to)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {

    public class QueryTest { @Autowired
    private UserMapper userMapper;

    // <=
    @Test
    void le() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer age = 20;
        LambdaQueryWrapper<User> lambdaQueryWrapper1 = (User::getAge, age);

        List<User> users = (lambdaQueryWrapper).
        (users);
    }
}

4.5 between (in the range of the interval)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {

    public class QueryTest { @Autowired
    private UserMapper userMapper;

    // Between scopes
    @Test
    void between() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<> ();
        (User::getAge, 10, 20);
        List<User> users = (lambdaQueryWrapper); (User::getAge, 10, 20); (User::getAge, 10, 20)
        (users);

    }
}

4.6 notBetween (not in the range of the interval)

在这里插入图片描述

import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {

    public class QueryTest { @Autowired
    private UserMapper userMapper;

    // Out of range: (age NOT BETWEEN ? AND ?)
    @Test
    void notBetween() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getAge,10,18).
        List<User> users = (lambdaQueryWrapper).
        (users);
    }
}

5. Fuzzy queries

5.1 like( % value %)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {

    public class QueryTest { @Autowired
    private UserMapper userMapper;

    // Fuzzy query: %J%(String)
    @Test
    void like() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<> ();
        (User::getName, "J").
        List<User> users = (lambdaQueryWrapper).
        (users);
    }
}

5.2 notLike (does not satisfy % value %)

在这里插入图片描述

5.3 likeLeft (% value)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import ;import ;import ;import ;import
import ;


@SpringBootTest // Note that this test must have a scenario class or it will not run. If the test has a different class, you also need to specify the
// Package
public class QueryTest {

    public class QueryTest { @Autowired
    private UserMapper userMapper;

    // Fuzzy query: %e(String) left
    @Test
    void likeft() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getName, "e").
        List<User> users = (lambdaQueryWrapper).
        (users);

    }
}

5.4 likeRight (value %)

在这里插入图片描述

6. Empty queries

6.1 isNUll(determine if it is Null)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest02 {
    @Resource
   private UserMapper userMapper;

    // Determine if the null WHERE (name IS NULL)
    @Test
    void isNUll3() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getName);
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

6.2 isNotNull

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest02 {
    @Resource
    private UserMapper userMapper;

    //  WHERE (name IS NULL)
    @Test
    void isNotNull() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getName);
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

7. Inclusion of queries

7.1 in (the field containing the content)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest02 {
    @Resource
    private UserMapper userMapper;

    // field = (be) worth or field = (be) worth ->in
    // r WHERE (age IN (?,?,?))
    @Test
    void in() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        ArrayList<Object> arrayList = new ArrayList<>();
        (arrayList, 18, 20, 22);
        (User::getAge, arrayList);
        List<User> users = (lambdaQueryWrapper);
        (users);

    }
}

7.2 notIn (fields that do not contain this content)

在这里插入图片描述



import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest02 {
    @Resource
    private UserMapper userMapper;

    // field!=(be) worth and field!=(be) worth ->not in
    // WHERE (age NOT IN (?,?,?))
    @Test
    void notIn() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        ArrayList<Integer> arrayList = new ArrayList<>();
        (arrayList, 18, 20, 22);
        (User::getAge, arrayList);
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

7.3 inSql (the field containing the content)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest02 {
    @Resource
    private UserMapper userMapper;

    // er WHERE (age IN (18,20,22))
    @Test
    void inSql() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getAge, "18,20,22");
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

7.4 notInSql (fields that do not contain this content)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest02 {
    @Resource
    private UserMapper userMapper;

    // age NOT IN (18,20,22))
    @Test
    void notInsql() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getAge, "18,20,22");
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

在这里插入图片描述



import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest02 {
    @Resource
    private UserMapper userMapper;


    // RE (age NOT IN (select age from rainbowsea_user where age > 20))
    @Test
    void notInSql2() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getAge, "select age from rainbowsea_user where age > 20");
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

8. Grouping queries

8.1 groupBy (grouped fields)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;

    // select age,count(*) as field_count from rainbowsea_user group by age;
    // grouping inquiry
    @Test
    void groupBy() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        // grouping field
        ("age");
        // query field
        ("age,count(*) as field_count");
        List<Map<String, Object>> maps = (queryWrapper);
        (maps);
    }
}

9. Aggregation queries

9.1 having (aggregated fields)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;

    // select age,count(*) as field_count from rainbowsea_user group by age HAVING field_count >=2;
    // aggregate search
    @Test
    void having() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        // query field
        ("age,count(*) as field_count");
        // Polymerization Criteria Filtering
        ("field_count = 1");
        List<Map<String, Object>> maps = (userQueryWrapper);
        (maps);
    }
}

10. Sorting queries

10.1 orderByAsc (ascending order)

在这里插入图片描述

import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;



@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;

    // descending order
    @Test
    void orderByAsc() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getAge, User::getId);
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

10.2 orderByDesc (descending order)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;



@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;

    // ascending order
    @Test
    void orderByDesc() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getAge, User::getId);
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

10.3 orderBy (multi-field sort definition)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;

import . *; ; import ; import ; import .
import ;
import ;
import ;





@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;

    @Test
    void orderBy() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // Set the sort field and the way to sort it
        // Parameter 1: if the value of the sort field is null, whether it should be used as a sort field or not
        // Parameter 2: Whether to sort in ascending order.
        // Parameter 3: The sort field
        // (true, true, User::getAge); (false, true, User::getAge).
        (false, true, User::getAge).
        (true, false, User::getId); (true, false, User::getId).
        List<User> users = (lambdaQueryWrapper).
        (users);
    }
}

11. func queries

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;



@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;

    // // May choose to splice different query conditions depending on different please situations
    @Test
    void func() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        // May choose to splice different query conditions depending on different please situations
      /* (new Consumer<LambdaQueryWrapper<User>>() {
            @Override
            public void accept(LambdaQueryWrapper<User> userLambdaQueryWrapper) {
                //if (true) {
                if (false) {
                    (User::getId,1);
                } else {
                    (User::getId,1);
                }
            }
        });*/

        // utilizationlambaddisplayed formula
        (userLambdaQueryWrapper -> {
            if (false) {
                (User::getId, 1);
            } else {
                (User::getId, 1);
            }
        });
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

12. Logical queries

12.1 and (with)

在这里插入图片描述



import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;



@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;

    //  WHERE (age > ? AND age < ?)
    @Test
    void and() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getAge, 22).lt(User::getAge, 30);
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;



@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;


    // WHERE (name = ? AND (age > ? OR age < ?))
    @Test
    void add2() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getName, "wang").and(i -> (User::getAge, 26).or().lt(User::getAge, 22));
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

12.2 or (or)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;

    //  WHERE (age < ? AND age > ?)
    @Test
    void or() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getAge, 20).gt(User::getAge, 23);// age < 20 || age >=23
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

12.3 nested (not)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;

    //  WHERE ((name = ? AND age <> ?))
    @Test
    void nested() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (i->(User::getName,"Tom").ne(User::getAge,22));
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

13. Customized conditional queries

13.1 apply (custom query conditions)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;

    // Customized Conditional Queries
    @Test
    void apply() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        ("id = 1");
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

14. last query (splice "string" at the end of the sql statement)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;
    // Finally add character splicing
    // SELECT id,name,age,email,`desc` FROM rainbowsea_user limit 0,2
    @Test
    void last() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        ("limit 0,2");
        List<User> users = (lambdaQueryWrapper);
        (users);
    }

}

15. exists query

15.1 exists (existence query)

在这里插入图片描述


@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;

    // SELECT * from rainbowsea_user WHERE EXISTS (select id FROM rainbowsea_user WHERE age = 18);
    // SELECT * from rainbowsea_user WHERE EXISTS (select id FROM rainbowsea_user WHERE age = 10);

    @Test
    void exists() {
        // 1. establishQueryWrapperboyfriend
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 2. Building Queries
        //("select id FROM rainbowsea_user WHERE age = 18");
        ("select id FROM rainbowsea_user WHERE age = 10");
        // 3.consult (a document etc)
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

15.2 notExists (non-existent queries)

在这里插入图片描述


import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;


    // SELECT * from rainbowsea_user WHERE not EXISTS (select id FROM rainbowsea_user WHERE age = 10);
    @Test
    void notExists() {
        // 1. establishQueryWrapperboyfriend
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 2. Building Queries
        //("select id FROM rainbowsea_user WHERE age = 18");
        ("select id FROM rainbowsea_user WHERE age = 10");
        // 3.consult (a document etc)
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
}

16. Field queries

16.1 select (field queries)

在这里插入图片描述



import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;

@SpringBootTest
public class QueryTest03 {

    @Autowired
    private UserMapper userMapper;


    @Test
    void select() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        (User::getId,User::getName);
        List<User> users = (lambdaQueryWrapper);
        (users);
    }
    
}

17. Finally:

"In this final chapter, I would like to express my gratitude to each and every one of my readers. Your attention and responses have been a source of motivation for my creative endeavors, and I have drawn endless inspiration and courage from you. I will keep your encouragement in my heart and continue to struggle in other areas. Thanks to you all, we will always meet again at some point."

在这里插入图片描述