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

相关推荐
shehuiyuelaiyuehao1 小时前
1计算机是如何工作的
java·计算机网络
letisgo52 小时前
JAVA 高级进阶07篇《@Transactional失效的8个场景:代理机制到传播行为》
java·面试·transactional·spring事务·aop动态代理
水域安全老周2 小时前
水趣钓鱼救生衣专利拆解:两级锁紧如何解决落水人衣分离
java·前端·网络
三8442 小时前
Java 模板注入(FreeMarker) · 02 · 模板引擎与 FreeMarker 语法
java·开发语言·web安全·freemarker
暮雨哀尘3 小时前
Java 基础练习(一):创建类并调用对象
java·开发语言
想要入门的程序猿3 小时前
回调函数学习
java·网络·学习
泡海椒3 小时前
动态代理核心原理:JQuick-Java接口规则自动生成机制解析
java·开发语言·python
步行cgn3 小时前
Spring 启动报错:BeanFactory not initialized 的原因与解决
java·python·spring
步行cgn4 小时前
Spring 报错:No bean class specified on bean definition 的原因与解决
java·后端·spring
zhangjw344 小时前
第47篇:Java综合实战:电商秒杀系统(高并发场景)
java·开发语言