7、MyBatis教程之分页实现
8、分页实现
1、limit实现分页
思考:为什么需要分页?
在学习mybatis等持久层框架的时候,会经常对数据进行增删改查操作,使用最多的是对数据库进行查询操作,如果查询大量数据的时候,我们往往使用分页进行查询,也就是每次处理小部分数据,这样对数据库压力就在可控范围内。
使用Limit实现分页
#语法
SELECT * FROM table LIMIT stratIndex,pageSize
SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15
#为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:
SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.
#如果只给定一个参数,它表示返回最大的记录行数目:
SELECT * FROM table LIMIT 5; //检索前 5 个记录行
#换句话说,LIMIT n 等价于 LIMIT 0,n。
步骤:
1、修改Mapper文件
<select id="getUserByPage" parameterType="map" resultType="user">
select * from user limit #{startIndex},#{pageSize}
</select>
2、Mapper接口,参数为map
//选择全部用户实现分页
List<User> getUserByPage(Map<String,Integer> map);
3、在测试类中传入参数测试
- 推断:起始位置 = (当前页面 - 1 ) * 页面大小
//分页查询 , 两个参数startIndex , pageSize
@Test
public void testSelectUser() {
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
int currentPage = 1; //第几页
int pageSize = 2; //每页显示几个
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("startIndex",(currentPage-1)*pageSize);
map.put("pageSize",pageSize);
List<User> users = mapper.getUserByPage(map);
for (User user: users){
System.out.println(user);
}
session.close();
}
2、RowBounds分页
我们除了使用Limit在SQL层面实现分页,也可以使用RowBounds在Java代码层面实现分页,当然此种方式作为了解即可。我们来看下如何实现的!
步骤:
1、mapper接口
//选择全部用户RowBounds实现分页
List<User> getUserByRowBounds();
2、mapper文件
<select id="getUserByRowBounds" resultType="user">
select * from user
</select>
3、测试类
在这里,我们需要使用RowBounds类
@Test
public void testUserByRowBounds() {
SqlSession session = MybatisUtils.getSession();
int currentPage = 2; //第几页
int pageSize = 2; //每页显示几个
RowBounds rowBounds = new RowBounds((currentPage-1)*pageSize,pageSize);
//通过session.**方法进行传递rowBounds,[此种方式现在已经不推荐使用了]
List<User> users =session.selectList("com.kuang.mapper.UserMapper.getUserByRowBounds", null,rowBounds);
for (User user: users){
System.out.println(user);
}
session.close();
}
3、其他分页插件
PageHelper
了解即可,可以自己尝试使用
官方文档:https://pagehelper.github.io/
7、MyBatis教程之分页实现的更多相关文章
- mybatis教程:入门>>精通>>实战
以前曾经用过ibatis,这是mybatis的前身,当时在做项目时,感觉很不错,比hibernate灵活.性能也比hibernate好.而且也比较轻量级,因为当时在项目中,没来的及做很很多笔记.后来项 ...
- mybatis 教程(mybatis in action)
目录简介: 一:开发环境搭建二:以接口的方式编程 三:实现数据的增删改查 四:实现关联数据的查询 五:与spring3集成(附源码) 六:与Spring MVC 的集成 七:实现mybatis分页(源 ...
- MyBatis 教程 ——检视阅读
MyBatis 教程 --检视阅读 准备 官网文档-中文 教程地址yiibai,质量很差 教程地址w3cschool,纯理论,还不如直接看官网文档 教程地址Mybatis框架入门教程,Oracle M ...
- Mybatis Generator实现分页功能
Mybatis Generator实现分页功能 分类: IBATIS2013-07-17 17:03 882人阅读 评论(1) 收藏 举报 mybatisibatisgeneratorpage分页 众 ...
- SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页
SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...
- SpringBoot+Mybatis+PageHelper实现分页
SpringBoot+Mybatis+PageHelper实现分页 mybatis自己没有分页功能,我们可以通过PageHelper工具来实现分页,非常简单方便 第一步:添加依赖 <depend ...
- MyBatis教程目录
MyBatis教程目录 2017-10-18 摘自 YSOcean MyBatis教程目录: 1 mybatis 详解(一)------JDBC 2 mybatis 详解(二)------入门实例( ...
- SpringBoot集成Mybatis并具有分页功能PageHelper
SpringBoot集成Mybatis并具有分页功能PageHelper 环境:IDEA编译工具 第一步:生成测试的数据库表和数据 SET FOREIGN_KEY_CHECKS=0; ...
- Mybatis教程(一)
1 Mybatis教程(一) 学习过的持久层框架:DBUtils , Hibernate Mybatis就是类似于hibernate的orm持久层框架. 为什么学Mybatis? 目前最主流 ...
随机推荐
- Install wx
Ubuntu 16.04: 由于是PY交易, 实际上是安装wxPython: pip install -U \ -f https://extras.wxpython.org/wxPython4/ext ...
- php 配置主机虚拟目录(使用虚拟域名访问 127.0.0.1) 一点也不好使?????
php 配置主机虚拟目录(使用虚拟域名访问 127.0.0.1)steps:1>打开目录 D:\xwamp\bin\apache\apache2.4.9\conf 修改文件 httpd ...
- PPT online viewer
PPT online viewer PPT 在线查看器 SpeakerDeck https://speakerdeck.com/xgqfrms/python?slide=3 SlideShare ht ...
- Dart All In One
Dart All In One dart & flutter https://github.com/dart-lang https://github.com/dart-lang/sdk win ...
- redux & multi dispatch & async await
redux & multi dispatch & async await 同时发送多个 action, 怎么保证按序返回数据 dispatch multi actions http:/ ...
- modal over table bug
modal over table bug table can not clickable bad <el-row> <el-col :span="24"> ...
- react hooks & component will unmount & useEffect & clear up
react hooks & component will unmount & useEffect & clear up useEffect & return === u ...
- Flutter 在同一页面显示List和Grid
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends State ...
- np.mean(img, axis=(0, 1))
np.mean(img, axis=(0, 1)) img 是shape为(H,W,3)的图片 np.mean(img, axis=(0, 1)) 是求出各个通道的平均值,shape是 (3, ) ...
- Anno&Viper -分布式锁服务端怎么实现
1.Anno简介 Anno是一个微服务框架引擎.入门简单.安全.稳定.高可用.全平台可监控.依赖第三方框架少.底层通讯RPC(Remote Procedure Call)采用稳定可靠经过无数成功项目验 ...