springboot使用PageInterceptor分页插件

1.pom引入

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

2.为sqlsessionfactory添加拦截器

    /**
     * 返回数据库的会话工厂
     *
     * @param ds
     * @return
     * @throws Exception
     */
    @Bean(name = "SqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dbSource") DataSource ds) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(ds);

        //添加分页支持
        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        interceptor.setProperties(properties);
        bean.setPlugins(new Interceptor[]{interceptor});

        return bean.getObject();
    }

3.使用

	@Override
	public PageBean<Map<String, Object>> listMethod(PageReqDto pageReqDto) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		PageHelper.startPage(pageReqDto.getPageNum(), pageReqDto.getPageSize());
		list = xxDao.listMethod(pageReqDto);
		PageBean<Map<String, Object>> p = new PageBean<>(list);
		return p;
	}

public class PageReqDto extends BaseReqDto {

	/**
	 * 第几页
	 */
	private Integer pageNum;

	/**
	 * 每页多少行
	 */
	private Integer pageSize;

	/**
	 * 默认第几页
	 */
	public static final Integer PAGENUM_DEFAULT = 1;

	/**
	 * 默认每页显示多少行
	 */
	public static final Integer PAGESIZE_DEFAULT = 10;

	public Integer getPageNum() {
		if (null == pageNum) {
			pageNum = PAGENUM_DEFAULT;
		}
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {

		this.pageNum = pageNum;
	}

	public Integer getPageSize() {
		if (null == pageSize) {
			pageSize = PAGESIZE_DEFAULT;
		}
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {

		this.pageSize = pageSize;

	}
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中有多个流行的分页插件可供选择,其中最常用的是MyBatis分页插件Spring Data JPA分页插件。下面是使用这两个插件的简单介绍。 ## MyBatis分页插件 1. 在项目的`pom.xml`中添加以下依赖: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency> ``` 2. 在MyBatis的配置文件中添加插件配置: ```xml <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"/> </plugins> ``` 3. 在需要分页的Mapper接口中添加方法: ```java List<MyObject> selectByPage(@Param("pageNum") int pageNum, @Param("pageSize") int pageSize); ``` 4. 在Mapper对应的XML文件中编写分页查询的SQL语句: ```xml <select id="selectByPage" resultType="com.example.demo.MyObject"> SELECT * FROM my_table </select> ``` 5. 在业务逻辑中调用Mapper方法,传入分页参数: ```java List<MyObject> myObjects = myMapper.selectByPage(pageNum, pageSize); ``` ## Spring Data JPA分页插件 1. 在项目的`pom.xml`中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 2. 在需要分页的Repository接口中继承`JpaRepository`,并添加方法: ```java Page<MyObject> findAll(Pageable pageable); ``` 3. 在业务逻辑中调用Repository方法,传入分页参数: ```java Pageable pageable = PageRequest.of(pageNum, pageSize); Page<MyObject> myObjectPage = myObjectRepository.findAll(pageable); List<MyObject> myObjects = myObjectPage.getContent(); ``` 以上是使用MyBatis和Spring Data JPA分页插件的简单示例。不同的分页插件可能有一些差异,具体的使用方法可以参考相应的文档和示例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值