mybatisplus联合查询
时间: 2025-06-08 10:24:50 浏览: 3
### MyBatisPlus 多表联合查询使用教程及示例
MyBatisPlus 提供了多种方式来实现多表联合查询,其中 MPJ(MyBatis-Plus-Join)插件是一个非常强大的工具。以下是一个详细的使用方法和示例:
#### 1. 配置 MPJ 插件
在使用 MPJ 插件之前,需要先进行配置。可以通过引入依赖并注册拦截器来完成。
**Maven 依赖:**
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>com.github.yuweiguocn</groupId>
<artifactId>mpj-join</artifactId>
<version>1.3.8</version>
</dependency>
```
**Spring Boot 配置类:**
```java
package com.example.demo.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.github.yuweiguocn.mpj.extend.MybatisPlusJoinInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
// MPJ 插件
interceptor.addInnerInterceptor(new MybatisPlusJoinInterceptor());
return interceptor;
}
}
```
#### 2. 定义实体类
假设我们有两个表:`user` 和 `order`,分别对应用户表和订单表。
**User 实体类:**
```java
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("user")
public class User {
@TableId
private Long id;
private String name;
private Integer age;
}
```
**Order 实体类:**
```java
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("orders")
public class Order {
@TableId
private Long id;
private Long userId;
private String productName;
private Double amount;
}
```
#### 3. 使用 MPJLambdaWrapper 进行联合查询
MPJLambdaWrapper 是 MPJ 插件提供的一个工具类,用于简化多表联合查询的编写。
**示例代码:**
```java
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yuweiguocn.mpj.extend.MPJLambdaWrapper;
import com.example.demo.entity.User;
import com.example.demo.entity.Order;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl {
@Autowired
private UserMapper userMapper;
public List<User> getUserWithOrders() {
MPJLambdaWrapper<User> wrapper = new MPJLambdaWrapper<>();
wrapper.select(User::getId, User::getName) // 主表字段
.selectAs(Order::getProductName, "product_name") // 关联表字段
.leftJoin(Order.class, order -> order.eq(Order::getUserId, User::getId)) // 左连接
.orderByDesc(User::getId); // 排序
return userMapper.selectJoinList(wrapper);
}
public Page<User> getUserWithOrdersPage(Integer current, Integer size) {
Page<User> page = new Page<>(current, size);
MPJLambdaWrapper<User> wrapper = new MPJLambdaWrapper<>();
wrapper.select(User::getId, User::getName)
.selectAs(Order::getProductName, "product_name")
.leftJoin(Order.class, order -> order.eq(Order::getUserId, User::getId))
.orderByDesc(User::getId);
return userMapper.selectJoinPage(page, wrapper);
}
}
```
#### 4. 注意事项
- 确保主表的实体类型正确传递给 MPJLambdaWrapper[^2]。
- 在复杂查询中,注意 SQL 性能优化,避免不必要的字段加载[^2]。
- 增加单元测试以验证查询逻辑的正确性[^2]。
---
### 示例输出结果
假设数据库中有以下数据:
- 用户表:
| id | name | age |
|-----|--------|-----|
| 1 | Alice | 25 |
| 2 | Bob | 30 |
- 订单表:
| id | user_id | product_name | amount |
|-----|---------|--------------|--------|
| 101 | 1 | Book | 19.99 |
| 102 | 2 | Pen | 5.99 |
查询结果可能如下:
```json
[
{
"id": 1,
"name": "Alice",
"product_name": "Book"
},
{
"id": 2,
"name": "Bob",
"product_name": "Pen"
}
]
```
---
阅读全文
相关推荐
















