企业级真实应用利用Mybatis-Plus进行分页查询处理

怎么导入依赖我在之前的文章里边有说过不理解的可以看看

你应该懂点Mybatis-plus,真的好用

1:了解Page<T>类的使用 

首先我们需要使用到Page类 ,建立一个Page类,泛式类型中放入我们需要输出的类,是列表的话就放入列表。

MyBatis Plus 是 MyBatis 的增强包装库,MyBatis 是一种流行的 Java 应用程序持久性框架。MyBatis Plus 提供了额外的特性和实用程序来简化数据访问层代码的开发。

在MyBatis Plus中,该类Page<T>是用于分页的通用类。它代表一个数据页面,并提供检索有关当前页面的信息以及执行与分页相关的操作的方法。

Page<T>下面是MyBatis Plus 中类的一些常用方法和属性:

  1. getCurrent():返回当前页码。
  2. getSize():返回每页的记录数。
  3. getRecords():返回当前页面的记录列表。
  4. getTotal():返回所有页面的记录总数。
  5. getPages():返回总页数。
  6. hasNext():true如果有下一页则返回,false否则返回。
  7. hasPrevious()true如果有上一页则返回,false否则返回。
  8. next():移至下一页并返回一个新Page<T>对象。
  9. previous():移至上一页并返回一个新Page<T>对象。
  10. convert(Function<? super T, ? extends U> mapper):使用提供的映射函数转换当前页面上的记录。
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


public IPage<User> getUsersByPage(int pageNumber, int pageSize) {
   
    Page<User> page = new Page<>(pageNumber, pageSize);


    IPage<User> userPage = userMapper.selectPage(page, null);

    List<User> userList = userPage.getRecords();
    long total = userPage.getTotal();
    int currentPage = userPage.getCurrent();
    int pageSize = userPage.getSize();
    int totalPages = userPage.getPages();
    boolean hasNext = userPage.hasNext();
    boolean hasPrevious = userPage.hasPrevious();

    return userPage;
}

2.了解BaseMapper<T>


BaseMapper<T>MyBatis Plus 提供的一个接口,作为对T实体进行 CRUD 操作的基础映射器。它包括用于基本数据库操作(例如插入、更新、删除和查询记录)的常用方法。

以下是该接口提供的一些常用方法BaseMapper<User>

  • int insert(T entity): 将新记录插入到指定实体的数据库中。
  • int insertBatch(List<T> entityList):将指定实体列表的多条记录插入数据库。
  • int updateById(T entity):使用主键更新指定实体在数据库中的记录。
  • int deleteById(Serializable id):根据主键从数据库中删除记录。
  • T selectById(Serializable id):根据主键从数据库中检索单个记录。
  • List<T> selectBatchIds(Collection<? extends Serializable> idList):根据主键值列表从数据库中检索多条记录。
  • List<T> selectByMap(Map<String, Object> columnMap):根据列名和值的映射从数据库中检索记录。
  • IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper):执行分页查询并返回一页结果。

BaseMapper<User>接口提供这些方法作为默认实现,这意味着您可以直接使用它们,而无需编写任何额外的代码。但是,如果需要修改它们的行为,您也可以在自定义映射器接口中重写这些方法。

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    
    // Custom query methods
    
    User selectById(Long id);
    
    List<User> selectByUsername(String username);
    
    List<User> selectByAgeRange(@Param("minAge") int minAge, @Param("maxAge") int maxAge);
    
    // Custom update method
    
    int updateUsernameById(@Param("id") Long id, @Param("username") String username);
    
    // Custom delete method
    
    int deleteByUsername(String username);
    
}

通过扩展BaseMapper<T>,您的自定义映射器接口(例如 )UserMapper继承了这些方法,并且可以另外定义特定于User实体或应用程序要求的自定义方法。

使用BaseMapper<User>作为映射器的基本接口提供了一种在实体上执行基本 CRUD 操作的便捷方法,User而无需显式编写 SQL 查询。MyBatis Plus根据方法调用和注释生成SQL语句,使数据库操作更简单、更高效。

3:Mybatis-Plus使用PageHelper分页插件

第一步:引包

<!--SpringBoot框架必须引入此依赖-->
 <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.1</version>
 </dependency


<!--需要网页展示的需要加上这个依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>



<!--为什么不直接使用mybatis直接的依赖,需要是使用springboot那一套依赖呢,因为需要进行springboot跟mybatis适配所以不用下边的这个依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
        </dependency>

第二步:配置文件

使用mybatis下的PageHelper插件,不是只引入依赖就可以了。还需要在配置文件里边进行一系列的配置。

1:启动项配置文件

mybatis-plus:
  mapper-locations: classpath*:sql/**/*Mapper.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  database:
    pageHelper:
      helperDialect: mysql
      reasonable: true
      supportMethousArguments: true
      params: count=countSql




  
mybatis:
  mapper-locations: classpath*:sql/**/*Mapper.xml
  configuration:
    database-id: ${mybatis.database.provider.type:mysql}
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  database:
    pageHelper:
      helperDialect: mysql
      reasonable: true
      supportMethousArguments: true
      params: count=countSql

数据逻辑里边使用的是mybatis-plus就是用它的配置,使用mybatis就使用mybatis的配置。

第三步:使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Recently 祝祝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值