一、Limit分页
<select id="getUserInfo1" parameterType="map" resultType="dayu">
select * from user
<if test="startPos!=null and pageSize!=null">
limit ${startPos},${pageSize}
</if>
</select>
List<User> getUserInfo1(Map<String,Object> map);
@Test
public void selectUser() {
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
//这里塞值
Map<String,Object> parms = new HashMap<>();
parms.put("startPos","0");
parms.put("pageSize","5");
List<User> users = mapper.getUserInfo1(parms);
for (User map: users){
System.out.println(map);
}
session.close();
}
二、RowBounds分页
RowBounds对象有2个属性,offset和limit。
- offset:起始行数
- limit:需要的数据行数
<select id="getUserInfoRowBounds" resultType="dayu">
select * from user
</select>
List<User> getUserInfoRowBounds(RowBounds rowBounds);
@Test
public void selectUserRowBounds() {
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
// List<User> users = session.selectList("com.dy.mapper.UserMapper.getUserInfoRowBounds",null,new RowBounds(0, 5));
List<User> users = mapper.getUserInfoRowBounds(new RowBounds(0,5));
for (User map: users){
System.out.println(map);
}
session.close();
}
三、Mybatis_PageHelper分页插件
引入jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.7</version>
</dependency>
配置MyBatis核心配置文件
标签:map,分页,mapper,List,UserMapper,session,三种,MyBatis,users From: https://www.cnblogs.com/KL2016/p/16770554.html<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor" />
</plugins>
@Test
public void selectUserPageHelper() {
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
//第二种,Mapper接口方式的调用,推荐这种使用方式。
PageHelper.startPage(1, 3);
List<User> list = mapper.getUserInfo();
//用PageInfo将包装起来
PageInfo page = new PageInfo(list);
for (User map: list){
System.out.println(map);
}
System.out.println("page:---"+page);
session.close();
}