springboot+mybatis-plus+freemarker分页显示数据(简单实现)

mybatis-plus配置类,注册分页插件

package com.example.demo.mapper;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.baomidou.mybatisplus.plugins.PaginationInterceptor;

//Spring boot方式
@EnableTransactionManagement
@Configuration
@MapperScan("com.itheima.mapper")
public class MyBatisPlusConfig {
     
  /**
   * 分页插件
   * @return
   */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
     
      PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
      //你的最大单页限制数量,默认 500 条,小于 0 如 -1 不受限制
      //paginationInterceptor.setLimit(2);
      return paginationInterceptor;
  }
}

controller类

@RequestMapping("selectByTerm2")
	public String slectByTerm2(Model model,
			@RequestParam(value = "p", required = false, defaultValue = "1" + "") int p) {
     
		Page<Employee> page = new Page<Employee>(p, 5);
		List<Employee> employees = employeeMapper.selectPage(page, null);
//		model.addAttribute("Total", page.getTotal());
//		model.addAttribute("Current", page.getCurrent());
//		model.addAttribute("Pages", page.getPages());
		model.addAttribute("page", page);
		model.addAttribute("employees", employees);

		return "show3";
	}

前端ftlh页面

<table>
		<tr>
			<th>ID</th>
			<th>名字</th>
			<th>邮箱</th>
			<th>性别</th>
			<th>年龄</th>
		</tr>
		<#list employees as user>
		<tr>
			<td>${
     user.id}</td>
			<td>${
     (user.lastName)!""}</td>
			<td>${
     (user.email)!""}</td>
			<td>${
     (user.gender)!""}</td>
			<td>${
     (user.age)!""}</td>
			</tr>
		</#list>
		<#-- 显示首页 -->
	<#if (page.getCurrent() > 1)>
		<td><a href="selectByTerm2?p=1">首页</a></td>
	</#if>
	<#-- 显示上一页 -->
	<#if (page.getCurrent() > 1)>
		<td><a href="selectByTerm2?p=${page.getCurrent()-1}">上一页</a></td>
	</#if>
	<#if (page.getCurrent()<page.getPages())>
		<td><a href="selectByTerm2?p=${page.getCurrent()+1}">下一页</a></td>
	</#if>
	</table>
	
	

springboot+mybatis-plus+freemarker分页显示数据(简单实现)_第1张图片
springboot+mybatis-plus+freemarker分页显示数据(简单实现)_第2张图片

简单的分页处理,在你的现有基础上添加这三段代码应该就可以实现。如果增删改查没搞定可以看我之前的博客

你可能感兴趣的:(sb+mp+fm)