MyBatis+Spring分页插件配置

1.pom.xml依赖

<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatis-springartifactId>
    <version>1.3.0version>
dependency>
<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>3.4.1version>
dependency>
<dependency>
    <groupId>com.github.miemiedevgroupId>
    <artifactId>mybatis-paginatorartifactId>
    <version>1.2.15version>
dependency>

2.TestUser.mapper.xml 映射文件

id="BaseResult" type="com.ecs.mybatis.TestUser">
  <id column="id" property="id" />
  <result column="name" property="name" />
  <result column="address" property="address" />

3.datasource.xml spring配置

id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="jdbcDataSource" />
    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
     <property name="plugins">
        <list>
            class="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor" >
                <property name="dialectClass" value="com.github.miemiedev.mybatis.paginator.dialect.OracleDialect" />
            
        list>
    property>

4.测试

@Test
public void testquery(){

    int page = 1;     //页号, 从1开始
    int pageSize = 3; //每页有几条数据
    String sortString = "id.asc"; //如果你想排序的话逗号分隔可以排序多列 
    PageBounds pageBounds = new PageBounds(page, pageSize , Order.formString(sortString));

    TestUser user = new TestUser();
    user.setName("张三");
    List list = userDao.query("张三",pageBounds);
    PageList pageList = (PageList)list;
    Paginator p =  pageList.getPaginator();

    //System.err.println("totalCount:"+p.getTotalCount());
    //System.err.println("totalPage:"+p.getTotalPages());
    System.out.println(list);

    for(TestUser u:pageList){
        System.out.println();
        System.out.print(u.getId()+"\t");
    }
}

你可能感兴趣的:(Spring,MyBatis)