zoukankan      html  css  js  c++  java
  • PageHelper使用

    PageHelper是国内非常优秀的一款开源的mybatis分页插件,它支持基本主流与常用的数据库,例如mysql、 oracle、mariaDB、DB2、SQLite、Hsqldb等。

    1、下载了解

    在 github 的项目地址:https://github.com/pagehelper/Mybatis-PageHelper

    在 gitosc 的项目地址:http://git.oschina.net/free/Mybatis_PageHelper

    2、这里介绍maven使用方式

    在pom.xml文件中添加:

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

    3、配置 MyBatis方式或Spring 配置者方式,二选一

    在 MyBatis 配置 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="param1" value="value1"/>
        </plugin>
    </plugins>

    在 Spring 配置文件中配置拦截器插件

       <!--spring 和 mybatis整合-->
        <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="druidDataSource"></property>
            <property name="typeAliasesPackage" value="com.boat.pojo"></property>
            <!-- oralce配置,分页插件 -->
            <property name="plugins">
                <array>
                    <bean class="com.github.pagehelper.PageInterceptor">
                        <property name="properties">
                            <props>
                     <!--mysql 或者 oracle 配置--> <prop key="helperDialect">oracle</prop> <prop key="reasonable">true</prop> </props> </property> </bean> </array> </property> </bean>

    配置参数说明:

    1. helperDialect :分页插件会自动检测当前的数据库链接,自动选择合适的分页方式。 你可以配置 helperDialect 属性来指定分页插件使用哪种方言。配置时,可以使用下面的缩写值: oracle , mysql , mariadb , sqlite , hsqldb , postgresql , db2 , sqlserver , informix , h2 , sqlserver2012 , derby 特别注意:使用 SqlServer2012 数据库时,需要手动指定为 sqlserver2012 ,否则会使用 SqlServer2005 的 方式进行分页。 你也可以实现 AbstractHelperDialect ,然后配置该属性为实现类的全限定名称即可使用自定义的实现方 法。

    2. offsetAsPageNum :默认值为 false ,该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为 true 时,会将 RowBounds 中的 offset 参数当成 pageNum 使用,可以用页码和页面大小两个参数进行分 页。

    3. rowBoundsWithCount :默认值为 false ,该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置 为 true 时,使用 RowBounds 分页会进行 count 查询。

    4. pageSizeZero :默认值为 false ,当该参数设置为 true 时,如果 pageSize=0 或者 RowBounds.limit = 0 就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是 Page 类型)。

    5. reasonable :分页合理化参数,默认值为 false 。当该参数设置为 true 时, pageNum<=0 时会查询第一 页, pageNum>pages (超过总数时),会查询最后一页。默认 false 时,直接根据参数进行查询。

    6. params :为了支持 startPage(Object params) 方法,增加了该参数来配置参数映射,用于从对象中根据属 性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable ,不配置映射的用默认值, 默认 值为 pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero 。

    7. supportMethodsArguments :支持通过 Mapper 接口参数来传递分页参数,默认值 false ,分页插件会从查 询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页。 使用方法 可以参考测试代码中的 com.github.pagehelper.test.basic 包下的 ArgumentsMapTest 和 ArgumentsObjTest 。

    8. autoRuntimeDialect :默认值为 false 。设置为 true 时,允许在运行时根据多数据源自动识别对应方言 的分页 (不支持自动选择 sqlserver2012 ,只能使用 sqlserver ),用法和注意事项参考下面的场景五。

    9. closeConn :默认值为 true 。当使用运行时动态数据源或没有设置 helperDialect 属性自动获取数据库类 型时,会自动获取一个数据库连接, 通过该属性来设置是否关闭获取的这个连接,默认 true 关闭,设置为 false 后,不会关闭获取的连接,这个参数的设置要根据自己选择的数据源来决定。 

    4、使用

    dao层:

    public interface ProductDao {
    
        /**
         * 查询所有商品
         * @return
         */
        @Select("select * from product")
        public List<Product> selectAll();
    
    }

    service层:

    @Service
    public class ProductServiceImpl implements ProductService{
    
        @Autowired
        private ProductDao productDao;
    
        @Override
        public List<Product> findAll(Integer page,Integer size) {
            PageHelper.startPage(page, size);
            return productDao.selectAll();
        }
    
    }

    controller层:

    @Controller
    @RequestMapping("/product")
    public class ProductController {
    
        @Autowired
        private ProductService productService;
    
        @RequestMapping("/findAll")
        public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1") Integer page,
                                    @RequestParam(name = "size",required = true,defaultValue = "5") Integer size){
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("product-list");
            List<Product> list = productService.findAll(page, size);
            PageInfo pageInfo = new PageInfo(list);
            modelAndView.addObject("pageInfo",pageInfo);
            return modelAndView;
        }
    
    }

    这里使用oracle数据,分页查询结果近看日志:

  • 相关阅读:
    leetcode Convert Sorted List to Binary Search Tree
    leetcode Convert Sorted Array to Binary Search Tree
    leetcode Binary Tree Level Order Traversal II
    leetcode Construct Binary Tree from Preorder and Inorder Traversal
    leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
    证明中序遍历O(n)
    leetcode Maximum Depth of Binary Tree
    限制 button 在 3 秒内不可重复点击
    HTML 和 CSS 画三角形和画多边行基本原理及实践
    在线前端 JS 或 HTML 或 CSS 编写 Demo 处 JSbin 与 jsFiddle 比较
  • 原文地址:https://www.cnblogs.com/M87-A/p/14880998.html
Copyright © 2011-2022 走看看