Advanced Query Examples with MyBatis-Plus: Case Studies and Code Walkthrough

This tutorial showcases MyBatis-Plus advanced query techniques through three practical case studies, providing sample data, detailed SQL conditions, and complete Java test code snippets to help developers master complex database queries.

Wukong Talks Architecture
Wukong Talks Architecture
Wukong Talks Architecture
Advanced Query Examples with MyBatis-Plus: Case Studies and Code Walkthrough

This article presents a series of MyBatis-Plus advanced query case studies, including setup instructions, sample data, and three specific query scenarios.

Scenario 1 demonstrates selecting users whose name contains “Ja” and age is less than 30 using like and lt conditions.

/**
 * 描述:例2.1 查询名字中包含“Ja”并且年龄小于30的用户
 * 作者:博客园-悟空聊架构
 * 时间:2019-01-20
 * Github:https://github.com/Jackson0714/study-mybatis-plus.git
 * 博客园:https://www.cnblogs.com/jackson0714
 */
@Test
public void testSelectByQueryWrapper() {
    System.out.println("----- 查询名字中包含“Ja”并且年龄小于30的用户------");
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.like("name", "ja").lt("age", 30);
    List<User> userList = userMapper.selectList(queryWrapper);
    userList.forEach(System.out::println);
}

Scenario 2 uses like, between, and isNotNull to find users whose name contains “a”, age between 15 and 25, and non‑null email.

/**
 * 描述:例1.2 查询名字中包含“a”并且年龄大于等于15且年龄小于等于35,且email不为空
 * 作者:博客园-悟空聊架构
 * 时间:2019-01-20
 * Github:https://github.com/Jackson0714/study-mybatis-plus.git
 * 博客园:https://www.cnblogs.com/jackson0714
 */
@Test
public void testSelectByQueryWrapper2() {
    System.out.println("----- 查询名字中包含“a”并且年龄大于等于15且年龄小于等于25,且email不为空------");
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    //queryWrapper.like("name", "a").ge("age", 15).le("age", 25).isNotNull("email");
    queryWrapper.like("name", "a").between("age", 15, 25).isNotNull("email");
    List<User> userList = userMapper.selectList(queryWrapper);
    userList.forEach(System.out::println);
}

Scenario 3 illustrates using likeRight, or, gt, and ordering by age descending then id ascending to retrieve users whose name starts with “J” and age greater than 26.

/**
 * 描述:例1.3 查询名字中“J”开头并且年龄大于26,按照年龄降序排列,年龄相同按照id升序排列
 * 作者:博客园-悟空聊架构
 * 时间:2019-01-20
 * Github:https://github.com/Jackson0714/study-mybatis-plus.git
 * 博客园:https://www.cnblogs.com/jackson0714
 */
@Test
public void testSelectByQueryWrapper3() {
    System.out.println("----- 查询名字中包含“a”并且年龄大于26,按照年龄降序排列,年龄相同按照id升序排列 ------");
    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 article also provides a link to the GitHub repository for downloading the full example code and encourages readers to follow the “悟空聊架构” public account for more architecture 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.

JavaSQLBackend DevelopmentORMMyBatis-PlusAdvanced Queries
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.