Mybatis 使用分页插件pageHelper、JSqlParser

由于编写分页查询业务繁琐,自己编写分页查询语句不仅使得页面代码冗余,而且开发效率低下,因此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();
	} 
   }


使用分页插件:全局文件config.xml配置
     
       
        
一、使用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 page = PageHelper.startPage(1, 2);
		List list = studentDao.selectPage2();
		for (Student student : list) {
			System.out.println(student);
		}
		System.out.println("查询总页数:"+page.getPages());
		System.out.println("当前页数:"+page.getPageNum());
		System.out.println("每页大小:"+page.getPageSize());
		System.out.println("开始查询的row:"+page.getStartRow());
		System.out.println("总记录数:"+page.getTotal());
	} catch (IOException e) {
		e.printStackTrace();
	} 
	   
	   /*
	    查询结果:
Student [id=1, name=zhangsan, NO=654321, school=null]
Student [id=2, name=王五, NO=108030009, school=null]
查询总页数:3
当前页数:1
每页大小:2
开始查询的row:0
总记录数:6	    
	    */
   }   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 page = PageHelper.startPage(3, 2);
		List list = studentDao.selectPage2();
		for (Student student : list) {
			System.out.println(student);
		}
		
		/**使用pageInfo*/
		PageInfo pageInfo = new PageInfo<>(list, 2); //显示连续两个页码
		
		System.out.println("查询总页数:"+pageInfo.getPages());
		System.out.println("当前页数:"+pageInfo.getPageNum());
		System.out.println("每页大小:"+pageInfo.getPageSize());
		System.out.println("开始查询的row:"+pageInfo.getStartRow());
		System.out.println("总记录数:"+pageInfo.getTotal());
		/**新增功能*/
		System.out.println("是第一页?"+pageInfo.isIsFirstPage());
		int[] a = pageInfo.getNavigatepageNums();
		for (int i : a) {
			System.out.print(" "+i);
		}
	} catch (IOException e) {
		e.printStackTrace();
	} 
	   
	   /*
	    查询结果:
Student [id=5, name=white, NO=3333, school=null]
Student [id=6, name=周莹, NO=8859-1, school=null]
查询总页数:3
当前页数:3
每页大小:2
开始查询的row:5
总记录数:6
是第一页?false
 2 3	    
	    
	    */
	   
   }      
  








你可能感兴趣的:(Mybatis)