pagehelper分页插件

2019-12-05  本文已影响0人  IT小池

pagehelper官网https://pagehelper.github.io/docs/howtouse/

导包:

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

然后在 spring.xml配置文件中配置如下:

<!-- 配置 sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 加载 mybytais 配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 指定映射文件路径 -->
        <property name="mapperLocations" value="classpath:cm/xiaochi/ssm/mapper/*.xml"/>
        <!-- 包别名:因为 mybatis 的 xml 文件中需要写类的全限定名,较繁琐,以便在 mybatis 的 resultType 返回类型的时候用 -->
        <property name="typeAliasesPackage" value="cm.xiaochi.ssm.pojo"/>
        <!-- 分页插件 -->
        <property name="plugins">
            <list>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                           <!-- 指定数据库类型 --> 
                            <prop key="helperDialect">mysql</prop>
                        </props>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

在代码中使用:

@RequestMapping("/findAll")
public String findAll(Integer pageNum,Model model){
    // 分页  pageNum当前页,页面传递
    if (pageNum == null){
        pageNum = 1;// 默认1
    }
    int size = 5;// 每页显示条数
    // 设置分页
    PageHelper.startPage(pageNum,size);
    // 查询数据
    List<Product> products = productService.selectAll();
    // 将查询出的结构封装到 PageInfo 对象中
    PageInfo<Product> productPageInfo = new PageInfo<Product>(products);

    model.addAttribute("productPageInfo",productPageInfo);
    return "productManager";
}
上一篇 下一篇

猜你喜欢

热点阅读