Mybatis Plus 连表分页查询、时间日期范围、条件查询

  • 实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("platform_bill")
public class PlatformBill implements Serializable {

    /** 账单详情编号 */
    private Long id;

    /** 交易时间 */
    private Timestamp transactionTime;
  
    /** 商户号,用商户号跟商户表 store 连接*/
    private String mchtNo;
    
}
  • Dto

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PlatformBillDto implements Serializable {

    /** 账单详情编号 */
    private Long id;

    /** 交易时间,用List集合装,前端传入开始时间和结束时间*/
    private List<Timestamp> transactionTime;
  
    /** 商户号,用商户号跟商户表 stroe 连接*/
    private String mchtNo;

    /** 商户名称,这个 name 是从商户表 stroe 查过来的*/
    private String name;

}
  • mapper 

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

@Repository
public interface PlatformBillMapper extends CoreMapper<PlatformBill> {

    /**
     * 账单表连商户表,查账单信息和商户名称
     * @param
     * @return
     */
    // 自定义 sql 语句
    @Select("SELECT p.id,p.transaction_time,p.mcht_no,s.name FROM platform_bill AS p LEFT JOIN store AS s ON p.mcht_no = s.id ${ew.customSqlSegment}")
    IPage<PlatformBillDto> getList(Page<PlatformBillDto> pageParam, @Param(Constants.WRAPPER) QueryWrapper<PlatformBillDto> queryWrapper);

}
  • service 

import com.baomidou.mybatisplus.core.metadata.IPage;

public interface PlatformBillService  extends BaseService<PlatformBill>{
  
	IPage<PlatformBillDto> getList(Page<PlatformBillDto> pageParam, PlatformBillDto platformBillDto);
  
}
  • impl

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.StringUtils;

@Service
@AllArgsConstructor
//@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PlatformBillServiceImpl extends BaseServiceImpl<PlatformBillMapper, PlatformBill> implements PlatformBillService {
  
    @Override
    public IPage<PlatformBillDto> getList(Page<PlatformBillDto> pageParam, PlatformBillDto platformBillDto) {

        // 条件构造器
        QueryWrapper<PlatformBillDto> queryWrapper = new QueryWrapper<>();
        // 有分页查询 无条件查询,如果没有传条件过来,那么全部查询且分页
        if (PlatformBillDto == null) {
            return baseMapper.getList(pageParam,null);
        }
      
        //  查询订单号
        String orderId = PlatformBillDto.getOrderId();
      	// 传过来的时间,开始时间 与 结束时间,用 List 集合装
        List<Timestamp> transactionTime = PlatformBillDto.getTransactionTime();
      	// 还需要什么条件查询的,根据自己需要加
      
      	// 如果有传过来的订单号,那么会加入条件构造器中
        if (!StringUtils.isEmpty(orderId)) {
            queryWrapper.eq("order_id", orderId);
        }
      
      	// 传过来的时间,开始时间在第 0 位,结束时间在第 1 位,
        // 前端传参要写transactionTime=2022-xx-xx&transactionTime=2022-xx-xx, 看自己时间格式是什么。
        if (!StringUtils.isEmpty(transactionTime)) {
            queryWrapper.between("transaction_Time", transactionTime.get(0),transactionTime.get(1));
        }
        
        // queryWrapper 把所有条件传进去
        return baseMapper.getList(pageParam, queryWrapper);
    }

}
  • 定义的返回结果集,根据自己的需求而写,非必须

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class DataVo<T> implements Serializable{
    
    private Integer count;
    private List<T> data;
    
    public DataVo(Integer count, List<T> data) {
        this.count = count;
        this.data = data;
    }
    
}
  • controller

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;

@RestController
@RequestMapping("/platformBill")
public class PlatformBillController {

	@GetMapping("/getList")
    public DataVo getList(Integer page,Integer size,PlatformBillDto platformBillDto) {

        DataVo result = new DataVo();
        
        // 分页参数
        Page<PlatformBillDto> pageParam = new Page<>(page, size);
        IPage<PlatformBillDto> pageModel = PlatformBillDto.getList(pageParam, platformBillDto);
        // 查出数据
        List<PlatformBillDto> data = pageModel.getRecords();
        // 统计条数
        long conunt = pageModel.getTotal();
        
        result.setCount(conunt);
        result.setData(data);
        return result;

    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值