mybatis分页插件

maven:

 <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>

mybatis配置文件:

<plugins>  
    <!-- com.github.pagehelper为PageHelper类所在包名 -->  
    <plugin interceptor="com.github.pagehelper.PageHelper">  
        <!-- 方言 -->  
        <property name="dialect" value="mysql"/>  
        <!-- 该参数默认为false -->  
        <!-- 设置为true时,使用RowBounds分页会进行count查询 -->  
        <property name="rowBoundsWithCount" value="true"/>  
    </plugin>  
</plugins>  

spring与mybatis整合

spring配置文件:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- <property name="configLocation" value="classpath:mybatis-config.xml" /> -->
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:com/yupont/gs/dao/mapper/*.xml" ></property>
        <property name="typeAliasesPackage" value="com.yupont.gs.model"/>
        <property name="plugins">
            <array>
                  <!-- 分页插件 -->
                  <bean class="com.github.pagehelper.PageInterceptor">
                      <property name="properties">
                          <value>
                            <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
                            rowBoundsWithCount=true
                          </value>
                    </property>
                  </bean>
                  <!-- 性能拦截器,用于输出每条 SQL 语句及其执行时间,会影响一定性能,正式环境要关闭 -->
                  <bean class="com.yupont.core.mvc.mybatis.PerformanceInterceptor">
                  </bean>
            </array>
          </property>
    </bean>

使用方法:

@Test
    public void selectAll(){
        PageHelper.startPage(2, 5); //第一个参数offset是当前页数,第二个参数limit是每页显示多少数据,分页会在紧跟的selete查询后执行,通过PageInfo类,还可以获得更多信息
        //Condition c = new Condition(Test1.class);
        //c.createCriteria().andCondition("name in ('zhaoliu','zhangsan')");
        List<Test1> list = mapper.selectAll();
        PageInfo<Test1> page = new PageInfo<Test1>(list);
        System.out.println(page.getTotal());
        for (Test1 test1 : list) {
            System.out.println(test1.getName());
        }
    }

 

  

  
posted @ 2018-04-19 08:29  王森123  阅读(140)  评论(0编辑  收藏  举报