springmvc和springboot做分页查询

第一种:springmvc

1、  pom.xml引入分页插件:

    <artifactid>pagehelper</artifactid>

    <version>5.1.9</version>  

2、 在spring配置文件中的sqlSessionFactory中引入分页插件

 <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageInterceptor">
                <!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
                <property name="properties">
                    <value>
                        helperDialect=mysql//该属性会自动的进行数据库链接的识别,自动配置适用的分页方式。使用sqlserver2012时,要手动声明,若不声明就会默认使用sqlserver2005
                        reasonable=true
                        supportMethodsArguments=true
                        params=count=countSql
                        autoRuntimeDialect=true
                    </value>
                </property>
            </bean>
        </array>
    </property>

     在serviceImpl接口实现类中重构查询方法:

      本例中pageSize采用静态常量的方式:

                      public class CommonUtil{

        public static Integer pageSize=10;

      }

      public PageInfo<Emp> queryAll(int pageNum){

        if(pageNum<=0) pageNum=1;

        PageHelper.startPage(pageNum,CommonUtil.pageSize);

        List<emp> emps=empDao.queryAll();

        PageInfo<Emp> pageinfo =new PageInfo<>(emps);

        return pageInfo;

      }

    在controller中:

      

    返回的数据是这个样子(浏览器中json格式):

    

    最后就是在前端页面设置信息,下面是相应截图,用的bootstrap,在js方法中分页数据用了pageinfo进行了接收

    

    分页栏相关栏目的设置

    

    效果:

    

第二种:在springboot中集成pagehelper

    1、pom.xml中引入jar  

<!--pagehelper-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.3</version>
</dependency>

      2、application.properties 配置

#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

     3、实现层代码和springmvc通用

 

posted @ 2020-06-14 21:39  橙汁one  阅读(713)  评论(0编辑  收藏  举报