7、SpringBoot+Mybatis整合------PageHelper简单…

2018-07-03 01:01:22来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

开发工具:STS

代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/1d30d2a573ce6784149a28af9b8afcab7414370c

PageHelper官方地址:https://pagehelper.github.io/

前言:

当我们操作数据库获取数据列表时,往往面对对查询数据进行分页的情况。

今天,我们使用PageHelper插件,来简单实现分页效果。


 

一、Page简单使用

1.添加pom依赖:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.xm</groupId>
 7     <artifactId>demo005_Mybatis</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>demo005_Mybatis</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>2.0.2.RELEASE</version>
18         <relativePath/> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25     </properties>
26 
27     <dependencies>
28         <!-- 添加PageHelper依赖 -->
29         <dependency>
30             <groupId>com.github.pagehelper</groupId>
31             <artifactId>pagehelper-spring-boot-starter</artifactId>
32             <version>1.2.3</version>
33         </dependency>
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-jdbc</artifactId>
37         </dependency>
38         <dependency>
39             <groupId>org.springframework.boot</groupId>
40             <artifactId>spring-boot-starter-web</artifactId>
41         </dependency>
42         <dependency>
43             <groupId>org.mybatis.spring.boot</groupId>
44             <artifactId>mybatis-spring-boot-starter</artifactId>
45             <version>1.3.2</version>
46         </dependency>
47 
48         <dependency>
49             <groupId>mysql</groupId>
50             <artifactId>mysql-connector-java</artifactId>
51             <scope>runtime</scope>
52         </dependency>
53         <dependency>
54             <groupId>org.springframework.boot</groupId>
55             <artifactId>spring-boot-starter-test</artifactId>
56             <scope>test</scope>
57         </dependency>
58     </dependencies>
59 
60     <build>
61         <plugins>
62             <plugin>
63                 <groupId>org.springframework.boot</groupId>
64                 <artifactId>spring-boot-maven-plugin</artifactId>
65             </plugin>
66         </plugins>
67     </build>
68 
69 
70 </project>
pom.xml

 

2.添加测试用例:

 1 package com.xm;
 2 
 3 import java.util.List;
 4 
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.boot.test.context.SpringBootTest;
 9 import org.springframework.test.context.junit4.SpringRunner;
10 
11 import com.github.pagehelper.Page;
12 import com.github.pagehelper.PageHelper;
13 import com.xm.mapper.StudentMapper;
14 import com.xm.pojo.Student;
15 
16 @RunWith(SpringRunner.class)
17 @SpringBootTest
18 public class StudentTest {
19     @Autowired
20     private StudentMapper studentMapper;
21     
22     
23     
24     @Test
25     public void pageText() {
26         Page page = PageHelper.startPage(1, 5);
27         List<Student> students = studentMapper.list();
28         for(Student student : students) {
29             System.out.println(student.toString());
30         }
31         System.out.println("当前页:"+page.getPageNum());
32         System.out.println("当前页数据数:"+page.getPageSize());
33         System.out.println("页总数:"+page.getPages());
34         System.out.println("数据总数:"+page.getCountColumn());
35         System.out.println("第3个数据详情:"+page.get(2));
36     }
37     
38 
39 }
StudentTest.java

 

3.测试结果:

 


 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:从.Net到Java学习第十篇——Spring Boot文件上传

下一篇:spring boot(四) 多数据源