您的位置:首页 > 其它

Mybatis物理分页插件

2017-05-10 15:14 330 查看
使用方式: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis