PageHelper 原理
将分页参数存放入ThreadLoacl中,保证线程间互不影响;查询前,利用mybatis提供的拦截器,取得ThreadLocal的值,重新拼装分页SQL,完成分页
分页插件 应用组成
1、配置Maven依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
2、application.yml 配置
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
3、POJO
public class Product implements Serializable {
private String id;
private String name;
}
4、mpper.xml
<mapper namespace="**.dao.ProductDao">
<select id="findByPage" resultType="Product">
select * from product
</select>
</mapper>
5、DAO
@Mapper
public interface ProductDao {
Page<Product> findByPage();
}
6、使用
PageHelper.startPage(pageNum, pageSize);
Page<Product> products = productDao.findByPage();
PageInfo<Product> pageInfo = new PageInfo<>(products);
网友评论