mybatis分页插件pagehelper的使用

数据展示,离不开分页,我们可以生写,更可以使用插件。pagehelper可以方便的帮我们做到这一点。

怎么用呢?

1、如果是maven 项目,首先得添加maven依赖。我使用的是2018年最新版本。

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

2、核心文件的配置有两种方式,我这里使用 spring核心文件的配置方式

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>

    <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties">
                    <!-- config params as the following -->
                    <value>
                        helperDialect:mysql
                        reasonable:false
                    </value>
                </property>
            </bean>
        </array>
    </property>
</bean>

3、新建接口和实现类

public interface TuserServices {
    PageInfo<Tuser> queryPaging(int  pageNum, int pageSize);
}

实现类核心代码如下:

@Override
public PageInfo<Tuser> queryPaging(int  pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    List<Tuser> tuserList=tuserMapper.queryPaging();
    PageInfo<Tuser> pageInfo=new PageInfo<Tuser>(tuserList);
    return pageInfo;
}

完成了这一步,就可以进入测试了。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:ApplicationContext.xml"})
public class TuserServicesTest {
    @Resource
    private TuserServices tuserServices;

    @Test
    public void queryPaging() throws Exception {
        PageInfo<Tuser> pageInfo=tuserServices.queryPaging(0,2);
        List<Tuser> list=pageInfo.getList();
        for (Tuser tuser : list) {
            System.out.println(tuser);
        }
    }
}

结果如下:

运行正常。当然也有其它的运行方法,具体方法请去官网查找。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值