mybatis plus分页查询相关

2023-03-01 17:40:24 浏览数 (1)

文章目录[隐藏]

  • 使用 Wrapper 自定义SQL
  • 匹配Wrapper 问题
  • page.setTotal问题

使用 Wrapper 自定义SQL

${ew.customSqlSegment}使用

必须继承baseMapper<entity>才能使用

匹配Wrapper 问题

代码语言:javascript复制
if (userCompanyVO.getArea()!=null){
    lambdaQueryWrapper.eq(UserCompanyVO::getArea,userCompanyVO.getArea());
}
if (StringUtils.isNotBlank(userCompanyVO.getName())){
    lambdaQueryWrapper.eq(UserCompanyVO::getName,userCompanyVO.getName());
}
if (StringUtils.isNotBlank(userCompanyVO.getUsername())){
    lambdaQueryWrapper.eq(UserCompanyVO::getUsername,userCompanyVO.getUsername());
}

page.setTotal问题

代码语言:javascript复制
page.setSearchCount(false);
page.setTotal(this.count());

示例:

代码语言:javascript复制
//Mapper
public interface UserCompanyMapper extends BaseMapper<UserCompanyVO> {

    /**
     * 查询用户信息
     * @param page 分页
     * @param wrapper 条件
     * @return List<UserCompanyVo>
     */
    @Select("select u.id,u.username,u.create_time,u.role_id,c.`name` ,c.phone_one,c.area from user u LEFT JOIN company c   on  u.company_id =  c.id   ${ew.customSqlSegment}")
    IPage<UserCompanyVO> getPageUserCompanyVO(IPage<UserCompanyVO> page, @Param(Constants.WRAPPER) Wrapper<UserCompanyVO> wrapper);
}
代码语言:javascript复制
//Service
@Override
public IPage<UserCompanyVO> selectUserCompanyVo(Page<UserCompanyVO> page, QueryWrapper<UserCompanyVO> queryWrapper) {
    page.setSearchCount(false);
    page.setTotal(this.count());
    UserCompanyVO userCompanyVO = queryWrapper.getEntity();
    LambdaQueryWrapper<UserCompanyVO> lambdaQueryWrapper = Wrappers.lambdaQuery();
    if (userCompanyVO.getArea()!=null){
        lambdaQueryWrapper.eq(UserCompanyVO::getArea,userCompanyVO.getArea());
    }
    if (StringUtils.isNotBlank(userCompanyVO.getName())){
        lambdaQueryWrapper.eq(UserCompanyVO::getName,userCompanyVO.getName());
    }
    if (StringUtils.isNotBlank(userCompanyVO.getUsername())){
        lambdaQueryWrapper.eq(UserCompanyVO::getUsername,userCompanyVO.getUsername());
    }
    return userCompanyMapper.getPageUserCompanyVO(page,lambdaQueryWrapper);
}
sql

0 人点赞