Mybatis分页插件PageHelper快速入门使用

Mybatis分页插件

mybatis的分页插件可以自动根据项目使用的数据库生成对应的分页sql
在执行目标sql之前先拦截(PageInterceptor拦截类)修改对应的sql(添加分页的sql片段)
【例:】select * from xx_tabl
–>select * from xx_tabl limit?,?
–>select count(0) from xx_tabl

引入依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
     <version>5.1.2</version>
 </dependency>

mybatis-config.xml添加分页配置,需要写在settings之后

PageHelper插件4.0.0以后的版本支持自动识别使用的数据库,可以不用配置
<propertyname=“dialect” value=“mysql” />

<plugins>
		<!-- com.github.pagehelper为PageHelper类所在包名 -->
		<plugin interceptor="com.github.pagehelper.PageInterceptor">
			<!--当pageNum<=0时,将pageNum设置为1-->
			<!--当pageNum>pages时,将pageNum设置为pages-->
			<property name="reasonable" value="true"/>
		</plugin>
	</plugins>

完整的mybatis-config.xml如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<settings>
		<!--<setting name="lazyLoadingEnabled" value="true"/>-->
		<setting name="cacheEnabled" value="true"/>
		<setting name="aggressiveLazyLoading" value="false"/>
		<setting name="lazyLoadTriggerMethods" value="clone"/>
	</settings>
	<plugins>
		<!-- com.github.pagehelper为PageHelper类所在包名 -->
		<plugin interceptor="com.github.pagehelper.PageInterceptor">
			<!--当pageNum<=0时,将pageNum设置为1-->
			<!--当pageNum>pages时,将pageNum设置为pages-->
			<property name="reasonable" value="true"/>
		</plugin>
	</plugins>
</configuration>

然后通过PageInfo类可以看出,mybatis分页插件已经帮我们做好了分页结果类,我们只需要调用PageInfo(List<T> list)

使用:
xxxServiceImpl

@Autowired
private xxxMapper xxxMapper;
@Override
public PageInfo<xxx> query (QueryObject qo){
//告诉mybatis下面紧跟的查询sql需要分页
PageHelper.startPage(qo.getCurrentPage,qo.getPageSize);
//sql查询
List<xxx> list=xxxMapper.selectForList(qo);
return new PageInfo<xxx>(list);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis PageHelper是一个用于实现分页查询的插件,它可以帮助我们快速实现分页功能,并且与Mapper.xml完全解耦,避免了直接编写分页SQL的麻烦。在使用MyBatis PageHelper时,可以根据不同的情况进行配置。 如果你使用的是Spring Boot,可以直接配置几个属性来启用MyBatis PageHelper。在application.properties或application.yml文件中,添加以下配置项: ``` mybatis.configuration-properties.helperDialect=mysql mybatis.configuration-properties.offsetAsPageNum=true mybatis.configuration-properties.rowBoundsWithCount=true mybatis.configuration-properties.reasonable=true mybatis.configuration-properties.mapper-locations=mybatis/mapper/*.xml ``` 其中,helperDialect指定了数据库的方言,offsetAsPageNum设置为true表示使用RowBounds分页方式,rowBoundsWithCount设置为true表示查询总数时会同时执行count查询,reasonable设置为true表示当pageNum<=0或pageNum>pages时会自动修正为合理的值,mapper-locations指定了Mapper.xml文件的位置。 另外,如果你使用的是Spring Boot,还可以直接引入pagehelper-spring-boot-starter依赖,它会自动配置PageHelper,省去了许多不必要的配置。 ```xml <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.12</version> </dependency> ``` 通过以上配置,你就可以在MyBatis使用PageHelper来实现分页查询了。 #### 引用[.reference_title] - *1* [【Mybatis使用PageHelper进行分页查询](https://blog.csdn.net/Flying_Ape/article/details/128098911)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Mybatis分页插件——PageHelper快速入门](https://blog.csdn.net/weixin_52850476/article/details/124802877)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值