好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Mybatis中的PageHelper的执行流程分析

PageHelper Mybatis的执行流程

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

项目示例结构

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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

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

查看更多关于Mybatis中的PageHelper的执行流程分析的详细内容...

  阅读:20次