Mybatis中的PageHelper的執(zhí)行流程分析
PageHelper Mybatis的執(zhí)行流程

- mybatis中首先要在配置文件中配置一些東西
- 然后根據(jù)這些配置去創(chuàng)建一個會話工廠
- 再根據(jù)會話工廠創(chuàng)建會話,會話發(fā)出操作數(shù)據(jù)庫的sql語句
- 然后通過執(zhí)行器操作數(shù)據(jù)
- 再使用mappedStatement對數(shù)據(jù)進行封裝
這就是整個mybatis框架的執(zhí)行情況。
插件的執(zhí)行
它主要作用在Executor執(zhí)行器與mappedeStatement之間
也就是說mybatis可以在插件中獲得要執(zhí)行的sql語句
在sql語句中添加limit語句,然后再去對sql進行封裝,從而可以實現(xiàn)分頁處理。
SpringBoot操作PageHelper
引入依賴
<!--分頁插件 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配置
#整合數(shù)據(jù)源
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項目示例結(jié)構(gòu)

CategoryDao
因為使用了MybatisPlus所以有些方法可以不去實現(xiàn),通過Plus自己編寫
@Mapper
public interface CategoryDao extends BaseMapper<Category> {
}CateService接口
import cn.pojo.Category;
import java.util.*;
public interface CateService {
public List<Category> pageSelect(int page,int col);
}CateServiceImple實現(xiàn)
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) {
// 使用分頁表明,從第幾頁開始,一頁多少條數(shù)據(jù)
PageHelper.startPage(page,col);
// 使用Plus進行查詢所有,因為PageHelper插件會進行sql的limit的拼接
List<Category> categories = categoryDao.selectList(null);
return categories;
}
}核心代碼
// 使用分頁表明,從第幾頁開始,一頁多少條數(shù)據(jù)
PageHelper.startPage(page,col);
// 使用Plus進行查詢所有,因為PageHelper插件會進行sql的limit的拼接
List<Category> categories = categoryDao.selectList(null);查看結(jié)果

到此這篇關(guān)于Mybatis的PageHelper的文章就介紹到這了,更多相關(guān)Mybatis的PageHelper內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Retry?實現(xiàn)樂觀鎖重試實踐記錄
本文介紹了在秒殺商品SKU表中使用樂觀鎖和MybatisPlus配置樂觀鎖的方法,并分析了測試環(huán)境和生產(chǎn)環(huán)境的隔離級別對樂觀鎖的影響,通過簡單驗證,展示了在可重復(fù)讀和讀已提交隔離級別下的不同行為,感興趣的朋友一起看看吧2025-03-03
談?wù)劄镴AXB和response設(shè)置編碼,解決wechat4j中文亂碼的問題
中文亂碼是每個程序員都會遇到的問題,本篇文章主要介紹了談?wù)劄镴AXB和response設(shè)置編碼,解決wechat4j中文亂碼的問題,具有一定的參考價值,有興趣的可以了解一下。2016-12-12
Springboot?整合maven插口調(diào)用maven?release?plugin實現(xiàn)一鍵打包功能
這篇文章主要介紹了Springboot?整合maven插口調(diào)用maven?release?plugin實現(xiàn)一鍵打包功能,整合maven-invoker使程序去執(zhí)行mvn命令,結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-03-03
java?SpringBoot注解@Async不生效的解決方法
大家好,本篇文章主要講的是java?SpringBoot注解@Async不生效的解決方法,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
SpringBoot整合JavaMail通過阿里云企業(yè)郵箱發(fā)送郵件的實現(xiàn)
這篇文章主要介紹了SpringBoot整合JavaMail通過阿里云企業(yè)郵箱發(fā)送郵件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
pom.xml中解決Provides?transitive?vulnerable?dependency?mave
這篇文章主要介紹了在pom.xml中如何解決Provides?transitive?vulnerable?dependency?maven:org.yaml:snakeyaml:1.33警告問題,需要的朋友可以參考下2023-06-06

