SpringBoot+Mybatis+Thymeleaf+Pagehelper
1.在pom.xml中导入依赖
1 2 3 4 5
| <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency>
|
2.在application.yml中添加配置
1 2 3 4 5 6
| pagehelper: helper-dialect: mysql reasonable: true support-methods-arguments: true params: count=countSql
|
3.编写sql语句
1 2 3 4
| <! <select id="getAllBlog" resultMap="blog"> select * from t_blog </select>
|
这里请注意不要加’;’!!! ** 因为pagehelper是在sql语句的后面追加’limit 5(这里’5’只是举例,无任何实际含义),如果在sql语句后面加了’;’,那么sql语句就变成了‘select * from t_blog;limit 5’**,此时就会报错。
4.编写Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package com.zhb.blogs.controller.admin;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.zhb.blogs.pojo.Blog; import com.zhb.blogs.pojo.User; import com.zhb.blogs.service.BlogService; import com.zhb.blogs.service.TagService; import com.zhb.blogs.service.TypeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession; import java.util.List;
@Controller public class BlogController {
@Autowired private BlogService blogService;
@GetMapping("/blogs") public String blogs(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){ PageHelper.startPage(pagenum, 5); List<Blog> allBlog = blogService.getAllBlog(); PageInfo pageInfo = new PageInfo(allBlog); model.addAttribute("pageInfo", pageInfo); return "admin/blogs"; } }
|
5.编写前端
1 2 3 4 5 6 7 8 9 10 11 12 13
| <div class="column"> <div class="ui floated pagination menu"> <p class="ui blue info message">当前第<span th:text="${pageInfo.pageNum}"></span>页,总<span th:text="${pageInfo.pages}"></span>页,共<span th:text="${pageInfo.total}"></span>条记录</p> </div> </div> <div class="right aligned column"> <div class="ui right floated pagination menu" th:if="${pageInfo.pages>1}"> <div class="item"><a th:href="@{/}">首页</a></div> <div class="item"><a th:href="@{/(pagenum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页 </a></div> <div class="item"><a th:href="@{/(pagenum=${pageInfo.hasNextPage}? ${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a></div> <div class="item"><a th:href="@{/(pagenum=${pageInfo.pages})}">尾页</a></div> </div> </div>
|
6.结果