mybatisPlus实现多表连接分页动态查询

文章展示了在Java项目中使用MyBatisPlus进行数据查询的操作,包括实体类PointInfo的定义,包含数据库表字段注解,以及对应的VO类PointInfoVo,用于业务层的数据封装。mapper接口中定义了不同查询方法,结合QueryWrapper处理查询条件。最后在serviceImpl类中实现了分页查询功能,通过QueryWrapper构建查询条件并调用mapper接口获取数据。
摘要由CSDN通过智能技术生成

实体类:

package com.byx.communitycontrol.common.entity.baseinfo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;

/**
 * 点位信息
 **/
@Data
@TableName(value = "T_DWXX")
public class PointInfo implements Serializable {

    /**
     * 相机编码
     */
    @TableField(value = "CAMERAINDEXCODE")
    private String cameraIndexCode;

    /**
     * 点位名称
     */
    @TableField(value = "NAME")
    private String name;

    /**
     * 区域编码
     */
    @TableField(value = "UNITINDEXCODE")
    private String unitIndexCode;

    /**
     * 国标编码
     */
    @TableField(value = "GBINDEXCODE")
    private String gbIndexCode;

    /**
     * 设备编码
     */
    @TableField(value = "DEVICEINDEXCODE")
    private String deviceIndexCode;

    /**
     * 纬度
     */
    @TableField(value = "LATITUDE")
    private String latitude;

    /**
     * 经度
     */
    @TableField(value = "LONGITUDE")
    private String longitude;

    /**
     * 高度
     */
    @TableField(value = "ALTITUDE")
    private String altitude;

    /**
     * 像素
     */
    @TableField(value = "PIXEL")
    private Integer pixel;

    /**
     * 相机类别
     */
    @TableField(value = "CAMERATYPE")
    private Integer cameraType;

    /**
     * 安装位置
     */
    @TableField(value = "INSTALLPLACE")
    private String installPlace;

    /**
     * 矩阵编号
     */
    @TableField(value = "MATRIXCODE")
    private String matrixCode;

    /**
     * 频道号
     */
    @TableField(value = "CHANNUM")
    private Integer chanNum;

    /**
     * 查看棚
     */
    @TableField(value = "VIEWSHED")
    private String viewShed;

    /**
     * 能力集
     */
    @TableField(value = "CAPABILITYSET")
    private String capabilitySet;

    /**
     * 智能套装
     */
    @TableField(value = "INTELLIGENTSET")
    private String intelligentSet;


    /**
     * 记录位置
     */
    @TableField(value = "RECORDLOCATION")
    private String recordLocation;

    /**
     * 云台控制
     */
    @TableField(value = "PTZCONTROLLER")
    private String ptzController;

    /**
     * 设备资源类型
     */
    @TableField(value = "DEVICERESOURCETYPE")
    private String deviceResourceType;

    /**
     * 信道类型
     */
    @TableField(value = "CHANNELTYPE")
    private String channelType;

    /**
     * 转换类型
     */
    @TableField(value = "TRANSTYPE")
    private Integer transType;


    /**
     * 更新时间
     */
    @TableField(value = "UPDATETIME")
    private String updateTime;

    /**
     * 创建时间
     */
    @TableField(value = "CREATETIME")
    private String createTime;

    /**
     * 状态
     */
    @TableField(value = "STATUS")
    private Integer status;

    /**
     * 相机类型名称
     */
    @TableField(value = "CAMERATYPENAME")
    private String cameraTypeName;

    /**
     * 能力集名称
     */
    @TableField(value = "CAPABILITYSETNAME")
    private String capabilitySetName;

    /**
     * 智能设置名称
     */
    @TableField(value = "INTELLIGENTSETNAME")
    private String intelligentSetName;

    /**
     * 记录位置名称
     */
    @TableField(value = "RECORDLOCATIONNAME")
    private String recordLocationName;

    /**
     * 云台控制名称
     */
    @TableField(value = "PTZCONTROLLERNAME")
    private String ptzControllerName;

    /**
     * 设备资源类型名称
     */
    @TableField(value = "DEVICERESOURCETYPENAME")
    private String deviceResourceTypeName;

    /**
     *
     * 频道类型名称
     */
    @TableField(value = "CHANNELTYPENAME")
    private String channelTypeName;
    /**
     * 转换类型名称
     */
    @TableField(value = "TRANSTYPENAME")
    private String transTypeName;
    /**
     * 条约类型
     */
    @TableField(value = "TREATYTYPE")
    private String treatyType;
    /**
     * 条约类型名称
     */
    @TableField(value = "TREATYTYPENAME")
    private String treatyTypeName;
    /**
     * 状态名称
     */
    @TableField(value = "STATUSNAME")
    private String statusName;
}

