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>
	
	

在这里插入图片描述
在这里插入图片描述

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

Spring Boot 是一个基于 Spring 框架的快速开发的框架,它提供了很多便捷的功能,如自动配置、嵌入式服务器、安全性等。而 MyBatis Plus 则是在 MyBatis 的基础上进行扩展的一个库,它提供了很多实用的功能,如自动生成代码、分页查询、条件构造器等。 下面介绍一下如何使用 Spring Boot 和 MyBatis Plus 来开发一个网站项目: 1. 创建 Spring Boot 项目 可以使用 Spring Initializr 来创建一个 Spring Boot 项目,选择相应的依赖即可。在 pom.xml 文件中添加 MyBatis Plus 的依赖。 2. 配置数据源 在 application.properties 文件中配置数据源的相关信息,如数据库连接地址、用户名、密码等。 3. 创建实体类和 Mapper 使用 MyBatis Plus 的代码生成器来生成实体类和 Mapper 接口,具体使用方法可以参考官方文档。 4. 定义 Service 接口和实现类 在 Service 接口中定义对实体类的操作方法,如增加、删除、修改、查询等。在实现类中使用 MyBatis Plus 的方法来实现这些操作。 5. 编写 Controller 在 Controller 中定义接口,用于接收前端的请求并调用对应的 Service 方法。可以使用注解来实现请求映射等功能。 6. 配置页面模板引擎 可以选择 Thymeleaf、Freemarker 等页面模板引擎来实现前端页面的渲染,具体使用方法可以参考官方文档。 7. 运行项目 可以使用 Maven 打包项目并运行,也可以使用 IDE 自带的运行功能。 以上就是使用 Spring Boot 和 MyBatis Plus 开发网站项目的基本步骤。当然,实际开发中还有很多需要注意的细节,需要根据具体情况进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值