关于分页查询的基础案例
2024-01-02 09:23:41
public class BroadScopeController { @Resource private BroadScopeService broadScopeService; @PostMapping ("/queryPage") /** * 分页查询 */ public Result queryPage(@RequestBody BroadScopePageDTO dto) { //如果穿了页数的话,就默认第一页,然后每页10条 if (Objects.isNull(dto.getPage())||dto.getPage()<1) { dto.setPage(1); } if (Objects.isNull(dto.getPageSize())||dto.getPageSize()<1) { dto.setPageSize(10); } PageResult<BroadScopeVO> queryPage =broadScopeService.queryPage(dto); return Result.success(queryPage); }
@Service public interface BroadScopeService { PageResult<BroadScopeVO> queryPage(BroadScopePageDTO dto);}
@Service public class BroadScopeServiceImpl implements BroadScopeService { @Resource private BroadScopeMapper broadScopeMapper; @Resource private BroadScopePromptMapper broadScopePromptMapper; /** * 分页查询 * @param dto * @return */ @Override public PageResult<BroadScopeVO> queryPage(BroadScopePageDTO dto) { // 计算偏移量,防止sql注入 int offset = (dto.getPage() - 1) * dto.getPageSize(); List<BroadScopePromptCategory> category = broadScopeMapper.queryPage(offset, dto.getPageSize()); // 判断是否为空,没有配置指令,就返回null if (ObjectUtils.isEmpty(category)) { return null; } // 实体类转vo List<BroadScopeVO> categoryVO = new ArrayList<>(); for (BroadScopePromptCategory broadScopePromptCategory : category) { BroadScopeVO broadScopeVO = new BroadScopeVO(); // Bean拷贝失败,换个工具类, BeanUtils.copyProperties(broadScopePromptCategory, broadScopeVO, BroadScopeVO.class); categoryVO.add(broadScopeVO); } PageResult<BroadScopeVO> data = new PageResult<>(); data.setPage(dto.getPage()); data.setPageSize(dto.getPageSize()); data.setRows(categoryVO); data.setTotal(category.size()); return data; }
@Mapper public interface BroadScopeMapper { /** * 分页查询 * @param offset * @param pageSize * @return */ List<BroadScopePromptCategory> queryPage(@Param("offset") int offset,@Param("pageSize") Integer pageSize);
<!--分页查询--> <select id="queryPage" resultType="com.example.domain.entity.BroadScopePromptCategory"> select id, category_name, sort from broadscope_prompt_category order by sort LIMIT #{pageSize} OFFSET #{offset} </select>
统一返回类
@Data @NoArgsConstructor @AllArgsConstructor public class Result { private Integer code;//响应码,1 代表成功; 0 代表失败 private String msg; //响应信息 描述字符串 private Object data; //返回的数据 //增删改 成功响应 public static Result success(){ return new Result(1,"success",null); } //查询 成功响应 public static Result success(Object data){ return new Result(1,"success",data); } //失败响应 public static Result error(String msg){ return new Result(0,msg,null); } }
分页传入dto
@Data public class BroadScopePageDTO { /** * 当前页 */ private Integer page; /** * 每页展示几条 */ private Integer pageSize; }
返回的数据vo:
@Data @AllArgsConstructor @NoArgsConstructor public class BroadScopeVO implements Serializable { private Long id; /** * 类目名称 */ private String categoryName; /** * 排序 */ private Long sort; }
全局异常类:
/** * 自定义全局异常处理器 */ public class BusinessException extends RuntimeException { public BusinessException(String message) { super(message); } public BusinessException(){ } }
信息常量类:
/** * 信息提示常量类 */ public class MessageConstant { public static final String CATEGORY_EXISTS = "该分类已经存在"; public static final String SAVE_FAILED = "新增失败"; public static final String DELETE_FAILED = "删除失败"; public static final String CATEGORY_DELETE = ",分类下有指令,删除失败"; public static final String UPDATE_FAILED = "修改失败"; public static final String SORT_NULL = "排序字段不能为空"; public static final String SORT_FAILED = "排序失败"; /** * 参数不能为空 */ private static final String PARAMETER_NULL = "参数不能为空"; public static final String ID_NULL = "id不能为空"; }
测试:
文章来源:https://blog.csdn.net/2301_80189343/article/details/135324493
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!