分页插件pagehelper的使用
1.引入依赖
<dependencies>
<!-- mybatis的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--连接mysql数据库的驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>my Sql -connector-java</artifactId>
<version>8.0.24</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId> junit </groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!--分页插件的依赖->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
2.在resources目录下的配置文件mybatis-config.xml里的environments标签的上面添加,注意位置一定在environments标签的上面
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
3 进行测试
常用方法 都是PageHelper这个类的方法
注意 pagehelper的使用要在调用方法之前进行使用
1 startPage(int pageNum, int pageSize) // 使用1 第一个参数起始页 第二个参数每页数量 相当于 limit (page-1)*size,size
2 startPage(int pageNum, int pageSize, String orderBy) 在1的基础上增加了排序字段、
3 offsetPage(int offset, int limit) 直接等价于limit offset,size
4 clearPage() 清空原来的配置
public class MainApplicaion {
public String name;
@Test
public void sodsf(){
String config=" mybatis .xml";
try {
// 获取SqlSessionFactory
InputStream strem = Resources.getResourceAsStream(config);
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(strem);
SqlSession sqlSession = build.openSession();
some mapper = sqlSession.getMapper(some.class);
// 注意 pagehelper的使用要在调用方法之前进行使用
// 使用1 第一个参数起始页 第二个参数每页数量 相当于 limit (page-1)*size,size
// Page<Book> page = PageHelper.startPage(1, 3);
// 使用2 直接等价于limit offset,size
Page<Object> page = PageHelper.offsetPage(10, 3);
// 方法3 清空原来的配置
// PageHelper.clearPage();
List<Book> books=mapper.getDeaprtmenst();
books.forEach(System.out::println);
} catch (IO Exception e) {
e.printStackTrace();
}
}
}