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.
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
queryWrapper = new QueryWrapper<>();
queryWrapper.like("name", "ja").lt("age", 30);
List
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
queryWrapper = new QueryWrapper<>();
queryWrapper.like("name", "a").between("age", 15, 25).isNotNull("email");
List
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
queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("name", "J").or().gt("age", 26)
.orderByDesc("age").orderByAsc("id");
List
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.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.