一、接口传入page跟size,及查询条件的封装对象。
@PostMapping ( value = "/search/{page}/{size}" )
public Result < PageInfo > findPage ( @RequestBody ( required = false ) Album album, @PathVariable int page, @PathVariable int size) {
PageInfo < Album > pageInfo = albumService. findPage ( album, page, size) ;
return new Result ( true , StatusCode . OK, "查询成功" , pageInfo) ;
}
二、分页数据传入pageHelper中,构建查询条件,最终返回。
@Override
public PageInfo < Album > findPage ( Album album, int page, int size) {
PageHelper . startPage ( page, size) ;
Example example = createExample ( album) ;
return new PageInfo < Album > ( albumMapper. selectByExample ( example) ) ;
}
public Example createExample ( Album album) {
Example example= new Example ( Album . class ) ;
Example. Criteria criteria = example. createCriteria ( ) ;
if ( album!= null ) {
if ( ! StringUtils . isEmpty ( album. getId ( ) ) ) {
criteria. andEqualTo ( "id" , album. getId ( ) ) ;
}
if ( ! StringUtils . isEmpty ( album. getTitle ( ) ) ) {
criteria. andLike ( "title" , "%" + album. getTitle ( ) + "%" ) ;
}
if ( ! StringUtils . isEmpty ( album. getImage ( ) ) ) {
criteria. andEqualTo ( "image" , album. getImage ( ) ) ;
}
if ( ! StringUtils . isEmpty ( album. getImageItems ( ) ) ) {
criteria. andEqualTo ( "imageItems" , album. getImageItems ( ) ) ;
}
}
return example;
}