分页成果展示:www.qihea.xyz
1.整合mybatis框架
1.数据库准备好数据:我网站中的数据时通过python爬虫,直接保存到数据库中的。图片直接保存的豆瓣的url。
2.整合mybatis依赖和mybatis分页插件:
根据此教程完成初步操作:Kotlin+IDEA+SpringBoot+MyBatis+MySQL+Gradle项目(不要选择kotlin即可)
然后添加分页插件依赖:官网https://github.com/abel533/MyBatis-Spring-Boot
//mybatis分页插件
compile group: 'com.github.pagehelper', name:'pagehelper-spring-boot-starter',version: '1.2.10'
推荐阅读:(读不读都行)
1.为什么使用mybatis
2.Mybatis配置之配置元素详述
3.mybatis使用
2.结合mybatis分页插件编写动态sql实现分页功能。
有三种分页方式:JavaWeb分页显示内容之分页查询的三种思路(数据库分页查询)。我选择的是后台分页。
1.约定好url接口(REST风格)
因为使用mybatis分页插件直接得到的是json数据,所以我直接使用接口来访问和传递数据。如果不想用接口的话,也可以使用模版等方式,不过我就不说了。
使用接口后,前端可以直接通过ajax来通过rest风格接口获取数据。
示例:
- 如果什么都不选择http://www.qihea.xyz/page?size=1&start=0&sort=0:
- {"total":4867,"list":[{"id":1292052,"name":"肖申克的救赎","score":9.6,"url":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg"}],"pageNum":0,"pageSize":1,"size":1,"startRow":1,"endRow":1,"pages":4867,"prePage":0,"nextPage":1,"isFirstPage":false,"isLastPage":false,"hasPreviousPage":false,"hasNextPage":true,"navigatePages":8,"navigatepageNums":[1,2,3,4,5,6,7,8],"navigateFirstPage":1,"navigateLastPage":8}
- 比如选择:剧情分类(genres=11),地区为中国(place=中国),年代为2018(beginyear和endyear都为2018),排序为最热电影(sort=0),第一页(start=1),每页最多条目(size=10)来发送url请求。
- http://www.qihea.xyz/page?size=10&start=1&sort=0&genres=11&place=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E9%99%86&beginyear=2018&endyear=2018
-
{"total":6,"list":[{"id":26752088,"name":"我不是药神","score":9.0,"url":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2519070834.jpg"},{"id":26425063,"name":"无双","score":8.2,"url":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535096871.jpg"},{"id":26647117,"name":"暴裂无声","score":8.2,"url":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2517333671.jpg"},{"id":27110296,"name":"无名之辈","score":8.3,"url":"https://img3.doubanio.com/