MyBatisPlus(SpringBoot版)的分页插件
目录
一、前置工作:
1.整体项目目录结构
2.创建普通javamaven项目。
3.导入依赖,改造成springboot项目
4.配置启动类
5.创建service接口及其实现类
6.创建接口Mapper
7.配置数据源
8.创建数据库表
二、使用MP(mybatisplus)的分页插件
二、使用自定义的分页插件
MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能。
一、前置工作:
1.整体项目目录结构
2.创建普通javamaven项目。
3.导入依赖,改造成springboot项目
依赖:
4.0.0 com.qcby SpringBootMybatisPlus 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.4.0 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test com.baomidou mybatis-plus-boot-starter 3.5.1 org.projectlombok lombok true mysql mysql-connector-java 5.1.46 runtime org.springframework.boot spring-boot-maven-plugin
4.配置启动类
package com.qcby; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.qcby.SpringBoot.mapper") public class SpringBootApplication1 { public static void main(String[] args) { SpringApplication.run(SpringBootApplication1.class, args); } }
5.配置实体类
package com.qcby.SpringBoot.pojo; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("t_product") public class Product { private Long id; private String name; private Integer price; private Integer version; }
package com.qcby.SpringBoot.pojo; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data //set和get方法 @AllArgsConstructor //全参构造器 @NoArgsConstructor //无参构造器 @TableName("t_user") public class User { //因为用到雪花算法,所以用Long属性 @TableId private Long id; private String name; private Integer age; private String email; @TableLogic private Integer isDeleted; }
5.创建service接口及其实现类
package com.qcby.SpringBoot.service; import com.baomidou.mybatisplus.extension.service.IService; import com.qcby.SpringBoot.pojo.User; /** * UserService继承IService模板提供的基础功能 */ public interface UserService extends IService { }
package com.qcby.SpringBoot.service.Impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.qcby.SpringBoot.mapper.UserMapper; import com.qcby.SpringBoot.pojo.User; import com.qcby.SpringBoot.service.UserService; import org.springframework.stereotype.Service; /** * ServiceImpl实现了IService,提供了IService中基础功能的实现 * 若ServiceImpl无法满足业务需求,则可以使用自定的UserService定义方法,并在实现类中实现 */ @Service public class UserServiceImpl extends ServiceImpl implements UserService { }
6.创建接口Mapper
package com.qcby.SpringBoot.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.qcby.SpringBoot.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; @Mapper public interface UserMapper extends BaseMapper { }
package com.qcby.SpringBoot.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.qcby.SpringBoot.pojo.Product; import org.apache.ibatis.annotations.Mapper; @Mapper public interface ProductMapper extends BaseMapper { }
7.配置数据源
在application.yaml中配置信息。
spring: # 配置数据源信息 datasource: # 配置数据源类型 type: com.zaxxer.hikari.HikariDataSource # 配置连接数据库信息 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false username: root password: root # 配置MyBatis日志 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
8.创建数据库表
二、使用MP(mybatisplus)的分页插件
首先要在容器中配置一个mybatisplus分页插件的bean。
可以自定义一个配置类,也可以在启动类中配置,因为启动类也是一个配置类。
package com.qcby.SpringBoot.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MybatisPlusConfig { /** * 分页插件 * 构建一个拦截来处理分页 * 每个数据库厂商对于分页的实现语法有差别,因此,在声明该拦截时,需要指定应用的数据库类型 * @return */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//由于各个数据库的语法会有差别,因此,要指明数据库类型 return interceptor; } }
编写测试类
package com.qcby; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.qcby.SpringBoot.mapper.ProductMapper; import com.qcby.SpringBoot.mapper.UserMapper; import com.qcby.SpringBoot.pojo.Product; import com.qcby.SpringBoot.pojo.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest public class Test2 { @Autowired private UserMapper userMapper; @Autowired private ProductMapper productMapper; @Test public void testPage(){ //设置分页参数 Page page = new Page(1, 5); userMapper.selectPage(page, null); //获取分页数据 List list = page.getRecords(); list.forEach(System.out::println); System.out.println("当前页:"+page.getCurrent()); System.out.println("每页显示的条数:"+page.getSize()); System.out.println("总记录数:"+page.getTotal()); System.out.println("总页数:"+page.getPages()); System.out.println("是否有上一页:"+page.hasPrevious()); System.out.println("是否有下一页:"+page.hasNext()); } }
二、使用自定义的分页插件
在usermapper中加入方法
package com.qcby.SpringBoot.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.qcby.SpringBoot.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; @Mapper public interface UserMapper extends BaseMapper { /** * 根据年龄查询用户列表,分页显示 * @param page 分页对象 ,xml中可以从里面进行取值 ,传递参数 Page 即自动分页 ,必须放在第一位 * @param age 年龄 * @return */ /** * 不用加limit语句,因为配置了一个拦截的插件,只需要传入page对象,还是使用的MP的分页插件 * @param page * @param age * @return */ @Select("SELECT id,name,age,email FROM t_user WHERE age > #{age}") IPage selectPageVo(@Param("page") Page page, @Param("age") Integer age); }
package com.qcby; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.qcby.SpringBoot.mapper.ProductMapper; import com.qcby.SpringBoot.mapper.UserMapper; import com.qcby.SpringBoot.pojo.Product; import com.qcby.SpringBoot.pojo.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest public class Test2 { @Autowired private UserMapper userMapper; @Autowired private ProductMapper productMapper; @Test public void testSelectPageVo(){ //设置分页参数 Page page = new Page(1, 5); userMapper.selectPageVo(page, 20); //获取分页数据 List list = page.getRecords(); list.forEach(System.out::println); System.out.println("当前页:"+page.getCurrent()); System.out.println("每页显示的条数:"+page.getSize()); System.out.println("总记录数:"+page.getTotal()); System.out.println("总页数:"+page.getPages()); System.out.println("是否有上一页:"+page.hasPrevious()); System.out.println("是否有下一页:"+page.hasNext()); } }
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理!
部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!
图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!