Backend Development 3 min read

Object‑Oriented Join Queries in MyBatis‑Plus Using the mybatis‑plus‑join Library

The article explains how to replace hand‑written SQL join statements in MyBatis with an object‑oriented approach by adding the mybatis‑plus‑join library, configuring the Maven dependency, and writing concise Java code that leverages MPJQueryWrapper for clear and error‑free join queries.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Object‑Oriented Join Queries in MyBatis‑Plus Using the mybatis‑plus‑join Library

In MyBatis, developers often write raw SQL for join queries, but the author prefers an object‑oriented solution.

Because MyBatis‑Plus does not directly support such joins, the mybatis-plus-join library can be used. Add the following Maven dependency to the project:

<dependency>
    <groupId>com.github.yulichang</groupId>
    <artifactId>mybatis-plus-join</artifactId>
    <version>...</version>
</dependency>

After adding the dependency, write Java code that builds a query with MPJQueryWrapper , selects fields, and performs a left join without writing raw SQL:

class Test {
    @Resource
    private UserMapper userMapper;

    void testJoin() {
        MPJQueryWrapper<UserDO> wrapper = new MPJQueryWrapper<UserDO>()
            .selectAll(UserDO.class)
            .select(UserAddrDO::getTel, UserAddrDO::getAddress)
            .leftJoin(UserDO.class, UserDO::getId, UserAddrDO::getUserId)
            .eq(UserAddrDO::getTel, "18888888888");
        // Execute query
        List<UserDTO> list = userMapper.selectJoinList(UserDTO.class, wrapper);
    }
}

This approach keeps the code structure clear, avoids typographical errors common in hand‑written SQL, and showcases the benefits of an object‑oriented query style.

BackendJavaORMMyBatis-Plusobject-orientedJoin Query
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

login 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.