SpringBoot分页查询(使用插件pagehelper)

这里的分页查询是使用了  github上别人写的一个插件。

步骤一:引入依赖

        <!-- github上的一个mybatis分页插件 -->
		<dependency>
		    <groupId>com.github.pagehelper </groupId>
		    <artifactId>pagehelper</artifactId>
		    <version>4.1.0</version>
		</dependency>

步骤二:编写配置类

@Configuration
public class MybatisConfiguration {

	@Bean
	 public PageHelper pageHelper(){
		 System.out.println("mybatis MybatisConfiguration.pageHelper()");
		 PageHelper pageHelper=new PageHelper();
		 Properties p=new Properties(); 
		 p.setProperty("offsetAsPageNum", "true");
		 p.setProperty("rowBoundsWithCount", "true");
		 pageHelper.setProperties(p);
		 return pageHelper;
	 }
}

步骤三:在控制层需要使用分页的地方调用即可。

    @ResponseBody
    @RequestMapping(value="/selectAll",method=RequestMethod.GET)
    public List<Person> selectAll(){
    	//startPage(第几页, 每页显示的条数)
    	PageHelper.startPage(1, 2);
    	return personService.selectAll();
    }
     

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot项目中集成PageHelper分页插件的步骤如下: 1. 打开Maven库,找到com.github.pagehelper:pagehelper-spring-boot-starter。 2. 确定需要使用的版本号,比如1.4.1。 3. 在项目的pom.xml文件中添加PageHelper的依赖。可以使用如下代码: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.1</version> </dependency> ``` 4. 在application.properties或application.yml配置文件中进行配置,添加以下属性: ``` # 开启分页支持 pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql ``` 这里的配置是针对MySQL数据库的,如果你使用的是其他数据库,需要根据对应的数据库类型进行配置。 5. 在需要分页的Mapper方法上使用PageHelper.startPage方法来开启分页功能。例如: ``` @Mapper public interface UserMapper { List<User> getUsers(); } ``` 在UserMapper接口中的方法上加上注解@Select,并在方法内部使用PageHelper.startPage方法开启分页,如下所示: ``` @Select("SELECT * FROM user") List<User> getUsers(); ``` 6. 运行项目,分页功能就会生效。 通过以上步骤,你就可以在Spring Boot项目中成功集成PageHelper分页插件了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [springboot整合分页插件PageHelper](https://blog.csdn.net/weixin_42408447/article/details/117528795)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Springboot集成pagehelper 分页插件](https://blog.csdn.net/weixin_58696998/article/details/124397756)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值