分页

1.使用limit分页

1.方法:利用集合去接受结果,把分页的数值存在map中

1
2
//分页
List<User> getLimit(Map<String,Integer> map);

2.SQL语句,就像前面提到的那样,我们的结果如果是list。那么把泛型数据填在里面;

1
2
3
<typeAliases>
<typeAlias type="com.saxon.pojo.User" alias="user"/>
</typeAliases>
1
2
3
<select id="getLimit" parameterType="map" resultType="user">
select * from mybatis.saxon limit ${startIndex},${pageSize};
</select>

3.测试

1
2
3
4
5
6
7
8
9
10
11
12
13
@Test
public void limit(){
SqlSession sqlSession = MybatisUnit.getSqlSession ();
UserMapper mapper = sqlSession.getMapper (UserMapper.class);
Map<String, Integer> Map = new HashMap<> ();
Map.put ("startIndex",0);
Map.put ("pageSize",2);
List<User> limit = mapper.getLimit (Map);
for (User user : limit) {
System.out.println (user);
}
sqlSession.close ();
}

2.使用rowbounds

1
2
//分页2
List<User> getLimitby();

分页操作由rowbounds做

1
2
3
<select id="getLimitby" resultType="user">
select * from mybatis.saxon
</select>
1
2
3
4
5
6
7
8
9
10
11
@Test
public void testRowbounds () {
SqlSession sqlSession = MybatisUnit.getSqlSession ();
RowBounds rowBounds = new RowBounds (1, 2);//其实下标和也页面大小
List<User> list = sqlSession.selectList ("com.saxon.Dao.UserMapper.getLimitby", null,rowBounds );
for (User o : list) {
System.out.println (o);
}

sqlSession.close ();
}

3.使用插件

例:pageHelper