Mybatis-plus-Join--分页查询

发布于:2024-12-20 ⋅ 阅读:(15) ⋅ 点赞:(0)

数据表四张:

user: id,username,create_time,update_time

product: id,name,price,number(库存),create_time,update_times

order: id,quantity,order_time(下单时间),update_time

order_detail:id,product_id,order_id,quantity,price,create_time,update_time

1、UserService接口:

List<UserDTO> selectAll(int pageNum, int pageSize);

 2、封装的UserDTO类(想在前端展示什么内容,就封装什么属性)

 

package com.xxxx.DTO;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.xxxx.entity.OrderDetail;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;
@Data
public class OrderDTO {

    @ApiModelProperty(value = "订单ID")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty(value = "订单数量")
    private Integer quantity;

    @ApiModelProperty(value = "下单时间")
    @TableField(fill = FieldFill.INSERT ,value = "order_time")
    private LocalDateTime orderTime;

    @ApiModelProperty(value = "修改时间")
    @TableField(fill = FieldFill.INSERT_UPDATE ,value = "update_time")
    private LocalDateTime updateTime;


    @ApiModelProperty(value = "用户id")
    @TableField(value = "user_id")
    private Integer userId;

    /**
     * 订单和订单详情是一对多
     */
    private List<OrderDetail> orderDetailList;

3、重点:UserServiceImpl 中的实现逻辑

注入:

@Autowired
private UserMapper userMapper;
 @Override
    public List<UserDTO> selectAll(int pageNum, int pageSize) {
        //用户: 订单   1: n
        //订单 : 订单详情 1: n
        //订单详情 : 商品 1: 1
        MPJLambdaWrapper<User> wrapper=new MPJLambdaWrapper<>(User.class)
                .selectAll(User.class)
                //用户: 订单   1: n
                .selectCollection(OrderDetail.class,UserDTO::getOrderDetailList)
                .leftJoin(Order.class,Order::getUserId,User::getId)
                .leftJoin(OrderDetail.class,OrderDetail::getOrderId,Order::getId);
        return userMapper.selectJoinList(UserDTO.class,wrapper);

    }

 4、UserControler

    @Resource
    private UserService userService;

    /**
     * 分页查询所有用户及详情
     */
    @PostMapping("selectAllPage")
    public List<UserDTO> selectByPage(int pageNum, int pageSize){
        return userService.selectAll(pageNum,pageSize);
    }

5、再PostMan中测试

访问路径:http://localhost:9999/springboot_mp/selectAllPage?pageNum=1&pageSize=3

[
    {
        "id": 1,
        "username": "张三",
        "orderDetailList": [
            {
                "id": 1,
                "productId": 1,
                "orderId": 1,
                "quantity": 3,
                "price": 3000.00,
                "createTime": "2024-12-17T15:24:20",
                "updateTime": "2024-12-17T15:24:20"
            },
            {
                "id": 2,
                "productId": 3,
                "orderId": 1,
                "quantity": 1,
                "price": 2300.00,
                "createTime": "2024-12-17T15:24:20",
                "updateTime": "2024-12-17T15:24:20"
            },
            {
                "id": 5,
                "productId": 1,
                "orderId": 3,
                "quantity": 9,
                "price": 3000.00,
                "createTime": "2024-12-17T15:25:55",
                "updateTime": "2024-12-17T15:25:55"
            },
            {
                "id": 6,
                "productId": 3,
                "orderId": 3,
                "quantity": 10,
                "price": 2300.00,
                "createTime": "2024-12-17T15:25:55",
                "updateTime": "2024-12-17T15:25:55"
            }
        ]
    }
]