PageHelper的使用(完全舍弃mybatis配置文件)

PageHelper的使用(完全舍弃mybatis配置文件)

  1. 导入坐标

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>x.x.x</version><!-- 这里我用的版本是5.1.2 -->
    </dependency>
    
  2. 配置

    1. 方式一:完全整合到Spring配置文件中

      分析org.mybatis.spring.SqlSessionFactoryBean类,发现其内plugins的类型为一个拦截器数组

      private Interceptor[] plugins;
      

      这个org.apache.ibatis.plugin.Interceptor接口的实现类有两个

      • com.github.pagehelper.PageInterceptor(分页拦截器)
      • com.github.pagehelper.QueryInterceptor(查询拦截器,里面没有实现属性设置方法)

      故先要往spring容器中配置一个拦截器的实现类PageInterceptor

      applicationContext.xml

      <!-- 配置pageHelp的拦截器 -->
      <bean id="plugin" class="com.github.pagehelper.PageInterceptor">
          <property name="properties">
              <value>helperDialect=mysql</value>
          </property>
      </bean>
      

      然后再sqlSessionFactory中注入属性

      <!-- 配置SqlSessionFactory -->
      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <!-- 注入数据库连接 -->
          <property name="dataSource" ref="dataSource"></property>
          <!-- 设置映射文件地址 -->
          <property name="mapperLocations" value="classpath:com/zjweu/mapper/*.xml"></property>
          <!-- 设置实体类别名 -->
          <property name="typeAliasesPackage" value="com.zjweu.entity"></property>
          <!-- 配置蛇形转驼峰 -->
          <property name="configuration" ref="settings"></property>
      
          <!-- 配置分页插件 -->
          <property name="plugins">
              <list>
                  <ref bean="plugin"></ref>
              </list>
          </property>
      </bean>
      

      在需要分页的地方使用,这里我在controller层使用分页

      @RequestMapping("/tolist")
      public ModelAndView tolist(@RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue="10")
              Integer
              pageSize){
          PageHelper.startPage(page,pageSize);//开始分页
          List<News> list = newsService.findAll();
          PageInfo<News> pageInfo = new PageInfo<News>(list);//封装分页数据
          ModelAndView mav = new ModelAndView();
          mav.setViewName("newsList");
          mav.addObject("pageInfo", pageInfo);
          return mav;
      }
      

      页面参考代码

      <p>当前${pageInfo.pageNum}页,共${pageInfo.pages}页,总共${pageInfo.total}条记录</p>
      <a href="tolist?page=${pageInfo.firstPage}">第一页</a>
      <c:if test="${pageInfo.hasPreviousPage}">
          <a href="tolist.do?page=${pageInfo.pageNum-1}">上一页</a>
      </c:if>
      <c:if test="${pageInfo.hasNextPage}">
          <a href="tolist?page=${pageInfo.pageNum+1}">下一页</a>
      </c:if>
      <a href="tolist?page=${pageInfo.lastPage}">最后一页</a>
      
    2. 方式二:在将配置放在mybatis的配置文件中

      mybatis-config.xml

      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
      <configuration>
          <!-- 配置分页插件 -->
          <plugins>
              <plugin interceptor="com.github.pagehelper.PageInterceptor">
                  <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
                  <property name="helperDialect" value="mysql"/>
              </plugin>
          </plugins>
      </configuration>
      

      然后在spring配置文件中加载mybatis的配置

      applicationContext.xml

      <!-- 配置SqlSessionFactory -->
      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <!-- 注入数据库连接 -->
          <property name="dataSource" ref="dataSource"></property>
          <!-- 设置映射文件地址 -->
          <property name="mapperLocations" value="classpath:com/zjweu/mapper/*.xml"></property>
          <!-- 加载mybatis配置文件 -->
          <property name="configLocation" value="classpath:mybatis-config.xml"></property>
          <!-- 设置实体类别名 -->
          <property name="typeAliasesPackage" value="com.zjweu.entity"></property>
      </bean>
      

      使用和第一种方式一样

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值