springboot+pagehelper
阿新 • • 發佈:2020-12-19
技術標籤:springbootmybatisspringbootpagehelper
pom.xml
<!--pagehelper分頁外掛-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
pojo
@Data
public class Book implements Serializable {
private int id;
private String bookName;
}
dao
@Mapper
@Repository
public interface BookMapper {
@Select("select * from book")
public List<Book> queryAllBooks();
}
service
@Service
public class BookService {
@Autowired
private BookMapper bookMapper;
public List<Book> queryAllBooks(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize){
//使用PageHelper
PageHelper.startPage(pageIndex,pageSize);
List<Book> books = bookMapper.queryAllBooks ();
PageInfo<Book> pi = new PageInfo<>(books);
return pi.getList();
}
}
controller
@RestController
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("/queryAll")
public String queryAllBooks(@RequestParam("pageIndex") int pageIndex,@RequestParam("pageSize") int pageSize){
List<Book> books = bookService.queryAllBooks(pageIndex,pageSize);
return JSON.toJSONString(books);
}
}
測試
book表中所有資料:
http://localhost:8080/book/queryAll?pageIndex=2&pageSize=5 取第2頁,共5條
[
{
bookName: "Python",
id: 6
},
{
bookName: "C#",
id: 7
},
{
bookName: "Js",
id: 8
},
{
bookName: "Spring",
id: 9
},
{
bookName: "SpringMVC",
id: 10
}
]