关键字: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
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com