Guoguo-notes
主页
常用笔记
vue笔记及周边生态
  • 团队协作及规范
  • 项目框架及架构
  • 飞码篇
  • Java
  • React笔记
GitHub
主页
常用笔记
vue笔记及周边生态
  • 团队协作及规范
  • 项目框架及架构
  • 飞码篇
  • Java
  • React笔记
GitHub
  • Java

    • 1. AjaxResult.md
    • 2. Java 基础.md
    • 3. Java 注解.md
    • 4. MyBatis Plus.md
    • 5. MySQL完整.md
    • 6. Mybatis 语法.md
    • 7. StringBoot.md
    • 8. idea 插件.md
    • 9. idea快捷键.md
    • 10. java 代码片段.md
    • 11. mySql.md
    • 12. 分页插件使用.md
    • 13. 项目初始化.md

yxd总结

首先在 pom.xml 文件中导入包

<!--pagehelper-->      
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.4.7</version>
</dependency>

如果是低版本的 springboot 框架 使用这个

<!--pagehelper-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.0.0</version>
</dependency>

就可以在serviceImpl 实现类中使用了

startPage 用来接收 前端传递过来的参数

然后就可以通过 PageInfo<【封装数据的类型 可能是一个 PageHelper 泛型】> employeePageInfo = new PageInfo<>(【需要根据需求进行封装的数据】);来进行接收了

    public AjaxResult getUsers() {
        PageHelper.startPage(1, 10);
        List<Employee> employees = employeeMapper.getUsers();
        PageInfo<Employee> employeePageInfo = new PageInfo<>(employees);

        return AjaxResult.success(employeePageInfo);
    }

吴俊楠总结

PageHelper简介:**是国内非常优秀的一款开源的mybatis分页插件,它支持基本主流与常用的数据库,例如mysql、oracle、mariaDB、DB2、SQLite、Hsqldb等。

作用:简化分页查询,支持多种关系型数据库的分页查询

1.引入pagehelper依赖

<!--pagehelper-->
 <dependency>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper</artifactId>
   <version>5.0.0</version>
 </dependency>

2.通过核心配置文件整合pagehelper

<!--在typeAliases的下方添加pagehelper的拦截器-->
<plugins>
	<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

3.BrandMapper层添加接口方法

 List<Brand> selectByPageAndCondition2(Brand brand);

3.1映射配置文件中添加查询Sql

<select id="selectByPageAndCondition2" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
            <if test="brandName != null and brandName != '' ">
                and  brand_name like #{brandName}
            </if>

            <if test="companyName != null and companyName != '' ">
                and  company_name like #{companyName}
            </if>

            <if test="status != null">
                and  status = #{status}
            </if>

        </where>
    </select>

4.分页插件的基本使用:在调用dao层查询方法前调用**PageHelper.startPage(当前页,每页显示条数)**该方法,分页插件自动计算起始位置,添加limit实现分页查询;并自动生成查询总条数的sql

4.1 BrandService接口中添加分页方法

//service
PageInfo<Brand> selectByPageAndCondition2(int currentPage, int pageSize, Brand brand);

4.2 BrandServiceImpl重写该方法

@Override
    public PageInfo<Brand> selectByPageAndCondition2(int currentPage, int pageSize, Brand brand) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        // 处理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 + "%");
        }

        PageHelper.startPage(currentPage,pageSize);
        //5. 查询当前页数据
        List<Brand> rows = mapper.selectByPageAndCondition2(brand);

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

        return new PageInfo<Brand>(rows);
    }

4.3 BrandServlet层调用service层实现分页查询

public void selectByPageAndCondition2(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查询
        PageInfo<Brand> pageBean = brandService.selectByPageAndCondition2(currentPage,pageSize,brand);

        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

PS:PageInfo类是pagehelper提供的,重点记忆(total,list)中存的数据是什么

//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;

//由于startRow和endRow不常用,这里说个具体的用法
//可以在页面中"显示startRow到endRow 共size条数据"

//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集(当前页要展示的数据)
private List<T> list;

5.前端页面修改

axios({
                    method:"post",
                    url:"http://localhost:8080/brand-case/brand/selectByPageAndCondition2?currentPage="+this.currentPage+"&pageSize="+this.pageSize,
                    data:this.brandSearch
                }).then(resp =>{
                    //设置表格数据
                    this.tableData = resp.data.list; // {list:[],total:100,...}
                    //设置总记录数
                    this.totalCount = resp.data.total;
                })
Edit this page
Last Updated:
Contributors: 袁果锅
Prev
11. mySql.md
Next
13. 项目初始化.md