页面编写顺序 dao 层 mybatis 配置 --》service 层的接口及实现类 --》controller(web 下)
分页插件作用于 dao 层, 与之相关的是 mybatis 的配置
- <!-- 分页插件 -->
- <dependency>
- <groupId>com.GitHub.pagehelper</groupId>
- <artifactId>pagehelper</artifactId>
- </dependency>
- <configuration>
- <!-- 配置分页插件 -->
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageHelper">
- <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库 -->
- <property name="dialect" value="mysql"/>
- </plugin>
- </plugins>
- </configuration>
然后在执行 sql 语句前加上
- // 分页处理
- PageHelper.startPage(1, 10);
- List<TbItem> list = mapper.selectByExample(example);// 执行 sql 语句, 返回列表
- // 分页信息
- PageInfo<TbItem> pageInfo=new PageInfo<>(list);
- long total = pageInfo.getTotal();
- System.out.println("共有商品:"+ total);
来源: http://www.bubuko.com/infodetail-3107838.html