时间:2023-02-21 13:44:42 | 栏目:JAVA代码 | 点击:次
PageHelper Mybatis的执行流程

这就是整个mybatis框架的执行情况。
它主要作用在Executor执行器与mappedeStatement之间
也就是说mybatis可以在插件中获得要执行的sql语句
在sql语句中添加limit语句,然后再去对sql进行封装,从而可以实现分页处理。
引入依赖
<!--分页插件 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配置
#整合数据源
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<Category> {
}
CateService接口
import cn.pojo.Category;
import java.util.*;
public interface CateService {
public List<Category> 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<Category> pageSelect(int page, int col) {
// 使用分页表明,从第几页开始,一页多少条数据
PageHelper.startPage(page,col);
// 使用Plus进行查询所有,因为PageHelper插件会进行sql的limit的拼接
List<Category> categories = categoryDao.selectList(null);
return categories;
}
}
核心代码
// 使用分页表明,从第几页开始,一页多少条数据
PageHelper.startPage(page,col);
// 使用Plus进行查询所有,因为PageHelper插件会进行sql的limit的拼接
List<Category> categories = categoryDao.selectList(null);
查看结果
