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.17</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>

springmvc+mybatis的时候需要在数据源里面加载插件

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mapperLocations" value="classpath:com/zsai/foundation/mapping/*.xml"></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> 
     </property>
</bean>

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

<select id="findList" resultMap="ResultMap">
    select
    <include refid="Base_Column_List" />
    from user
    <where>
        <if test="id != null">
            and id = #{id,jdbcType=BIGINT}
        </if>
        <if test="addTime != null">
            and addTime = #{addTime,jdbcType=TIMESTAMP}
        </if>
        <if test="QQ != null">
            and QQ = #{QQ,jdbcType=VARCHAR}
        </if>
        <if test="address != null">
            and address = #{address,jdbcType=VARCHAR}
        </if>
        <if test="email != null">
            and email = #{email,jdbcType=VARCHAR}
        </if>
        <if test="mobile != null">
            and mobile = #{mobile,jdbcType=VARCHAR}
        </if>
        <if test="password != null">
            and password = #{password,jdbcType=VARCHAR}
        </if>
        <if test="sex != null">
            and sex = #{sex,jdbcType=INTEGER}
        </if>
        <if test="userName != null">
            and userName = #{userName,jdbcType=VARCHAR}
        </if>
    </where>
</select>

mapper中添加接口:

List<User> findList(Map<String, Object> params, PageBounds pageBounds);

Service中添加实现 :

public List<User> findList(Map<String, Object> params, PageBounds pageBounds) {
    List<User> list = new ArrayList<User>();
    try {
        list = this.UserMapper.findList(params, pageBounds);
    } catch (Exception e) {
        StackTraceElement[] traces = e.getStackTrace();  
        System.err.println("报错啦! => " + traces[0]);
    }
    return list;
}

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

int page = 1; //页号
int pageSize = 20; //每页数据条数
String sortString = "addTime.asc,id.desc";//如果你想排序的话逗号分隔可以排序多列
PageBounds pageBounds = new PageBounds(page, pageSize , Order.formString(sortString));
Map<String, Object> params = new HashMap<String, Object>();
params.put("sex", Integer.valueOf(1));
List list = findList(params,pageBounds); 

//获得分页结果

PageList pageList = (PageList)list;
System.out.println(JSON.toJSONString(pageList));
System.out.println(JSON.toJSONString(pageList.getPaginator()));

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值