MyBatis-Plus Advanced Query Cases and Detailed Explanations

This article presents a series of MyBatis-Plus advanced query examples, including how to filter users by name patterns, age ranges, and email presence, and demonstrates ordering and combined conditions with complete Java test code and a sample User table schema.

Wukong Talks Architecture
Wukong Talks Architecture
Wukong Talks Architecture
MyBatis-Plus Advanced Query Cases and Detailed Explanations

The article introduces advanced query capabilities of MyBatis-Plus, following a brief overview of environment setup and basic queries.

A sample User table is defined with columns id, name, age, and email.

Case 1: Find users whose name contains "Ja" and age is less than 30

SQL condition:

name LIKE '%Ja%' AND age < 30
/*
 * Description: Example 1.1 – Query users with name containing "Ja" and age < 30
 */
@Test
public void testSelectByQueryWrapper() {
    System.out.println("----- Query users with name containing \"Ja\" and age < 30 -----");
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.like("name", "ja").lt("age", 30);
    List<User> userList = userMapper.selectList(queryWrapper);
    userList.forEach(System.out::println);
}

Case 2: Find users whose name contains "a", age between 15 and 25, and email is not null

SQL condition:

name LIKE '%a%' AND age BETWEEN 15 AND 25 AND email IS NOT NULL
/*
 * Description: Example 1.2 – Query users with name containing "a", age 15‑25, email not null
 */
@Test
public void testSelectByQueryWrapper2() {
    System.out.println("----- Query users with name containing \"a\", age 15‑25, email not null -----");
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.like("name", "a").between("age", 15, 25).isNotNull("email");
    List<User> userList = userMapper.selectList(queryWrapper);
    userList.forEach(System.out::println);
}

Case 3: Find users whose name starts with "J", age ≥ 25, ordered by age descending then id ascending

SQL condition:

name LIKE 'J%' AND age >= 25 ORDER BY age DESC, id ASC
/*
 * Description: Example 1.3 – Query users with name starting with "J", age ≥ 25, ordered by age desc, id asc
 */
@Test
public void testSelectByQueryWrapper3() {
    System.out.println("----- Query users with name starting with \"J\", age ≥ 25, ordered -----");
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.likeRight("name", "J").or().gt("age", 26)
                .orderByDesc("age").orderByAsc("id");
    List<User> userList = userMapper.selectList(queryWrapper);
    userList.forEach(System.out::println);
}

The source code repository can be downloaded from https://github.com/Jackson0714/study-mybatis-plus.git . The article also includes promotional messages encouraging readers to follow the author's public account for additional resources.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaSQLORMmybatis-plusQueryWrapper
Wukong Talks Architecture
Written by

Wukong Talks Architecture

Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.