服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Mybatis中的PageHelper的执行流程分析

Mybatis中的PageHelper的执行流程分析

2022-08-07 13:55树_tree Java教程

这篇文章主要介绍了Mybatis的PageHelper执行流程,本文给大家介绍介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

PageHelper Mybatis的执行流程

Mybatis中的PageHelper的执行流程分析

  • mybatis中首先要在配置文件中配置一些东西
  • 然后根据这些配置去创建一个会话工厂
  • 再根据会话工厂创建会话,会话发出操作数据库的sql语句
  • 然后通过执行器操作数据
  • 再使用mappedStatement对数据进行封装

这就是整个mybatis框架的执行情况。

插件的执行

它主要作用在Executor执行器与mappedeStatement之间

也就是说mybatis可以在插件中获得要执行的sql语句

在sql语句中添加limit语句,然后再去对sql进行封装,从而可以实现分页处理。

SpringBoot操作PageHelper

引入依赖

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--分页插件 pagehelper -->
       <dependency>
           <groupId>com.github.pagehelper</groupId>
           <artifactId>pagehelper-spring-boot-starter</artifactId>
           <!-- 特别注意版本问题 -->
           <version>1.2.13</version>
       </dependency>
 
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>8.0.18</version>
       </dependency>
       <dependency>
           <groupId>com.baomidou</groupId>
           <artifactId>mybatis-plus-boot-starter</artifactId>
           <version>3.3.1</version>
       </dependency>

yaml配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#整合数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: ok
    url: jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
#Mybatis-Plus的配置
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 配置在控制台打印 sql语句
  # 配置自定义sql语句的 *mapper.xml 文件位置
  mapper-locations: classpath:**/mapper/**.xml
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

项目示例结构

Mybatis中的PageHelper的执行流程分析

CategoryDao

因为使用了MybatisPlus所以有些方法可以不去实现,通过Plus自己编写

?
1
2
3
@Mapper
public interface CategoryDao extends BaseMapper<Category> {
}

CateService接口

?
1
2
3
4
5
import cn.pojo.Category;
import java.util.*;
public interface CateService {
    public List<Category> pageSelect(int page,int col);
}

CateServiceImple实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import javax.annotation.Resource;
import java.util.List;
@Service
public class CateServiceImple implements CateService {
    @Resource
    CategoryDao categoryDao;
    @Override
    public List<Category> pageSelect(int page, int col) {
//        使用分页表明,从第几页开始,一页多少条数据
        PageHelper.startPage(page,col);
        
//        使用Plus进行查询所有,因为PageHelper插件会进行sql的limit的拼接
        List<Category> categories = categoryDao.selectList(null);
 
        return categories;
    }
}

核心代码

?
1
2
3
4
//        使用分页表明,从第几页开始,一页多少条数据
        PageHelper.startPage(page,col);
//        使用Plus进行查询所有,因为PageHelper插件会进行sql的limit的拼接
        List<Category> categories = categoryDao.selectList(null);

查看结果

Mybatis中的PageHelper的执行流程分析

到此这篇关于Mybatis的PageHelper的文章就介绍到这了,更多相关Mybatis的PageHelper内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_46401545/article/details/123127588

延伸 · 阅读

精彩推荐