Spring Boot with mybatis paging plugin PageHelper to achieve paging
Introduction of depend on
Related to paging is pageHelper-spring-boot-starter
pom.xml
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.5.2 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.lagou</groupId> <artifactId>mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatis</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> < the groupId > org. Mybatis. Spring. The boot < / groupId > < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 2.2.0 < / version > </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <! -!!!!!! Paging starter --> <dependency> <groupId>com.github. Pagehelper </groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> </dependencies> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> </project>Copy the code
Spring Boot configuration file
application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot? serverTimezone=UTC&characterEncoding=UTF-8
username: root
password: root
mybatis:
configuration:
map-underscore-to-camel-case: true
type-aliases-package: com.lagou.mybatis.entitiy
Configure MyBatis XML configuration file path
mapper-locations: classpath:mapper/*.xml
# pagination configuration
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
Copy the code
com/lagou/mybatis/MybatisApplication.java
package com.lagou.mybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
// Configure mappper scanning, eliminating @mapper annotation for each mapper
@MapperScan("com.lagou.mybatis.mapper")
public class MybatisApplication {
public static void main(String[] args) { SpringApplication.run(MybatisApplication.class, args); }}Copy the code
Creating an entity Class
com/lagou/mybatis/entity/User.java
package com.lagou.mybatis.entity;
import lombok.Data;
import java.util.Date;
@Data
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
}
Copy the code
Create a Mapper interface
com/lagou/mybatis/mapper/UserMapper.java
package com.lagou.mybatis.mapper;
import com.lagou.mybatis.entity.User;
import java.util.List;
public interface UserMapper {
List<User> findAll(a);
}
Copy the code
resource/mapper/UserMapper.xml
<! DOCTYPEmapper PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lagou.mybatis.mapper.UserMapper">
<! Select * from 'all';
<select id="findAll" resultType="com.lagou.mybatis.entity.User">
select * from user
</select>
</mapper>
Copy the code
The test page
com/lagou/mybatis/MybatisApplicationTests.java
package com.lagou.mybatis;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.mybatis.entity.User;
import com.lagou.mybatis.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
class MybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testPageHelper(a) {
PageHelper.startPage(1.1);
List<User> users = userMapper.findAll();
PageInfo<User> pageInfo = new PageInfo<>(users);
System.out.println("total:"+pageInfo.getTotal());
System.out.println("page num:"+pageInfo.getPageNum());
System.out.println("page size:"+pageInfo.getPageSize()); users.stream().forEach(System.out::println); }}Copy the code
conclusion
- Importing into dependencies
pagehelper-spring-boot-starter
- Use before query
PageHelper. StartPage (1, 1);