MyBatis-Plus Advanced Query Examples and Explanations
This article presents a series of MyBatis-Plus advanced query cases, including data preparation, detailed SQL and Java QueryWrapper examples, difficulty ratings, and complete code snippets to help developers master complex query operations in a Java backend environment.
Case Summary (First Wave)
1.1 Query users whose name contains "Ja" and age is less than 30
Difficulty: ★☆
1.2 Query users whose name contains "a", age between 15 and 25, and email is not null
Difficulty: ★☆
1.3 Query users whose name starts with "J", age >= 25, ordered by age descending and id ascending
Difficulty: ★★★
Case Explanation
1.1 Query users whose name contains "Ja" and age is less than 30
Focus: like and lt usage.
SQL:
name LIKE '%Ja%' AND age < 30 /*
* Description: Example 2.1 – Query users whose name contains "Ja" and age is less than 30
* Author: Blog - Wukong Architecture
* Date: 2019-01-20
*/
@Test
public void testSelectByQueryWrapper() {
System.out.println("----- Query users whose name contains \"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);
}1.2 Query users whose name contains "a", age between 15 and 25, and email is not null
Focus: between and isNotNull usage.
SQL:
name LIKE '%a%' AND age BETWEEN 15 AND 25 AND email IS NOT NULL /*
* Description: Example 1.2 – Query users whose name contains "a", age between 15 and 25, email not null
* Author: Blog - Wukong Architecture
* Date: 2019-01-20
*/
@Test
public void testSelectByQueryWrapper2() {
System.out.println("----- Query users whose name contains \"a\" and 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);
}1.3 Query users whose name starts with "J", age >= 25, ordered by age descending then id ascending
Focus: likeRight, orderByDesc, orderByAsc usage.
SQL:
name LIKE 'J%' AND age >= 25 ORDER BY age DESC, id ASC /*
* Description: Example 1.3 – Query users whose name starts with "J", age > 26, ordered by age desc, id asc
* Author: Blog - Wukong Architecture
* Date: 2019-01-20
*/
@Test
public void testSelectByQueryWrapper3() {
System.out.println("----- Query users whose name starts with \"J\" and age > 26, ordered by age desc, id asc -----");
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);
}Data Preparation
The User table structure:
id
name
age
1
Jone
18
2
Jack
20
3
Tom
28
4
Sandy
21
5
Billie
24
Code repository: https://github.com/Jackson0714/study-mybatis-plus.git
Follow the public account "Wukong Architecture" for more daily technical tips and resources.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
