SpringBoot+mybatis配置pagehelper实现基础分页


1)pom.xml


    com.github.pagehelper
    pagehelper-spring-boot-starter
    1.2.10

2)application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/javaee
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: "00000000"  #密码加双引号
mybatis:
  configuration:
    map-underscore-to-camel-case: true  #开启驼峰命名映射
#分页配置
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

3)controller

@Controller
public class IndexController {
    @Autowired
    ArticleService articleService;
    @GetMapping("/")
    public String getArticleList(Model model,
                                 HttpServletRequest request,
                                 @RequestParam(value = "pageNum",
                                               defaultValue = "1",
                                               required = false) int pageNum,
                                 @RequestParam(value = "pageSize",
                                               defaultValue = "6") int pageSize){
				//pageNum前端传来的页号,pageSize每页的条数
        Page page = PageHelper.startPage(pageNum,pageSize);     
        List articleDTOS = articleService.getArticleDTOS();
        PageInfo pageInfo = new PageInfo<>(page.getResult());
        model.addAttribute("articles",articleDTOS);  			//获取文章传输对象放到前端
        model.addAttribute("pageInfo",pageInfo);

        return "index";

    }
}

4)前端Thymeleaf