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

时间:2025-03-19 08:08:01

认知

其实两者联合使用非常简单,只需要在使用分页插件的基础上使用其提供的orderByDesc/orderByAsc就能完美融合。写这篇博客只是给一些初学者一个方便的整理而已。下面会先给出完整的使用情况,在到后面分别解释两者的用法。以方便只关心这个用法或者赶时间的朋友。处于保密性考虑,我将大部分字段进行了删减,将项目名也进行了修改

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 (()) {
            return ("参数有误:" + ().toString());
        }
        QueryWrapper<ComOrganization> queryWrapper = new QueryWrapper<ComOrganization>();
        //如果前端有值传入 则将其赋值。无则就不赋值。为后面设置查询条件铺路
        (() != null && () != 0, "company_id", ());
        (() != null && () != 0, "department_id", ());
        //设置查询条件
        (());
        //根据id进行顺序排序 id可用替换为你想进行排序的字段
        ("id");
        //将页码与每页展示数量传入
        IPage<ComOrganization> page = new Page<>((()), (()));
        //第三个字段为缓存设置的key值
        return (page, queryWrapper,()+()+());
    }
    }

Service 

public interface ComOrganizationService extends IService<ComOrganization> {
	//Response 为自定义的统一返回方法  可以自己替换为自己想返回的类型
    Response getPage(IPage<ComOrganization> page, QueryWrapper<ComOrganization> queryWrapper,String key);
    
}

Impl 

	//项目使用redis缓存 自定义key值 这样能保证唯一,方便使用且易于查看
    @Cacheable(cacheNames = "ComOrganization", key = "#+':'+#+':'+#key")
    @Override
    public Response getPage(IPage<ComOrganization> page, QueryWrapper<ComOrganization> queryWrapper,String key) {
        IPage<ComOrganization> poPage = (page, queryWrapper);
        return (poPage);
    }

Entity 

/**
 * <p>
 * 租户机构表
 * </p>
 *
 * @author campchen
 * @since 2019-04-28
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("com_organization")
public class ComOrganization implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableField("id")
    private Long id;

    /**
     * 公司ID
     */
    @TableField("company_id")
    private Long companyId;


    /**
     * 负责部门ID
     */
    @TableField("department_id")
    private Long departmentId;

}

Entityparam

/**
 * <p>
 * 查询租户机构表参数
 * </p>
 *
 * @author campchen
 * @since 2019-04-28
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ComOrganizationQueryParam extends CommonParam {

    @ApiModelProperty(value = "公司ID")
    private Long companyId;
    @ApiModelProperty(value = "负责部门ID")
    private Long departmentId;
}

 CommonParam

@Data
public abstract class CommonParam implements Serializable {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 条数
	 */
	@ApiModelProperty(value = "条数", example = "5")
	private String pageSize = "5";

	/**
	 * 当前页
	 */
	@ApiModelProperty(value = "当前页", example = "1")
	private String pageNumber = "1";

}

Mapper

/**
 * <p>
 * 机构表 Mapper 接口
 * </p>
 *
 * @author campchen
 * @since 2019-04-28
 */
public interface ComOrganizationMapper extends BaseMapper<ComOrganization> {

}

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/">
<mapper namespace="">

</mapper>

至此 就完成了整个mybatisplus 的分页排序的代码。其提供了强大的功能,不需要我们再去编写其余代码。

Page

分页插件没有比官网介绍更清楚的了。那边清楚的讲述了来龙去脉。

/guide/

orderByAsc

orderByAsc(R... columns)
orderByAsc(boolean condition, R... columns)

例: orderByAsc("id", "name")--->order by id ASC,name ASC

orderByDesc

orderByDesc(R... columns)
orderByDesc(boolean condition, R... columns)

例: orderByDesc("id", "name")--->order by id DESC,name DESC

后记

好长一段时间没有写博客了,一方面忙,一方面也没有碰到什么需要解决的困难。这里是做为一个新手上手mybatisplus的一些记录。作为一个成长的记录把。加油!!!