• Mybatis分页插件PageHelper使用


    该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页

    第一步:把PageHelper依赖的jar包添加到工程中。官方提供的代码对逆向工程支持的不好,使用网上的pagehelper-fix

    第二步:在Mybatis配置xml中配置拦截器插件:

    <plugins>
        <!-- com.github.pagehelper为PageHelper类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->       
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>

    第三步:分页测试

    @Test
         public void testPageHelper() throws Exception {
              //初始化spring容器
              ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
              //获得Mapper的代理对象
              TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
              //设置分页信息
              PageHelper.startPage(1, 30);
              //执行查询
              TbItemExample example = new TbItemExample();
              List<TbItem> list = itemMapper.selectByExample(example);
              //取分页信息
              PageInfo<TbItem> pageInfo = new PageInfo<>(list);
              System.out.println(pageInfo.getTotal());
              System.out.println(pageInfo.getPages());
              System.out.println(pageInfo.getPageNum());
              System.out.println(pageInfo.getPageSize());
         }
  • 相关阅读:
    置换加密算法
    堆和优先队列的应用
    定时发送邮件小程序
    Hibernate的缓存
    Spring中使用JDBC
    Spring AOP(创建切面)
    处理不可中断阻塞
    SQL语句实例说明
    spring_声明式事务
    Flex_includeIn属性的作用
  • 原文地址:https://www.cnblogs.com/naixin007/p/10062904.html
Copyright © 2020-2023  润新知