欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 基于mybatis plus增加较复杂自定义查询以及分页

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

2024/10/24 17:25:26 来源:https://blog.csdn.net/rex0y/article/details/139706219  浏览:    关键词:基于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_namefrom t3d_material aINNER 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)icon-default.png?t=N7T8https://gitee.com/JavaLionLi/RuoYi-Vue-Plus

在 Service 中调用相对比较简单

@RequiredArgsConstructor
@Service
public class T3dMaterialServiceImpl implements IT3dMaterialService {private final T3dMaterialMapper baseMapper;@Overridepublic 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<?> ,或者封装成自己框架所需要的数据列表对象。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com