springmvc使用PageHelper分页插件

1、导入相关依赖

<!--使用PageHelper分页插件-->
<dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.0.0</version>
</dependency>

2、在applicationContext.xml配置文件中增加PageHelper的定义

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"/>
        <property name="typeAliasesPackage" value="com.zl.newHouse.entity"/>
        <!-- 添加全局配置 -->
        <property name="globalConfig" ref="globalConfig"/>
        <!-- 配置PageHelper分页插件 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="helperDialect">oracle</prop> //如果是mysql只需要更改为mysql即可
                            <prop key="reasonable">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

测试:

条件类:

springmvc使用PageHelper分页插件
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HouseQueryInfo {
    private int pageIndex;//当前页码(页面传递)
    private int pageSize;//页容量(后台设置)
    private String title;
    private String price_on;
    private String price_down;
    private String street_id;
    private String type_id;
    private String floorage_on;
    private String floorage_down;
    
}
View Code

编写一个接口:

public interface  IHouseService extends IService<House>{
    
    //根据条件查询所有房屋信息,分页显示  findAllHouse自己编写多表多条件查询的sql语句
    List<House> findAllHouse(HouseQueryInfo houseInfo) throws Exception;

    //使用pagehelper分页查询
    public PageInfo<House> findBookPage(HouseQueryInfo houseInfo) throws Exception;
}

编写一个接口的实现类:

//根据条件查询所有房屋信息,分页显示
@Override
public List<House> findAllHouse(HouseQueryInfo houseInfo) throws Exception {
    return houseMapper.findAllHouse(houseInfo);
}
//根据查询出来的数据分页显示
@Override
public PageInfo<House> findBookPage(HouseQueryInfo houseInfo) throws Exception {
        PageHelper.startPage(houseInfo.getPageIndex(),houseInfo.getPageSize());
    List<House> bookHouses = houseMapper.findAllHouse(houseInfo);
    PageInfo pageinfo = new PageInfo(bookHouses);
    return pageinfo;
}

测试数据:

@Test
public void testpage() throws  Exception{
    HouseQueryInfo houseInfo = new HouseQueryInfo(1,3,"", "", "", "", "", "", "");
    PageInfo pageInfo = houseService.findBookPage(houseInfo);
    System.out.println("总数量" + pageInfo.getTotal());
    System.out.println("当前页查询记录数" + pageInfo.getList().size());
    System.out.println("当前页码" + pageInfo.getPageNum());
    System.out.println("每页显示数量" + pageInfo.getPageSize());
    System.out.println("总页" + pageInfo.getPages());
    System.out.println("当前页查询记录" + pageInfo.getList());
}

 

上一篇:使用PageHelper得到的值没有list


下一篇:在web中的分页操作