Mybatis中的PageHelper的执行流程分析

网友投稿 862 2022-10-26

Mybatis中的PageHelper的执行流程分析

Mybatis中的PageHelper的执行流程分析

PageHelper Mybatis的执行流程

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

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

插件的执行

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

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

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

SpringBoot操作PageHelper

引入依赖

com.github.pagehelper

pagehelper-spring-boot-starter

1.2.13

mysql

mysql-connector-java

8.0.18

com.baomidou

mybatis-plus-boot-starter

3.3.1

yaml配置

#整合数据源

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自己编写

@Mapper

public interface CategoryDao extends BaseMapper {

}

CateService接口

import cn.pojo.Category;

import java.util.*;

public interface CateService {

public List pageSelect(int page,int col);

}

CateServiceImple实现

import javax.annotation.Resource;

import java.util.List;

@Service

public class CateServiceImple implements CateService {

@Resource

CategoryDao categoryDao;

@Override

public List pageSelect(int page, int col) {

// 使用分页表明,从第几页开始,一页多少条数据

PageHelper.startPage(page,col);

// 使用Plus进行查询所有,因为PageHelper插件会进行sql的limit的拼接

List categories = categoryDao.selectList(null);

return categories;

}

}

核心代码

// 使用分页表明,从第几页开始,一页多少条数据

PageHehttp://lper.startPage(page,col);

// 使用Plus进行查询所有,因为PageHelper插件会进行sql的limit的拼接

List categories = categoryDao.selectList(null);

查看结果

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python实现快速排序
下一篇:python实现希尔排序
相关文章

 发表评论

暂时没有评论,来抢沙发吧~