2024/1/19日 日志 关于Vue && Element 的后续---》综合案例(7)

功能5:分页条件查询

BrandMapper

点击查看代码
/**
 * 分页条件查询
 * @param begin
 * @param size
 * @return
 */
List<Brand> selectByPageAndCondition(@Param("begin") int begin,@Param("size") int size,@Param("brand") Brand brand);

/**
 * 根据条件查询总记录数
 * @return
 */
int selectTotalCountByCondition(Brand brand);
BrandServiceImpl
点击查看代码
@Override
public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
    //2. 获取SqlSession对象
    SqlSession sqlSession = factory.openSession();
    //3. 获取BrandMapper
    BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

    //4. 计算开始索引
    int begin = (currentPage - 1) * pageSize;
    // 计算查询条目数
    int size = pageSize;

    // 处理brand条件,模糊表达式
    String brandName = brand.getBrandName();
    if (brandName != null && brandName.length() > 0) {
        brand.setBrandName("%" + brandName + "%");
    }

    String companyName = brand.getCompanyName();
    if (companyName != null && companyName.length() > 0) {
        brand.setCompanyName("%" + companyName + "%");
    }

    //5. 查询当前页数据
    List<Brand> rows = mapper.selectByPageAndCondition(begin, size, brand);

    //6. 查询总记录数
    int totalCount = mapper.selectTotalCountByCondition(brand);

    //7. 封装PageBean对象
    PageBean<Brand> pageBean = new PageBean<>();
    pageBean.setRows(rows);
    pageBean.setTotalCount(totalCount);

    //8. 释放资源
    sqlSession.close();

    return pageBean;
}

BrandServlet

点击查看代码
public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
    String _currentPage = request.getParameter("currentPage");
    String _pageSize = request.getParameter("pageSize");

    int currentPage = Integer.parseInt(_currentPage);
    int pageSize = Integer.parseInt(_pageSize);

    // 获取查询条件对象
    BufferedReader br = request.getReader();
    String params = br.readLine();//json字符串

    //转为 Brand
    Brand brand = JSON.parseObject(params, Brand.class);

    //2. 调用service查询
    PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage,pageSize,brand);

    //2. 转为JSON
    String jsonString = JSON.toJSONString(pageBean);
    //3. 写数据
    response.setContentType("text/json;charset=utf-8");
    response.getWriter().write(jsonString);
}
posted @   Moonbeamsc  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
返回顶端
点击右上角即可分享
微信分享提示