集成PageHelper分页插件
# SpringBoot集成PageHelper分页插件
# 步骤一、导入jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
1
2
3
4
5
2
3
4
5
# 步骤二、配置SpringBoot文件
在application.yaml配置文件中添加以下配置:
#pagehelper
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
returnPageInfo: check
1
2
3
4
5
6
7
2
3
4
5
6
7
pagehelper.helper-dialect
: 指定分页插件使用哪种语言pagehelper.offset-as-page-num
: 默认为false
, 该参数对使用RowBounds
作为分页参数时有效,当为true
时,会将RowBounds
的offset
参数当成pageNum
使用pagehelper.row-bounds-with-count
: 默认为false
,该参数对使用RowBounds
作为分页参数时有效,当该参数值为true
时,使用RowBounds
分页会进行count
查询pagehelper.page-size-zero
: 默认为false
,当该参数为true
时,如果pageSize=0
或者RowBounds.limit=0
就会查询所有结果pagehelper.reasonable
: 分页合理化参数,默认为false
,当该值为true
,pageNum<=0
默认查询第一页,pageNum>pages
时会查询最后一页,false
时直接根据参数进行查询pagehelper.params
: 为了支持startPage(Object params)
方法,增加该参数来配置参数映射,用于从对象中根据属性名取值,可以配置pageNum,pageSize,pageSizeZero, reasonable
, 不配置映射是使用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero
pagehelper.support-methods-arguments
: 支持通过Mapper
接口参数来传递分页参数,默认为false
, 分页插件会从查询方法的参数中根据params
配置的字段中取值,查找到合适的就进行分页pagehelper.auto-runtime-dialect
: 默认为false
, 为true
时允许在运行时根据多数据源自动识别对应的方言进行分页pagehelper.close-conn
: 默认为true
, 当使用运行是动态数据源或者没有设置helperDialect
属性自动获取数据库类型时,会自动获取一个数据库连接,通过该属性来设置是否关闭获取的这个连接,默认为true
关闭,false
不会自动关闭
# 步骤三、使用
@Mapper
public interface ArticleDao {
List<Article> getArticleList();
}
1
2
3
4
5
2
3
4
5
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.singerw.dao.ArticleDao">
<select id="getArticleList" resultMap="articleMap">
select *
from article
</select>
<resultMap id="articleMap" type="Article">
<id property="aID" column="a_id"/>
<result property="articleTitle" column="article_title"/>
<result property="articleContent" column="article_content"/>
<result property="headImage" column="head_image"/>
<result property="articleAuthor" column="article_author"/>
<result property="typeNumber" column="type_number"/>
<result property="pageviews" column="pageviews"/>
<result property="createTime" column="create_time"/>
<result property="isState" column="is_state"/>
</resultMap>
</mapper>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public interface ArticleDaoService {
ResponseData<Article> getArticleList(Integer page,Integer limit);
}
1
2
3
4
2
3
4
@Service
public class ArticleDaoServiceImpl implements ArticleDaoService {
@Autowired
private ArticleDao articleDao;
@Override
public ResponseData<Article> getArticleList(Integer page, Integer limit) {
PageHelper.startPage(page, limit);
List<Article> articleList = articleDao.getArticleList();
PageInfo<Article> pageInfo = new PageInfo(articleList);
ResponseData<Article> responseData = new ResponseData(0, "success", pageInfo.getTotal(), articleList);
return responseData;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RestController
@RequestMapping("/api")
public class ArticleController {
@Autowired
private ArticleDaoService articleDaoService;
@GetMapping("/artList")
public ResponseData<Article> getArticleList(
@RequestParam(name = "page", defaultValue = "1") Integer page,
@RequestParam(name = "limit", defaultValue = "10") Integer limit) {
ResponseData<Article> responseData = articleDaoService.getArticleList(page, limit);
return responseData;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
编辑 (opens new window)