JavaEE——Mybatis(18)--MyBatis实用场景 1.PageHelper插件分页

• PageHelper是MyBatis中非常方便的第三方分页插件。
• 官方文档:https://gitee.com/free/Mybatis_PageHelper   我们可以对照官方文档的说明,快速的使用插件

https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

把最新的jar包下载下来

导入jar包之后要配置拦截器插件

在代码中使用

 

 PageInfo

package com.atguigu.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.atguigu.mybatis.bean.Student;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.atguigu.mybatis.dao.StudentMapper;
import org.junit.Test;

public class MyBatisTest {

	public SqlSessionFactory getSqlSessionFactory() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		return new SqlSessionFactoryBuilder().build(inputStream);
	}

	@Test
	public void test01() throws IOException {
		// 1、获取sqlSessionFactory对象
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		// 2、获取sqlSession对象
		SqlSession openSession = sqlSessionFactory.openSession();
		try {
			/*// 3、获取接口的实现类对象
			//会为接口自动的创建一个代理对象,代理对象去执行增删改查方法
			StudentMapper mapper = openSession.getMapper(StudentMapper.class);
			Student student = mapper.getEmpById(1);
			System.out.println(mapper);
			System.out.println( student );*/

			StudentMapper studentMapper = openSession.getMapper( StudentMapper.class );
			Page page = PageHelper.startPage(2, 4);
			List<Student> students = studentMapper.getStus();
			/*PageInfo<Student> info = new PageInfo<>(students);*/
			//传入要连续显示多少页
			PageInfo<Student> info = new PageInfo<>(students, 2);
			for(Student student : students){
				System.out.println(student);
			}
			System.out.println("--------Page-------");
			System.out.println("当前页码 " + page.getPageNum());
			System.out.println(" " + page.getCountColumn());
			System.out.println("总记录数 " + page.getTotal());
			System.out.println("总页码 " + page.getPages());
			System.out.println("每页的记录数 " + page.getPageSize());

			System.out.println("--------PageInfo-------");
			System.out.println("当前页码 " + info.getPageNum());
			System.out.println("总记录数 " + info.getTotal());
			System.out.println("总页码 " + info.getPages());
			System.out.println("每页的记录数 " + info.getPageSize());
			System.out.println("是否是第一页 " + info.isIsFirstPage());
			System.out.println("是否是最后一页 " + info.isIsLastPage());

			System.out.println("连续显示的页码");
			int[] nums = info.getNavigatepageNums();
			for(int i = 0; i < nums.length; i++){
				System.out.println(nums[i]);
			}

		} finally {
			openSession.close();
		}

	}

}

  

 

mybatis_config.xml  中的plugins的配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
	</plugins>

	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/db_person" />
				<property name="username" value="root" />
				<property name="password" value="1234" />
			</dataSource>
		</environment>
	</environments>
	<!-- 将我们写好的sql映射文件(StudentMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 -->
	<mappers>
		<mapper resource="StudentMapper.xml" />
	</mappers>
</configuration>

  

posted @ 2018-03-02 10:08  SkyeAngel  阅读(294)  评论(0编辑  收藏  举报