1.添加PageHelper依赖
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
2.在application.properties中添加配置
logging.level.com.example.demo.dao=DEBUG
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
pagehelper.page-size-zero=true
3.插件的使用
1.MovieTagMapper.xml文件中编写查询语句
<!-- 分页查询标签信息-->
<select id="page" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
*
from movie_tag
<if test="search != null and search != ''" >
where movieBiaoqian like concat('%',#{search} , '%')
</if>
</select>
2.MovieTagMapper
/**
* 分页查询标签信息
*/
List<MovieTag> page(@Param("search") String search);
3.MovieTagService
/**
* 查询所有标签信息
* @param page
* @param size
*/
PageInfo<MovieTag> page(Integer page, Integer size, String search);
4.MovieTagServiceImpl
@Override
public PageInfo<MovieTag> page(Integer page, Integer size, String search) {
PageHelper.startPage(page, size);
List<MovieTag> movieList = movieTagMapper.page(search);
PageInfo<MovieTag> pageInfo = new PageInfo<>(movieList);
return pageInfo;
}
5.MovieTagController
@GetMapping("/page")
public ResponseEntity pageSelect(@RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex,
@RequestParam(value = "pageSize",required = false,defaultValue = "6") Integer pageSize,
@RequestParam(value = "search",required = false,defaultValue = "") String search) {
PageInfo<MovieTag> pageInfo = movieTagService.page(pageIndex,pageSize,search);
return ResponseEntity.data(pageInfo);
}