MyBatis-Plus 分页、排序联合使用 补充

Controller

@Slf4j
@Validated
@RestController
@RequestMapping("/com-organization")
public class ComOrganizationController {
@PostMapping(value = "/getPage")
public Response<IPage<ComOrganizationQueryParam>> getPage(@RequestBody @Valid ComOrganizationQueryParam param, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
        }
        QueryWrapper<ComOrganization> queryWrapper = new QueryWrapper<ComOrganization>();
        queryWrapper.lambda()
                .eq((param.getCompanyId() != null && param.getCompanyId() != 0),
                        ComOrganization::getCompanyId, param.getCompanyId())
    /*分页写法之二
	如果无法在其中调用此函数时,由于没有指定R (没有指定该参数属于哪个类)
	//设置查询条件
    queryW
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus 中,你可以使用 PageHelper 插件来实现分页排序和将 null 值排在最后的功能。 首先,确保你已经正确地配置了 MyBatis-Plus 和 PageHelper 插件。 然后,在你的查询方法中,使用 PageHelper.startPage 方法来启动分页功能,并使用 PageHelper.orderBy 方法来指定排序字段和排序方式。例如: ```java import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.PageHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserMapper userMapper; public IPage<User> getUsersWithPaginationAndSort() { // 使用 PageHelper.startPage 方法开启分页功能 Page<User> page = PageHelper.startPage(1, 10); // 使用 PageHelper.orderBy 方法指定排序字段和排序方式 PageHelper.orderBy("name ASC NULLS LAST"); // 执行查询 List<User> userList = userMapper.selectList(null); // 将查询结果封装到 Page 对象中 return page.setRecords(userList); } } ``` 上述代码示例中,我们使用了 `name ASC NULLS LAST` 来指定按照 name 字段升序排序,并且将 null 值排在最后。 请根据你的实际需求调整排序字段和排序方式。注意,在不同的数据库中,对于 null 值的处理方式可能会有所不同,可以根据实际情况来调整排序语句。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值