工具类封装
package com. example. page. common;
import lombok. Data;
@Data
public class Page {
private int pageNum;
private int pageSize;
private long total;
private int pages;
public Page ( int pageNum, int pageSize, long total) {
this . pageNum = ( pageNum <= 0 ) ? 1 : pageNum;
this . pageSize = ( pageSize<= 0 ) ? 10 : pageSize;
this . total = total;
}
public static Page page ( int pageNum, int pageSize, long total) {
return new Page ( pageNum, pageSize, total) ;
}
private int getPages ( ) {
return ( int ) ( total / pageSize + ( ( total % pageSize == 0 ) ? 0 : 1 ) ) ;
}
public int offset ( ) {
return getPages ( ) == 0 ? 0 : ( pageNum > getPages ( ) ? ( getPages ( ) - 1 ) * pageSize : ( pageNum - 1 ) * pageSize) ;
}
}
package com. example. page. common;
import lombok. Data;
import java. util. List;
@Data
public class PageInfo < T> {
private int pageNum;
private int pageSize;
private int size;
private int pages;
private boolean firstPage = false ;
private boolean lastPage = false ;
private boolean hasPreviousPage = false ;
private boolean hasNextPage = false ;
private long total;
private List< T> list;
public PageInfo ( ) {
this . firstPage = false ;
this . lastPage = false ;
this . hasPreviousPage = false ;
this . hasNextPage = false ;
}
public PageInfo ( List< T> list, int pageNum, int pageSize, long total) {
this . firstPage = false ;
this . lastPage = false ;
this . hasPreviousPage = false ;
this . hasNextPage = false ;
this . list = list;
if ( list != null) {
this . pageNum = ( pageNum <= 0 ) ? 1 : pageNum;
this . pageSize = ( pageSize<= 0 ) ? 10 : pageSize;
this . total = total;
this . size = list. size ( ) ;
this . judgePageBoudary ( ) ;
if ( pageSize > 0 ) {
this . pages = ( int ) ( total / pageSize + ( ( total % pageSize == 0 ) ? 0 : 1 ) ) ;
} else {
this . pages = 0 ;
}
if ( pageNum > this . pages) {
if ( pages != 0 ) {
this . pageNum = this . pages;
}
}
}
}
private void judgePageBoudary ( ) {
this . firstPage = this . pageNum == 1 ;
this . lastPage = this . pageNum == this . pages || this . pages == 0 ;
this . hasPreviousPage = this . pageNum > 1 ;
this . hasNextPage = this . pageNum < this . pages;
}
}
mapper
@Mapper
public interface UserMapper {
@Select ( "select * from t_user where user_id >#{offset} limit #{pageSize}" )
List< User> selectAllByPage ( int offset, int pageSize) ;
@Select ( "select count(0) from t_user" )
long countTotal ( ) ;
}
service
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List< User> selectAllByPage ( Page page) {
return userMapper. selectAllByPage ( page. offset ( ) , page. getPageSize ( ) ) ;
}
public long countTotal ( ) {
return userMapper. countTotal ( ) ;
}
}
controller
package com. example. page. controller;
import com. example. page. common. Page;
import com. example. page. common. PageInfo;
import com. example. page. common. Result;
import com. example. page. model. User;
import com. example. page. service. UserService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. web. bind. annotation. GetMapping;
import org. springframework. web. bind. annotation. PathVariable;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. RestController;
import java. util. List;
@RestController
@RequestMapping ( value = "/api/v1/user" )
public class UserController {
@Autowired
private UserService userService;
@GetMapping ( value = "/{page}/{size}" )
public Result< PageInfo< User> > list ( @PathVariable Integer page, @PathVariable Integer size) {
long total = userService. countTotal ( ) ;
List< User> users = userService. selectAllByPage ( Page. page ( page, size, total) ) ;
PageInfo< User> pageInfo = new PageInfo < > ( users, page, size, total) ;
return Result. ok ( 2000 , "SUCCESS" , pageInfo) ;
}
}
统一返回格式
package com. example. page. common;
import lombok. Data;
@Data
public class Result < T> {
private int code;
private String message;
private T payload;
private Result< T> code ( int code) {
this . code = code;
return this ;
}
private Result< T> message ( String message) {
this . message = message;
return this ;
}
private Result< T> payload ( T payload) {
this . payload = payload;
return this ;
}
public static < T> Result< T> ok ( int code, String message, T payload) {
return new Result < T> ( ) . code ( code) . message ( message) . payload ( payload) ;
}
}