目录
pageHelper分页插件初体验
/      

pageHelper分页插件初体验

pageHelper分页插件初体验

**官方文档: ** https://pagehelper.github.io/docs/howtouse

以本站获取虎牙直播的api为demo

需要的jar包

  • pagehelper.jar
  • sql的解析工具, jsqlparser.jar

maven引入依赖

    <!--pageHelper 分页插件-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>LATEST</version>
    </dependency>

引入之后, maven会帮我们自动下载那两个jar包

配置拦截插件

如果我们只使用mybatis, 可以在mybatis-config.xml配置

<!--
    plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
    properties?, settings?,
    typeAliases?, typeHandlers?,
    objectFactory?,objectWrapperFactory?,
    plugins?,
    environments?, databaseIdProvider?, mappers?
-->
<plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
        <property name="helperDialect" value="mysql"/>
        <property name="reasonable" value="true"/>
	</plugin>
</plugins>

如果我们使用的是ssm, 我们可以在mybatis-config.xml配置上述参数

也可以在applicationContext.xml配置下列参数, 注意plugins的位置. 注意: 如果mybatis配置了, spring就不能再配置了.

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- 注意其他配置 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <!--使用下面的方式配置参数,一行配置一个 -->
                        <props>
                            <!--常用的两个,helperDialect配置操作数据库的类型-->
                            <prop key="helperDialect">mysql</prop>
                            <!--reasonable配置分页合理化(超过第一页第一页,超过最后一页即为最后一页)-->
                            <prop key="reasonable">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
</bean>

后台调用

控制器

    @RequestMapping("/huya")
    public String huya(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "pageSize", defaultValue = "60") Integer pageSize, Model model){
        String huyas = huyaService.findAll(page, pageSize);
        model.addAttribute("huya", huyas);
        return "/huya.jsp";
    }

HuyaService

    public String findAll(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        PageInfo pageInfo = new PageInfo(huyaDao.findAll());
        StringBuilder builder = new StringBuilder();
        List<Huya> list = pageInfo.getList();
        builder.append("huya({pages: \"" + pageInfo.getPages() + "\", msg: \"");
        // deaList 是自己写的方法 其实就是将集合手动转成json格式 的字符串
        return dealList(builder, list);
    }

HuyaDao

    @Select("select * from huya")
    List<Huya> findAll();

pageInfo的一些参数我们需要知道

	private int pageNum; // 当前页
    private int pageSize; // 每页显示的数量
    private int size;	// 当前页的条数
    private int startRow; // 从第几条开始
    private int endRow; // 从第几条结束
    private int pages; // 总共有多少页
    private int prePage; // 上一页
    private int nextPage; // 下一页
    private boolean isFirstPage; // 是否第一页
    private boolean isLastPage; // 是否最后一页
    private boolean hasPreviousPage;
    private boolean hasNextPage;
    private int navigatePages; 
    private int[] navigatepageNums;
    private int navigateFirstPage;
    private int navigateLastPage;

其他功能, 以后慢慢研究........


标题:pageHelper分页插件初体验
作者:gitsilence
地址:https://blog.lacknb.cn/articles/2019/11/16/1577974158328.html