基于mybatis plus增加较复杂自定义查询以及分页

基于java技术,spring-boot和mybatis-plus搭建后台框架是现在非常流行的技术。

其中关于多表关联查询的实现相对比较复杂,在这里进行记录以供开发人员参考。

以一个系统中实际的实体类为查询为例,

复制代码
T3dMaterial实体其中的fileType属性及字段,关联到类型表T3dMaterityps对象的typ_id字段。

如果要实现查询T3dMaterial实体的同时也返回关联表T3dMaterityps对象的字段属性如fileTypeName。

1、修改T3dMaterial实体或者T3dMaterial实体 的Vo对象 ,在Mapper.xml 增加所关联的 第二个表对象属性fileTypeName 。

<resultMap type="com.ldcc.gis.domain.vo.T3dMaterialVo" id="T3dMaterialVoResult">
        <result property="id" column="id"/>
        <result property="filename" column="filename"/>
        <result property="fileType" column="file_type"/>
        <result property="fileTypeName" column="typ_name"/>
        <result property="fileipPort" column="fileip_port"/>
        <result property="filepath" column="filepath"/>
        <result property="bucketname" column="bucketname"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

2、修改Vo对象增加属性

    private String fileTypeName;

3、在Mapper.xml增加关联查询的select定义如

<select id="selectList2" parameterType="com.ldcc.gis.domain.bo.T3dMaterialBo" resultMap="T3dMaterialVoResult">
        SELECT a.id,a.filename,a.file_type,a.file_type_parent,a.fileip_port,a.filepath,a.bucketname,a.create_by,a.create_time,b.typ_name
        from t3d_material a
        INNER JOIN t3d_materityps b on a.file_type=b.typ_id
        <where>
        <if test="et.id != null and et.id != 0">and id = #{et.id} </if>
        <if test="et.filename != null and et.filename != ''">and a.filename LIKE CONCAT('%', #{et.filename}, '%')</if>
        <if test="et.fileType != null and et.fileType != 0">and a.file_type = #{et.fileType}</if>
        <if test="et.filepath != null and et.filepath != ''">and a.filepath LIKE CONCAT('%', #{et.filepath}, '%') </if>
        <if test="et.bucketname != null and et.bucketname != ''">and a.bucketname = #{et.bucketname}</if>
        </where>
    </select>

该select 的parameterType 对应T3dMaterial实体的Bo对象(根据各个框架要求,也可以是其他Bean,只要属性能对应),resultMap是第一步在Mapper.xml增加的resultMap。

该select 实现了,根据对象属性作为查询参数,同时实现了对象参数是否为null或者空的校验,实现了 LIKE 查询的典型用法如 and a.filename LIKE CONCAT('%', #{et.filename}, '%')

该select的where 条件中每个传入参数对象都以 et作为别名,带et.前缀。之所以et是依据com.baomidou.mybatisplus.core.toolkit 包的Constants.ENTITY="et"

4、在 Mapper接口增加对应查询方法,增加的代码片段如下:

import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldcc.gis.domain.T3dMaterial;
import com.ldcc.gis.domain.bo.T3dMaterialBo;
import com.ldcc.gis.domain.vo.T3dMaterialVo;

import org.apache.ibatis.annotations.Param;

Page<T3dMaterialVo> selectList2(Page<?> page, @Param(Constants.ENTITY) T3dMaterialBo bo);

该方法主要用到了Page对象,传入参数前面注解 @Param(Constants.ENTITY) 表示查询条件根据实体Bean构造,与Mapper.xml中的select 的parameterType 对应。

5、完成以上步骤,基本上在spring-boot和mybatis-plus框架下对于一个较复杂关联查询,并且实现分页的功能已经完成。

本案例的实际环境是基于目前也比较常用的ruoyi-vue-plus 4.X
地址: RuoYi-Vue-Plus (gitee)https://gitee.com/JavaLionLi/RuoYi-Vue-Plus

在 Service 中调用相对比较简单

@RequiredArgsConstructor
@Service
public class T3dMaterialServiceImpl implements IT3dMaterialService {

    private final T3dMaterialMapper baseMapper;

    @Override
    public TableDataInfo<T3dMaterialVo> queryPageAList( Page<T3dMaterialVo> page,T3dMaterialBo bo ) {
        //LambdaQueryWrapper<T3dMaterial> lqw = buildQueryWrapper(bo);
        Page<T3dMaterialVo> result =   baseMapper.selectList2(page,bo);

//        return result;
        return TableDataInfo.build(result);
    }

该Service 调用最终以 TableDataInfo 返回,如果不是基于ruoyi-vue-plus没有该对象,可以直接返回Page<?> ,或者封装成自己框架所需要的数据列表对象。

相关推荐
虫小宝1 小时前
如何在Java中实现批量数据处理
java·开发语言
king888866661 小时前
Java中的AQS
java
冰暮流星1 小时前
软设之类的继承与泛化,多重继承
java·开发语言
虫小宝1 小时前
Java中的多线程与并发编程详解
java·开发语言
oNuoyi1 小时前
定位线上同步锁仍然重复扣费的Bug定位及Redis分布式锁解决方案
java·spring boot·redis·分布式
Easonmax1 小时前
【C++】 解决 C++ 语言报错:Undefined Reference
java·开发语言·c++
王大锤43911 小时前
idea导入opencv和mediapipe
java·opencv·intellij-idea
01传说1 小时前
JAVA 发送短信信息工具类(腾讯云)
java·windows·腾讯云
java6666688881 小时前
Java中的线程调度与性能优化技巧
java·开发语言·性能优化
Easonmax2 小时前
【C++】 解决 C++ 语言报错:Invalid Cast
java·开发语言·c++