关于java:MybatisPlus-多表查询分页

7次阅读

共计 1276 个字符,预计需要花费 4 分钟才能阅读完成。

Mybatis-Plus 多表查问分页

1. 在 mapper 外面定义方法

/* 第一种写法
@Select("select c.* from tb_staff_customer_relation as scr left join tb_customer as c on scr.customer_id = c.id ${ew.customSqlSegment}")
List<Customer> getListById(Page<Customer> page, @Param(Constants.WRAPPER) Wrapper wrapper);
*/

/**
 * 第二种写法
 */
@Select("select c.* from tb_staff_customer_relation as scr left join tb_customer as c on scr.customer_id = c.id ${ew.customSqlSegment}")
IPage<Customer> getListById(Page page, @Param(Constants.WRAPPER) Wrapper wrapper);
  • 解释:

    • page 参数 com.baomidou.mybatisplus.extension.plugins.pagination.Page 须要导入此包
    • wrapper 参数 com.baomidou.mybatisplus.core.conditions.Wrapper 须要导入此包
  • 固定写法, 至于怎么失效的, 我也不晓得呢

2. service 调用 mapper 外面的办法

/**
 * 第二种写法, 不应用 page 类  返回后果应用 ipage
 */
@Override
public List<CustomerResponseVo> getListByStaffId(int staff_id, int page, int limit) {QueryWrapper<Customer> wrapper = new QueryWrapper<>();

    // 查问条件
    wrapper.eq("scr.staff_id", staff_id);
    // 后果集排序
    wrapper.orderByDesc("c.id");

    IPage<Customer> customerIPage = staffCustomerRelationMapper.getListById(new Page(page, limit), wrapper);

    ArrayList<CustomerResponseVo> arrayList = new ArrayList<>();

    // 获取后果集
    List<Customer> list = customerIPage.getRecords();

    // 拼装返回数据
    for (Customer customer : list) {CustomerResponseVo responseVo = GemBeanUtils.copyProperties(customer, CustomerResponseVo.class);
        arrayList.add(responseVo);
    }
    return arrayList;
}

java 初学人员记录笔记, 欢送大佬指教

正文完
 0