基于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<?> ,或者封装成自己框架所需要的数据列表对象。

相关推荐
HanhahnaH5 分钟前
Spring集合注入Bean
java·spring
未定义.22111 分钟前
电子削铅笔刀顺序图详解:从UML设计到PlantUML实现
java·软件工程·uml
雾月5528 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
24k小善1 小时前
Flink TaskManager详解
java·大数据·flink·云计算
想不明白的过度思考者2 小时前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
.生产的驴2 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
猿周LV2 小时前
JMeter 安装及使用 [软件测试工具]
java·测试工具·jmeter·单元测试·压力测试
晨集2 小时前
Uni-App 多端电子合同开源项目介绍
java·spring boot·uni-app·电子合同
时间之城2 小时前
笔记:记一次使用EasyExcel重写convertToExcelData方法无法读取@ExcelDictFormat注解的问题(已解决)
java·spring boot·笔记·spring·excel
椰羊~王小美2 小时前
LeetCode -- Flora -- edit 2025-04-25
java·开发语言