mybatis 集成PageHelper分页插件

1. 需要的jar包,maven配置如下

   pagehelper依赖

<!-- mybatis pager-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>4.1.0</version>
    </dependency>

可以使用jsqlparser解析sql语句

<!-- jsqlparser 解析sql语句 -->
    <dependency>
      <groupId>com.github.jsqlparser</groupId>
      <artifactId>jsqlparser</artifactId>
      <version>0.9.4</version>
    </dependency>

2. 配置sqlSessionFactory时,配置分页插件

<!-- 创建sqlSessionFactory,同时指定数据源 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 自动扫描mapper目录, 省掉mybatis-config.xml里的手工配置 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <!-- 分页插件 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageHelper">
                    <property name="properties">
                        <value>
                            dialect=sqlserver
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

3. 如何使用

下面代码表示根据分类id,页面大小,页数查询到具体的页面信息,使用PageInFo类来承载信息

public ResultMap<PageInfo> getArticlesByCategoryId(int categoryId,int pageNum,int pageSize){
        ArticleCategory category = articleCategoryDao.selectByPrimaryKey(categoryId);
        if(category==null){
            return ResultMap.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),"文章分类参数错误");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articles = articleDao.getArticleList(categoryId);
        List<Articlevo> articleVos= new ArrayList<>();
        for(Article item:articles){
            articleVos.add(CommonUtil.articleToVo(item));
        }
        PageInfo pageInfo = new PageInfo(articleVos);
        return ResultMap.createBySuccess(pageInfo);

    }
Dao层接口以及对应的xml代码如下
 List<Article> getArticleList(int categoryId);
<select id="getArticleList" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    SELECT
    ArticleId, [User], Title, PublishDate, SchoolYear, College, Category
    from ArticleInfo where Category=#{categoryId}  order by PublishDate DESC
  </select>
可以看到,sql代码并不需要分页参数。

结果:
传入相应的查询参数


得到返回的分页信息:data里面为PageInFo包装的信息。

{
	"status": 1,
	"msg": null,
	"data": {
		"pageNum": 1,
		"pageSize": 2,
		"size": 2,
		"orderBy": null,
		"startRow": 0,
		"endRow": 1,
		"total": 2,
		"pages": 1,
		"list": [{
			"articleid": 28,
			"user": "55",
			"title": "55",
			"publishdate": "2018-02-03 00:00:00",
			"schoolyear": "22",
			"college": "22",
			"category": "1",
			"contents": null
		}, {
			"articleid": 29,
			"user": "11",
			"title": "33",
			"publishdate": "2018-01-01 00:00:00",
			"schoolyear": "11",
			"college": "11",
			"category": "1",
			"contents": null
		}],
		"firstPage": 1,
		"prePage": 0,
		"nextPage": 0,
		"lastPage": 1,
		"isFirstPage": true,
		"isLastPage": true,
		"hasPreviousPage": false,
		"hasNextPage": false,
		"navigatePages": 8,
		"navigatepageNums": [1]
	},
	"success": true
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值