基于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)icon-default.png?t=N7T8https://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<?> ,或者封装成自己框架所需要的数据列表对象。

MyBatis-Plus 支持使用自定义 SQL 语句进行分页查询,下面是一个示例代码: ```java // 设置分页参数 Page<User> page = new Page<>(1, 10); // 查询第 1 页,每页 10 条记录 // 执行分页查询 List<User> userList = userMapper.selectUserList(page, 1); // 执行自定义 SQL 查询 // 输出查询结果 long total = page.getTotal(); // 获取查询总数 ``` 上面的代码中,我们首先通过 `new Page<>(1, 10)` 创建一个分页参数对象,表示查询第 1 页,每页 10 条记录。然后,我们使用自定义的 SQL 语句进行分页查询,通过 `userMapper.selectUserList(page, 1)` 执行查询,将查询结果保存到 `userList` 对象中。最后,我们通过 `page.getTotal()` 获取查询总数。 在自定义 SQL 语句中,我们需要使用 MyBatis-Plus 提供的分页参数。例如,在 MySQL 中,我们可以使用 `limit #{page.offset}, #{page.size}` 来限制查询结果的数量,并使用 `count(1)` 函数统计查询总数。下面是一个示例 SQL 语句: ```xml <select id="selectUserList" resultType="com.example.User"> select * from user where status = #{status} limit #{page.offset}, #{page.size} </select> ``` 在上面的 SQL 语句中,我们使用 `#{page.offset}` 表示查询结果的偏移量,使用 `#{page.size}` 表示查询结果的数量,其中 `page` 是 MyBatis-Plus 提供的分页参数对象。我们还使用 `count(1)` 函数统计查询总数,并将查询总数保存到 `page` 对象中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值