PageHelper
MyBatis uses third-party plugins to extend functionality. The pagination assistant PageHelper encapsulates complex pagination operations. You can obtain pagination-related data in a simple way. PageHelper is a pagination plugin for MyBatis or MyBatis-Plus that provides easy-to-use pagination functionality. It intercepts SQL queries and automatically adds pagination conditions during queries to implement data pagination queries.
Basic Features
PageHelper helps us automatically perform pagination queries when using MyBatis, avoiding manually writing complex pagination logic. Through the interceptor approach, it dynamically passes pagination parameters into SQL statements during query execution and obtains pagination results by setting pagination objects.
Working Principles
The core functionality of PageHelper is to modify SQL through interceptors and add pagination parameters. Before query execution, it modifies the original SQL query based on provided pagination parameters, adding LIMIT, OFFSET and other pagination conditions, then returns paginated results. Pagination information is provided through the Page class, which encapsulates total record count, total pages, and other information.
Development Steps
- Import PageHelper coordinates
- Configure PageHelper plugin in MyBatis core configuration file
- Test pagination data acquisition
Import pom
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.5.1</version>
</dependency>
Plugin Configuration
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
</plugin>
Code Implementation
public class WzkicuPage01 {
public static void main(String[] args) throws Exception {
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
PageHelper.startPage(2, 1);
List<WzkUser> dataList = userMapper.findAll();
for (WzkUser wzk : dataList) {
System.out.println(wzk);
}
}
}
Execution Results
WzkUser(id=2, username=wzk2, password=icu2, birthday=Mon Nov 11 00:00:00 CST 2024, orderList=null, roleList=null)
Configure PageHelper
Configure PageHelper plugin in Spring configuration file, usually in application.properties or application.yml:
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
PageInfo Class
PageInfo is an important class used by PageHelper to encapsulate pagination query results. It contains pagination-related information:
getTotal(): Total record countgetPages(): Total page countgetPageNum(): Current page numbergetPageSize(): Page sizegetList(): Current page data
Notes
- When using PageHelper, the pagination query must be placed after calling
PageHelper.startPage()and before executing the query - If using a Mapper interface with @Mapper annotation, PageHelper can automatically work with MyBatis
- For queries requiring pagination, the returned result must be a List type
Generic Mapper
Brief Introduction
Generic mapper is designed to solve CRUD operations for single tables. Based on MyBatis plugin mechanism, developers don’t need to write SQL or add methods in DAO. They just need to write entity classes to have CRUD methods.
Import Dependency
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.5.1</version>
</dependency>
Set Primary Key
@Table(name = "wzk_user")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class WzkUser implements Serializable {
private int id;
private String username;
private String password;
private Date birthday;
private List<WzkOrder> orderList;
private List<WzkRole> roleList;
}
Modify DAO
public interface UserMapper extends Mapper<WzkUser> {
// Omit previous content
}
Write Code
public class WzkMapper01 {
public static void main(String[] args) throws Exception {
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
MapperHelper mapperHelper = new MapperHelper();
mapperHelper.registerMapper(UserMapper.class);
mapperHelper.processConfiguration(sqlSession.getConfiguration());
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<WzkUser> dataList = userMapper.selectAll();
for (WzkUser each : dataList) {
System.out.println(each);
}
sqlSession.close();
}
}
Execution Results
WzkUser(id=0, username=wzk2, password=icu2, birthday=Mon Nov 11 00:00:00 CST 2024, orderList=null, roleList=null)
Example Usage
public class WzkMapper02 {
public static void main(String[] args) throws Exception {
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
MapperHelper mapperHelper = new MapperHelper();
mapperHelper.registerMapper(UserMapper.class);
mapperHelper.processConfiguration(sqlSession.getConfiguration());
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Example example = new Example(WzkUser.class);
example.createCriteria().andEqualTo("username", "wzk2");
List<WzkUser> dataList = userMapper.selectByExample(example);
for (WzkUser each : dataList) {
System.out.println(each);
}
sqlSession.close();
}
}
Execution Results
WzkUser(id=0, username=wzk2, password=icu2, birthday=Mon Nov 11 00:00:00 CST 2024, orderList=null, roleList=null)