Configure Mapper to use MPJBaseMapper
masterMPJBaseMapper instead of the standard MyBatis-Plus BaseMapper.repository·master·Indexed 23 days ago
https://github.com/yulichang/mybatis-plus-joinAn extension for MyBatis-Plus that simplifies multi-table join queries using a type-safe Lambda-based wrapper. It provides MPJLambdaWrapper for constructing complex SQL joins and requires mappers to extend MPJBaseMapper. Supports Spring Boot via mybatis-plus-join-boot-starter and Solon via mybatis-plus-join-solon-plugin.
MPJBaseMapper instead of the standard MyBatis-Plus BaseMapper.You can install MyBatis-Plus-Join using Maven or Gradle. For Spring Boot projects, use the mybatis-plus-join-boot-starter artifact.
<dependency>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join-boot-starter</artifactId>
<version>1.5.9</version>
</dependency>implementation 'com.github.yulichang:mybatis-plus-join-boot-starter:1.5.9'To enable the join capabilities provided by mybatis-plus-join in your Solon application, your Mapper interfaces must extend MPJBaseMapper<T> instead of the standard MyBatis-Plus BaseMapper<T>.
import com.github.yulichang.base.MPJBaseMapper;
@Mapper
public interface UserMapper extends MPJBaseMapper<UserDO> {
}To use the Solon plugin for mybatis-plus-join, add the following dependency to your Maven pom.xml. Replace lastVersion with the appropriate version for your project.
<dependency>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join-solon-plugin</artifactId>
<version>lastVersion</version>
</dependency>If you encounter the Invalid bound statement (not found) error, you must configure the MPJSqlInjector in your MyBatis configuration. Add the sqlInjector property under your database configuration in your YAML file.
mybatis.db1:
globalConfig:
sqlInjector: com.github.yulichang.injector.MPJSqlInjectorUse MPJLambdaWrapper for type-safe join queries. This allows you to select fields from multiple tables, define join conditions, and apply filters using Lambda expressions.
class test {
@Resource
private UserMapper userMapper;
void testJoin() {
MPJLambdaWrapper<UserDO> wrapper = JoinWrappers.lambda(UserDO.class)
.selectAll(UserDO.class)
.select(AddressDO::getTel)
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
.eq(UserDO::getId, 1)
.like(AddressDO::getTel, "1");
// Join query returning a custom ResultType
List<UserDTO> list = userMapper.selectJoinList(UserDTO.class, wrapper);
// Paginated join query (requires MyBatis-Plus pagination plugin to be enabled)
Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(2, 10), UserDTO.class, wrapper);
}
}Generated SQL equivalent:
SELECT
t.id, t.name, t.sex, t.head_img,
t1.tel
FROM user t
LEFT JOIN address t1 ON t1.user_id = t.id
WHERE ( t.id = ? AND t1.tel LIKE ? )class test {
@Resource
private UserMapper userMapper;
void testJoin() {
MPJLambdaWrapper<UserDO> wrapper = JoinWrappers.lambda(UserDO.class)
.selectAll(UserDO.class)
.select(AddressDO::getTel)
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
.eq(UserDO::getId, 1)
.like(AddressDO::getTel, "1");
// 连表查询 返回自定义ResultType
List<UserDTO> list = userMapper.selectJoinList(UserDTO.class, wrapper);
// 分页查询 (需要启用 mybatis plus 分页插件)
Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(2, 10), UserDTO.class, wrapper);
}
}Ensure your version of MyBatis-Plus is compatible with the version of MyBatis-Plus-Join you are using:
| Mybatis-Plus-Join | Mybatis-Plus |
|---|---|
| 1.5.9 | 3.5.17+ |
| 1.5.8 | 3.1.2 - 3.5.16 |