由于编写分页查询业务繁琐,自己编写分页查询语句不仅使得页面代码冗余,而且开发效率低下,因此mybatis中可以借助分页插件进行分页查询的编程,提高编程效率。
下载插件:pageHelper.x.x.x.jar 和 JSqlParser.x.x.x.jar包
http://www.github.com/pageHelper
http://www.github.com/JSqlParser
首先自写分页查询:
/**自定义分页查询*/
@Test
public void test1() {
try {
InputStream in = Resources.getResourceAsStream("config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession session = sessionFactory.openSession();
StudentDao studentDao = session.getMapper(StudentDao.class);
Map map = new HashMap<>();
map.put("pageNum", (2-1)*3);
map.put("pageSize", 3);
List list = studentDao.selectPageById(map);
for (Student student : list) {
System.out.println(student);
}
} catch (IOException e) {
e.printStackTrace();
}
}
一、使用pageHelper:
/**测试pagehelper插件*/
/**使用page*/
@Test
public void test2() {
try {
InputStream in = Resources.getResourceAsStream("config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession session = sessionFactory.openSession();
StudentDao studentDao = session.getMapper(StudentDao.class);
/**查询紧跟page之后*/
Page
2)使用pageInfo实现更加详细的查询,突出功能---查询页码导航getNavigatepageNums()可以得到连续的页码数,
/**使用pageInfo 实现更详细查询*/
@Test
public void test3() {
try {
InputStream in = Resources.getResourceAsStream("config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession session = sessionFactory.openSession();
StudentDao studentDao = session.getMapper(StudentDao.class);
/**查询紧跟page之后*/
Page