VO类:

package com.byx.communitycontrol.common.vo;

import com.byx.communitycontrol.common.entity.baseinfo.PointInfo;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

@Data
@EqualsAndHashCode(callSuper = true)
public class PointInfoVo extends PointInfo implements Serializable {

    /**
     * 相机名称
     */
    private String cameraName;

    /**
     * 区域名称
     */
    private String qyName;

    /**
     * IP
     */
    private String complexIp;

    /**
     * 上级IP
     */
    private String parentIp;

}

mapper接口:

package com.byx.communitycontrol.gm.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.byx.communitycontrol.common.entity.baseinfo.PointInfo;
import com.byx.communitycontrol.common.vo.PointInfoVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;


@Mapper
public interface PointInfoMapper extends BaseMapper<PointInfo> {


    
    //常规写法
    @Select("SELECT\n" +
            "\td.CAMERAINDEXCODE,\n" +
            "\td.NAME,\n" +
            "\td.UNITINDEXCODE,\n" +
            "\td.GBINDEXCODE,\n" +
            "\td.DEVICEINDEXCODE,\n" +
            "\tqd.CAMERANAME,\n" +
            "\tqx.NAME AS qyname,\n" +
            "\td.STATUS,\n" +
            "\td.STATUSNAME,\n" +
            "\td.LATITUDE,\n" +
            "\td.LONGITUDE,\n" +
            "\td.ALTITUDE,\n" +
            "\ti.COMPLEXIP,\n" +
            "\ti.PARENTIP,\n" +
            "\td.CREATETIME,\n" +
            "\td.UPDATETIME \n" +
            "FROM\n" +
            "\tT_DWXX d\n" +
            "\tLEFT JOIN T_DWXXIP i ON d.CAMERAINDEXCODE = i.CAMERAINDEXCODE\n" +
            "\tLEFT JOIN T_QY_DW qd ON d.CAMERAINDEXCODE = qd.CAMERAINDEXCODE\n" +
            "\tLEFT JOIN T_QYXX qx ON d.UNITINDEXCODE = qx.INDEXCODE \n" +
            "${ew.customSqlSegment} " +
            "ORDER BY\n" +
            "\td.UPDATETIME DESC")
    IPage<PointInfoVo> getPointInfoPageList(Page<?> page, @Param("ew") Wrapper wrapper);

    //可返回一个List集合,之后再用page.setRecords()方法将集合set进去
    @Select("SELECT\n" +
            "\td.CAMERAINDEXCODE,\n" +
            "\td.NAME,\n" +
            "\td.UNITINDEXCODE,\n" +
            "\td.GBINDEXCODE,\n" +
            "\td.DEVICEINDEXCODE,\n" +
            "\tqd.CAMERANAME,\n" +
            "\tqx.NAME AS qyname,\n" +
            "\td.STATUS,\n" +
            "\td.STATUSNAME,\n" +
            "\td.LATITUDE,\n" +
            "\td.LONGITUDE,\n" +
            "\td.ALTITUDE,\n" +
            "\ti.COMPLEXIP,\n" +
            "\ti.PARENTIP,\n" +
            "\td.CREATETIME,\n" +
            "\td.UPDATETIME \n" +
            "FROM\n" +
            "\tT_DWXX d\n" +
            "\tLEFT JOIN T_DWXXIP i ON d.CAMERAINDEXCODE = i.CAMERAINDEXCODE\n" +
            "\tLEFT JOIN T_QY_DW qd ON d.CAMERAINDEXCODE = qd.CAMERAINDEXCODE\n" +
            "\tLEFT JOIN T_QYXX qx ON d.UNITINDEXCODE = qx.INDEXCODE \n" +
            "${ew.customSqlSegment} " +
            "ORDER BY\n" +
            "\td.UPDATETIME DESC")
    List<PointInfoVo> getPointInfoPageList(Page<?> page, @Param("ew") Wrapper wrapper);


