Mybatis分页和Spring的集成
写了一个Mybatis分页控件,在这记录一下使用方式。
在Maven中加入依赖:
1
2
3
4
5
6
7
8
9
|
< dependencies >
...
< dependency >
< groupId >com.github.miemiedev</ groupId >
< artifactId >mybatis-paginator</ artifactId >
< version >1.2.17</ version >
</ dependency >
...
</ dependencies >
|
Mybatis配置文件添加分页插件:
1
2
3
4
5
6
7
8
9
10
11
|
<? 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表达式,包括foreach和if等:
1
2
3
|
< select id = "findByCity" resultType = "map" >
select * from TEST_USER where city = #{city};
</ select >
|
Dao中的方法或许是这样(用接口也是类似):
1
2
3
4
5
6
7
|
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);
}
|
调用方式(分页加多列排序):
1
2
3
4
5
6
7
8
9
|
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这个对象来控制结果的输出,常用的使用方式一般都可以通过构造函数来配置。
1
2
3
4
5
6
7
|
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
|
=========================================
如果用的是Spring MVC的话可以把JSON的配置写成这样:
1
2
3
4
5
6
7
8
9
10
11
12
|
< mvc:annotation-driven >
< mvc:message-converters register-defaults = "true" >
< bean class = "org.springframework.http.converter.StringHttpMessageConverter" >
< constructor-arg value = "UTF-8" />
</ bean >
< bean class = "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >
< property name = "objectMapper" >
< bean class = "com.github.miemiedev.mybatis.paginator.jackson2.PageListJsonMapper" />
</ property >
</ bean >
</ mvc:message-converters >
</ mvc:annotation-driven >
|
那么在Controller就可以这样用了:
1
2
3
4
5
6
7
8
9
|
@ResponseBody
@RequestMapping (value = "/findByCity.json" )
public List findByCity( @RequestParam String city,
@RequestParam (required = false ,defaultValue = "1" ) int page,
@RequestParam (required = false ,defaultValue = "30" ) int limit,
@RequestParam (required = false ) String sort,
@RequestParam (required = false ) String dir) {
return userService.findByCity(city, new PageBounds(page, limit, Order.create(sort,dir)));
}
|
然后序列化后的JSON字符串就会变成这样的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
{
"items" :[
{ "NAME" : "xiaoma" , "AGE" :30, "GENDER" :1, "ID" :3, "CITY" : "BeiJing" },
{ "NAME" : "xiaoli" , "AGE" :30, "SCORE" :85, "GENDER" :1, "ID" :1, "CITY" : "BeiJing" },
{ "NAME" : "xiaowang" , "AGE" :30, "SCORE" :92, "GENDER" :0, "ID" :2, "CITY" : "BeiJing" },
{ "NAME" : "xiaoshao" , "AGE" :30, "SCORE" :99, "GENDER" :0, "ID" :4, "CITY" : "BeiJing" }
],
"slider" : [1, 2, 3, 4, 5, 6, 7],
"hasPrePage" : false ,
"startRow" : 1,
"offset" : 0,
"lastPage" : false ,
"prePage" : 1,
"hasNextPage" : true ,
"nextPage" : 2,
"endRow" : 30,
"totalCount" : 40351,
"firstPage" : true ,
"totalPages" : 1346,
"limit" : 30,
"page" : 1
}
|
=========================================
在SpringMVC中使用JSTL的话可以参考一下步骤(懒人用法)
在Spring配置文件中加入拦截器,或则参考拦截器实现定义自己的拦截器
1
2
3
4
5
6
|
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path= "/**" />
<bean class = "com.github.miemiedev.mybatis.paginator.springmvc.PageListAttrHandlerInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
|
然后Controller方法可以这样写
1
2
3
4
5
6
7
8
9
|
@RequestMapping (value = "/userView.action" )
public ModelAndView userView( @RequestParam String city,
@RequestParam (required = false ,defaultValue = "1" ) int page,
@RequestParam (required = false ,defaultValue = "30" ) int limit,
@RequestParam (required = false ) String sort,
@RequestParam (required = false ) String dir) {
List users = userService.findByCity(city, new PageBounds(page, limit, Order.create(sort,dir)));
return new ModelAndView( "account/user" , "users" , users);
}
|
JSP中就可以这样用了,拦截器会将PageList分拆添加Paginator属性,默认命名规则为"原属性名称"+"Paginator"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<table>
<c:forEach items= "${users}" var= "user" >
<tr>
<td>${user[ 'ID' ]}</td>
<td>${user[ 'NAME' ]}</td>
<td>${user[ 'AGE' ]}</td>
</tr>
</c:forEach>
</table>
上一页: ${usersPaginator.prePage}
当前页: ${usersPaginator.page}
下一页: ${usersPaginator.nextPage}
总页数: ${usersPaginator.totalPages}
总条数: ${usersPaginator.totalCount}
更多属性参考Paginator类提供的方法
|
=========================================
如果用如下方法设置pageBounds,当前这个查询就可以用两个线程同时查询list和totalCount了
1
|
pageBounds.setAsyncTotalCount(true);
|
如果所有的分页查询都是用异步的方式查询list和totalCount,可以在插件配置加入asyncTotalCount属性
1
2
3
4
|
< plugin interceptor = "com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor" >
< property name = "dialectClass" value = "com.github.miemiedev.mybatis.paginator.dialect.OracleDialect" />
< property name = "asyncTotalCount" value = "true" />
</ plugin >
|
但是你仍然可以用下面代码强制让这个查询不用异步
1
|
pageBounds.setAsyncTotalCount(false);
|
当然需要注意的是,只要你用到了异步查询,由于里面使用了线程池,所以在使用时就要加入清理监听器,以便在停止服务时关闭线程池。需要在web.xml中加入
1
2
3
|
< listener >
< listener-class >com.github.miemiedev.mybatis.paginator.CleanupMybatisPaginatorListener</ listener-class >
</ listener >
|
完。
转载于:https://www.cnblogs.com/telwanggs/p/5455564.html
Mybatis分页和Spring的集成相关推荐
- 框架源码专题:Spring是如何集成Mybatis的?Spring怎么管理Mapper接口的动态代理
文章目录 1. Spring集成Mybatis代码示例 2. Spring 如何解析Mybatis配置文件 3. Spring是怎么管理Mapper接口的动态代理的 4. Spring整合Mybati ...
- Spring Boot系列六 Spring boot集成mybatis、分页插件pagehelper
1. 概述 本文的内容包括如下内容: Spring Boot集成mybatis Spring Boot集成pagehelper分页插件,定义分页的相关类 实现工具类:model转dto,实现数据层和传 ...
- Spring Boot 集成MyBatis
Spring Boot 集成MyBatis Spring Boot 系列 Spring Boot 入门 Spring Boot 属性配置和使用 Spring Boot 集成MyBatis Spring ...
- Spring Boot 集成 Mybatis 实现双数据源
转载自 Spring Boot 集成 Mybatis 实现双数据源 这里用到了Spring Boot + Mybatis + DynamicDataSource配置动态双数据源,可以动态切换数据源 ...
- spring boot 集成Mybatis时 Invalid bound statement (not found)
spring boot 集成Mybatis时,运行提示 org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...
- Spring 5 + Spring MVC 5 + MyBatis 3 的 Maven 项目集成
相关链接: MyEclipse CI 2018.9.0 配置 Apache Maven 3.5.4 在MyEclipse CI 2018.9.0 中使用 Maven 3.5.4 创建Maven项目 在 ...
- 最详细的Spring+SpringMVC+Mybatis框架整合及mybatis分页讲解,适合初级者
最详细的关于idea整合ssm框架讲解 一个关于brand(品牌)的项目 [ssm框架搭建源代码及mysql数据库数据]链接:https://pan.baidu.com/s/1eBogklK0rFLj ...
- Spring Boot:实现MyBatis分页
综合概述 想必大家都有过这样的体验,在使用Mybatis时,最头痛的就是写分页了,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真的不想花双倍 ...
- Spring Boot集成pagehelper分页插件
Spring Boot集成pagehelper分页插件 1.在pom.xml中添加pagehelper依赖 2.修改配置文件 3.修改Controller里面的查询方法 4.返回分页信息 1. 修改U ...
最新文章
- 吴恩达的Landing.ai又迎来一位AI大牛
- python使用fpdf2包和pdfrw报包新内容添加到已有的PDF页面上
- Spring事务管理--(一)数据库事务隔离级别与mysql引擎基础讲解
- go split 正则_WEGO使用—华大基因在线GO功能注释
- linux中python解释器的配置_CentOS7配置Python3开发环境
- 海量大数据大屏分析展示一步到位:DataWorks数据服务对接DataV最佳实践 1
- Elasticsearch5.X Centos7安装过程
- WebService学习总结(四)——调用第三方提供的webService服务
- auto.js下载安装教程
- 宠物诊所java项目_java毕业设计_springboot框架的宠物医院医疗
- 《FLUENT 14流场分析自学手册》——1.4 流体运动及换热的多维方程组
- 数字电路设计之数字电路工程师面试集锦
- mt4怎么用云服务器跟单,免费好用的跟单系统 神速MT4跟单ea系统使用教程
- 全志F1c100s主线linux入坑记录 (4)GT911触摸移植
- 1013: 求两点间距离(C语言)
- 华为账号登录总显示服务器繁忙,愚人节玩笑:华为手机帐号无法登陆,提示网络繁忙...
- 回溯法高效搜索解空间树的两种办法
- ftp 227 entering passive mode
- pythoneducoder苹果梨子煮水的功效_【苹果梨子煮水喝的功效】_苹果好处_作用-大众养生网...
- vmware VM虚拟机去虚拟化教程 硬件虚拟机 过鲁大师检测
热门文章
- tomcat配置请求指定html文件路径,Tomcat8限制指定域名或者IP访问(示例代码)
- 周立功的linux开发板例程,USB NXP LPC1766 开发配套例程(周立功开发板自带例程) - 下载 - 搜珍网...
- (80)FPGA面试题-请画出序列“1101 “检测状态转移图
- (36)Verilog HDL关系运算:大于、小于、等于
- linux运维服务常见故障,linux常见故障处理
- MySQL 自定义函数设置执行时间_mysql自定义函数计算时间段内的工作日(支持跨年)...
- 8. GD32F103C8T6 定时器-输入捕获测频率
- 202.linux系统相关函数
- 12009.IMU惯导传感器
- java锁包读写锁_Java并发包7--读写锁ReentrantReadWriteLock的实现原理解析