Mybatis plus多表关联分页查询

mybatis-plus中的多个表的关联查询
由于明细表中,没有冗余主表的相关字段,再页面展示的时候,需要这些信息,所以只能关联查询了。
又要关联,又要分页,是否可以用控件实现呢?
那就看看这篇文章吧。

Mybatis-plus插件

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>${3.3.2}</version>
</dependency>

实体类

定义实体类,只需要和表字段映射就可以了。
不一定需要@TavleName

@Data
@TableName("merchant_account_detail")
public class MerchantAccountDetail {

    @TableId(type = IdType.AUTO)
    private Long id;
    /**
     * 余额账户表ID
     */
    private Long merchantAccountId;
	...省略其他属性

    @TableField(exist = false)
    private String merchantName;
 
    @TableField(exist = false)
    private String merchantName;

实现

ServiceImpl:

LambdaQueryWrapper<MerchantAccountDetail> queryWrapper = new LambdaQueryWrapper<>();
if (merchantBalanceAccountDetailDto.getStatus() != null){
    queryWrapper.eq(MerchantAccountDetail::getStatus, status);
}        queryWrapper.orderByDesc(MerchantAccountDetail::getId);

Page<MerchantAccountDetail> page = merchantAccountDetailMapper.selectPageAndMerchantInfo(PageFactory.defaultPage(), queryWrapper);

return new PageResult(page, merchantBalanceAccountDetailPage.getRecords());
        

Mapper:

@Select("SELECT t1.*, t3.merchant_name, t3.merchant_code FROM merchant_account_detail t1 " +
            "left join merchant_account t2 on t1.merchant_account_id = t2.id " +
            "left join merchant_info t3 on t3.merchant_code = t2.merchant_code ${ew.customSqlSegment}")

Page<MerchantAccountDetail> selectPageAndMerchantInfo(Page<MerchantAccountDetailVo> page, @Param(Constants.WRAPPER)LambdaQueryWrapper<MerchantAccountDetail> queryWrapper);
        

关键点:

  1. 如果使用entity,则需要使用@TableField(exist = false)忽略关联表中的字段,如果不使用entity,则就不需要了
  2. 导入的包,使用的相关包如下
Mapper中
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

ServiceImpl中
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

  1. 查询条件的使用,固定模式:
sql中的${ew.customSqlSegment},一定要加@Param(Constants.WRAPPER)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值