Mybatis物理分页插件(目前mybatis下最好的物理分页)

关键字:Mybatis物理分页插件(目前mybatis下最好的物理分页)

使用方式:http://my.oschina.net/miemiedev/blog/135516
项目git地址:https://github.com/miemiedev/mybatis-paginator

miemiedev还提供了一系列针对mybatis的插件,项目首页:https://github.com/miemiedev

简单示例:

在Maven中加入依赖:


<dependencies>
  ...
    <dependency>
        <groupId>com.github.miemiedev</groupId>
        <artifactId>mybatis-paginator</artifactId>
        <version>1.2.15</version>
    </dependency>
...
</dependencies>


Mybatis配置文件添加分页插件:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
    <plugins>
        <plugin interceptor="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor">
            <property name="dialectClass" value="com.github.miemiedev.mybatis.paginator.dialect.OracleDialect"/>
        </plugin>
    </plugins>
</configuration>

注:如果使用mybatis-spring来整合mybatis,也可以这样配置(主要是加载分页插件)


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations">
<list>
<value>classpath*:/mybatis/common/*-*.xml</value>
<value>classpath*:/mybatis/${jdbc.dialect}/*-*.xml</value>
</list>
</property>
<property name="plugins">
<list>
<bean class="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor">
<property name="dialectClass" value="com.github.miemiedev.mybatis.paginator.dialect.MySQLDialect"></property>
</bean>

</list>

</property>
</bean>



创建一个查询,内容可以是任何Mybatis表达式,包括foreach和if等:


<select id="findByCity" resultType="map">
    select * from TEST_USER where city = #{city};
</select>


Dao中的方法或许是这样(用接口也是类似):



public List findByCity(String city, PageBounds pageBounds){

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("city",city);

    return getSqlSession().selectList("db.table.user.findByCity", params, pageBounds);
}


调用方式(分页加多列排序):


int page = 1; //页号
int pageSize = 20; //每页数据条数
String sortString = "age.asc,gender.desc";//如果你想排序的话逗号分隔可以排序多列
PageBounds pageBounds = new PageBounds(page, pageSize , Order.formString(sortString));
List list = findByCity("BeiJing",pageBounds);

//获得结果集条总数
PageList pageList = (PageList)list;
System.out.println("totalCount: " + pageList.getPaginator().getTotalCount());

PageList类是继承于ArrayList的,这样Dao中就不用为了专门分页再多写一个方法。

使用PageBounds这个对象来控制结果的输出,常用的使用方式一般都可以通过构造函数来配置。



new PageBounds();//默认构造函数不提供分页,返回ArrayList
new PageBounds(int limit);//取TOPN操作,返回ArrayList
new PageBounds(Order... order);//只排序不分页,返回ArrayList

new PageBounds(int page, int limit);//默认分页,返回PageList
new PageBounds(int page, int limit, Order... order);//分页加排序,返回PageList
new PageBounds(int page, int limit, List<Order> orders, boolean containsTotalCount);//使用containsTotalCount来决定查不查询totalCount,即返回ArrayList还是PageList

猜你喜欢

转载自zhaoshijie.iteye.com/blog/2163038
今日推荐