    //可用实体属性代替查询元素
    @Select("SELECT\n" +
            "\td.*,\n" +
            "\tqd.cameraName,\n" +
            "\tqx.name AS qyName,\n" +
            "\ti.complexIp,\n" +
            "\ti.parentIp \n" +
            "FROM\n" +
            "\tT_DWXX d\n" +
            "\tLEFT JOIN T_DWXXIP i ON d.cameraIndexCode = i.cameraIndexCode\n" +
            "\tLEFT JOIN T_QY_DW qd ON d.cameraIndexCode = qd.cameraIndexCode\n" +
            "\tLEFT JOIN T_QYXX qx ON d.unitIndexCode = qx.indexCode \n" +
            "${ew.customSqlSegment} " +
            "ORDER BY\n" +
            "\td.updateTime DESC")
    IPage<PointInfoVo> getPointInfoPageList(Page<?> page, @Param("ew") Wrapper wrapper);
}

serviceImpl类:

package com.byx.communitycontrol.gm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.byx.communitycontrol.common.entity.AppPage;
import com.byx.communitycontrol.common.entity.baseinfo.PointInfo;
import com.byx.communitycontrol.common.request.PointInfoPageReq;
import com.byx.communitycontrol.common.vo.PointInfoVo;
import com.byx.communitycontrol.context.helper.MyBatisPlusPageImpl;
import com.byx.communitycontrol.gm.mapper.PointInfoMapper;
import com.byx.communitycontrol.gm.service.IPointInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service
public class PointInfoServiceImpl extends ServiceImpl<PointInfoMapper, PointInfo> implements IPointInfoService {

    @Resource
    private PointInfoMapper pointInfoMapper;

    @Override
    public AppPage<PointInfoVo> findListByPageCondition(PointInfoPageReq request) {
        Page<PointInfoVo> page = new Page(request.getCurrentPage(), request.getPageSize());

        //方法一:
        QueryWrapper<PointInfoVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(request.getDwName()), "name", request.getDwName());
        queryWrapper.like(StringUtils.hasText(request.getQyName()), "qyName", request.getQyName());
        queryWrapper.eq(request.getStatus() != null, "status", request.getStatus());
        List<PointInfoVo> pointInfoPageList = pointInfoMapper.getPointInfoPageList(page, queryWrapper);
        Page<PointInfoVo> pointInfoVoPage = page.setRecords(pointInfoPageList);

        //方法二:
        QueryWrapper<PointInfoVo> eq = Wrappers.query(new PointInfoVo()).like(StringUtils.hasText(request.getDwName()),
                "d.name",
                request.getDwName()).like(StringUtils.hasText(request.getQyName()), "qx.name",
                request.getQyName()).eq(request.getStatus() != null, "d.status", request.getStatus());
        IPage<PointInfoVo> pointInfoPageList = pointInfoMapper.getPointInfoPageList(page, eq);

        方法三:可将 Wrappers.query(new PointInfoVo()).like(...)... 换成 Wrappers.lambdaQuery(PointInfoVo.class).like(StringUtils.hasText(request.getDwName()),PointInfoVo::getName,
                request.getDwName())...

        return new MyBatisPlusPageImpl<>(pointInfoPageList);
    }
}

Mybatis-Plus提供了非常方便的多表查询分页的方法,可以通过Wrapper构造器实现。 假设我们有两个表t_user和t_order,t_user中有user_id和username两个字段,t_order中有order_id和user_id两个字段,我们需要查询t_user中username包含"张三"、t_order中user_id为1的订单,并且按照order_id排序。可以通过如下代码实现: ```java // 创建分页对象 IPage<Order> page = new Page<>(1, 10); // 构造查询条件 QueryWrapper<Order> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("user_id", 1) .orderByAsc("order_id"); // 执行查询 List<Order> orders = orderMapper.selectPage(page, queryWrapper).getRecords(); ``` 如果需要关联查询t_user表中的用户信息,可以使用左连接关联查询,如下: ```java // 创建分页对象 IPage<OrderVo> page = new Page<>(1, 10); // 构造查询条件 QueryWrapper<OrderVo> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("o.user_id", 1) .like("u.username", "张三%") .orderByAsc("o.order_id"); // 执行查询 List<OrderVo> orderVos = orderMapper.selectOrderVoPage(page, queryWrapper); ``` 其中,OrderVo是一个自定义的实体类,用于接收关联查询的结果,可以通过@TableName指定对应的表名,@TableField指定对应的字段名。同时,需要在OrderMapper中定义selectOrderVoPage方法,实现关联查询,如下: ```java List<OrderVo> selectOrderVoPage(IPage<OrderVo> page, @Param(Constants.WRAPPER) Wrapper<OrderVo> wrapper); ``` 需要注意的是,OrderVo中的字段名需要与查询条件中的别名一致,否则查询结果会为null。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值