table of Contents
Two paging operations
One is through limit query, the other is through PageHelper plugin
limit query
//分页
List<User> getUserByLimit(Map<String,Integer> map);
<select id="getUserByLimit" parameterType="map" resultType="com.lt.pojo.User">
select * from user limit #{startIndex},#{pageSize}
</select>
//测试分页方法(手写SQL)
@Test
public void getUserByLimit(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao dao = sqlSession.getMapper(UserDao.class);
HashMap<String,Integer> map = new HashMap<String, Integer>();
map.put("startIndex",0);
map.put("pageSize",2);
List<User> users = dao.getUserByLimit(map);
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
}
Use PageHelper plugin
Guide package
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.3</version>
</dependency>
Add the plugins tag in the main configuration file, pay attention to be written behind the settings tag
The interface does not need to change, ordinary query
mapper.xml doesn't need to change
Test method, add a line to set a few pages, how many lines are displayed on each page
View the results, according to the custom paging requirements, find the first two lines of data