使用mybatisPlus注解方式多表关联查询带条件分页

本文记录使用mybatisPlus 多表关联查询,分页,条件查询

1、新建一个实体vo,这个实体可以不是数据库存在的表。

自由发挥

2、先看一下service层的代码

//获取page对象
        Page<MfuHealthPushRecordDto> page =pageConverter.pageCo2Page(pageCo);
        //新建查询对象
        QueryWrapper<MfuHealthPushRecordDto> queryWrapper = new QueryWrapper<>();
        //增加条件查询
        queryWrapper.apply(StringUtils.isNotBlank(mDto.getStartTime()),
                    " and record.DT_UPDATE_DATE >= to_date('" + mDto.getStartTime() + "','yyyy-mm-dd')");
        queryWrapper.apply(StringUtils.isNotBlank(mDto.getEndTime()),
                    " record.DT_UPDATE_DATE <= to_date('" + mDto.getEndTime() + "','yyyy-mm-dd')");

        queryWrapper.like(StringUtils.isNotBlank(mDto.getPublishOrg()),"record.C_PUBLISH_ORG",mDto.getPublishOrg());
        queryWrapper.eq(StringUtils.isNotBlank(mDto.getIllnessCode()),"ill.C_ILLNESS_ID",mDto.getIllnessCode());

        List<MfuHealthPushRecordDto> mfuHealthPushRecordList = mfuHealthPushRecordMapper.getMfuHealthPushRecordList(page, queryWrapper);

3、要准备一个page对象,这个pageCo其实很简单


import lombok.Data;

import java.io.Serializable;

/**
 * 分页条件类
 *
 * @author wuhuanjia
 */
@Data
public class PageCo implements Serializable {
    private Long page;
    private Long rows;
}

这边是利用mapstruct把pageCo转换为Page(其实就是属性复制)


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyjk.platform.core.bean.PageBean;
import com.hyjk.platform.core.bean.PageParams;
import com.hyjk.platform.core.entity.PageCo;
import com.hyjk.platform.core.mapstruct.Converter;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.io.Serializable;

/**
 * 分页参数转化
 */
@Mapper(componentModel = "spring")
public interface PageConverter {

    /**
     * PageParams -> Page
     */
    @BeanMapping(resultType = Page.class)
    Page pageParms2Page(PageParams pageParams);

    /**
     * Page -> PageBean
     */
    @BeanMapping(resultType = PageBean.class)
    PageBean page2PageBean(IPage page);

    default <D extends Serializable, T extends Serializable> PageBean<T> page2PageBean(IPage<D> page, Converter<D, T> converter) {
        PageBean<T> pageBean = new PageBean<>();
        pageBean.setTotal(page.getTotal());
        pageBean.setCurrent(page.getCurrent());
        pageBean.setSize(page.getSize());
        pageBean.setRecords(converter.domain2Dto(page.getRecords()));
        return pageBean;
    }

    /**
     * @param pageCo
     * @return
     */
    @Mappings({
            @Mapping(source = "page", target = "current"),
            @Mapping(source = "rows", target = "size")
    })
    Page pageCo2Page(PageCo pageCo);
}
 

4、利用QueryWrapper对象进行条件查询,单表查询的网上有很多了,多表查询的使用居然不给用列别名,所以里面的条件查询都是用的表的列字段,更多的可以去看mybatisplus的官方文档有介绍api

5、因为我要分页 ,所以第一个参数传了page,后面做条件查询所以传了queryWrapper

6、到了mapper类,要实现basemapper

  @Select("select record.C_HEALTH_PUSH_ID as healthPushId, record.C_PUBLISH_TYPE as publishType,to_char(record.DT_UPDATE_DATE, 'yyyy-mm-dd hh24:mi:ss') as updateDate,record.C_RECIPIENT as recipient,record.C_PUBLISH_ORG_NAME as publishOrgName,record.C_PUBLISH_ORG as publishOrg,record.c_knowledge_id as knowledgeId,ill.C_ILLNESS_NAME as illnessName,ill.C_ILLNESS_ID as illnessCode, knowledge.c_theme as theme, knowledge.c_content as content from MFU_HEALTH_PUSH_RECORD record,MFU_RE_ILLNESS_KNOWLEDGE ill,MFU_HEALTH_KNOWLEDGE knowledge where record.c_knowledge_id = ill.c_knowledge_id and record.c_knowledge_id = knowledge.c_knowledge_id " + " ${ew.sqlSegment}")
    List<MfuHealthPushRecordDto> getMfuHealthPushRecordList(Page<MfuHealthPushRecordDto> page, @Param(Constants.WRAPPER) QueryWrapper<MfuHealthPushRecordDto> queryWrapper);

这里就是mapper类的写法,具体的包如下

完整的sql,精髓是后面拼接的那个参数,就是在上面代码动态拼接上去的

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus是一个基于MyBatis的增强工具,它提供了很多便捷的功能来简化开发。在MyBatis-Plus中,多表关联查询分页查询都是支持的。 对于多表关联查询MyBatis-Plus提供了多种方式来实现,其中一种常用的方式使用@TableName注解和@JoinTable注解。首先,在实体类上使用@TableName注解指定表名,然后在需要关联的字段上使用@JoinTable注解指定关联条件。通过这种方式,可以方便地进行多表关联查询。 下面是一个示例代码,演示了如何使用MyBatis-Plus进行多表关联查询: ```java // 实体类1 @TableName("table1") public class Entity1 { @TableId private Long id; private String name; // ... } // 实体类2 @TableName("table2") public class Entity2 { @TableId private Long id; private Long entityId1; private String info; // ... } // Mapper接口 public interface EntityMapper extends BaseMapper<Entity1> { @Select("SELECT t1.*, t2.info FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.entity_id1") List<Entity1> selectEntityWithInfo(); } ``` 在上述示例中,Entity1和Entity2分别对应数据库中的table1和table2表。通过@JoinTable注解,我们可以在EntityMapper接口中定义一个自定义的查询方法selectEntityWithInfo(),该方法使用了LEFT JOIN来实现多表关联查询,并返回Entity1的列表。 对于分页查询,MyBatis-Plus提供了Page对象来支持分页功能。在查询方法中,可以通过传入Page对象来指定分页参数,然后使用MyBatis-Plus提供的分页查询方法进行查询。查询结果会被封装到Page对象中,包含了总记录数、当前页数据等信息。 下面是一个示例代码,演示了如何使用MyBatis-Plus进行分页查询: ```java // Mapper接口 public interface EntityMapper extends BaseMapper<Entity1> { @Select("SELECT * FROM table1") IPage<Entity1> selectEntityWithPage(Page<Entity1> page); } ``` 在上述示例中,EntityMapper接口中的selectEntityWithPage()方法使用了Page对象作为参数,并通过@Select注解指定了查询语句。在实际调用时,可以创建一个Page对象并传入分页参数,然后调用selectEntityWithPage()方法进行分页查询。 以上就是使用MyBatis-Plus进行多表关联查询分页查询的简单介绍。如果你还有其他问题,请继续提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值