参考来自:
http://www.360doc.com/content/15/0728/15/12642656_487954693.shtml
https://www.cnblogs.com/digdeep/p/4608933.html
http://www.hifreud.com/2015/03/06/mybatis-7-Pagination/
http://www.cnblogs.com/jcli/archive/2011/08/09/2132222.html
1 逻辑分页 : 逻辑分页指的是将数据库中所有数据全部取出,然后通过Java代码控制分页逻辑。 2 物理分页 : 物理分页指的是在SQL查询过程中实现分页,依托与不同的数据库厂商,实现也会不同。
现在使用的是逻辑分页,因为出现了性能问题,考虑将其变为物理分页。ps:项目中使用的ibatis的方式,具体的旧代码后面会有 。
使用PageHelper也不会影响的部分,但为了说明还是列出来和mybatis相关的文件内容:
1 2 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 3 <property name="dataSource" ref="dataSource"/> 4 <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/> 5 6 <property name="typeAliasesPackage" value="classpath:com/baosight/**/entity"/> 7 8 <property name="mapperLocations" value="classpath:sql/**/*.xml"/> 9 bean>
来自BaseService.query()方法。主要逻辑是,当curPage=null或curRowNum=null时,不进行分页。否则进行分页处理。
重点代码如下:
1 Integer curPage = (Integer) paramInfo.get("curPage"); 2 Integer curRowNum = (Integer) paramInfo.get("curRowNum"); 3 4 ListresultArr = new ArrayList (); 5 try { 6 if (curPage == null || curRowNum == null) { 7 resultArr = sqlSessionTemplate.selectList(querySql, daoEntity); 8 } else { 9 resultArr = sqlSessionTemplate.selectList(querySql, daoEntity, 10 new RowBounds((curPage - 1) * curRowNum, curRowNum)); 11 } 12 } catch (Exception e) { 13 e.printStackTrace(); 14 paramInfo.put("status", Constants.EXECUTE_FAIL); 15 paramInfo.put("returnMsg", "查询出错,请检查SQL语句!"); 16 return paramInfo; 17 }
其他参数相关的代码如下:
即这里的querySql、countSql对应的是一个命名空间下的某方法。
1 paramInfo.put("querySql", "GlobalMessage.querybatch"); 2 paramInfo.put("countSql", "GlobalMessage.countbatch"); 3 paramInfo.put("DaoEntity", "com.lyh.entity.GlobalMessage");
GlobalMessage.xml的命名空间如下:
1 <mapper namespace="GlobalMessage">
queryBathch如下:显然是不带任何有关limit和offset的sql语句。
1 <select id="querybatch" parameterType="com.lyh.entity.GlobalMessage" 2 resultType="com.alibaba.fastjson.JSONObject"> 3 select * from t_global_message12 where 13 1 = 1 14 <if test="messageId != null"> 15 and MESSAGE_ID = #{messageId} 16 if> 17 <if test="messageKey != null and messageKey != ''" > 18 and MESSAGE_KEY in (${messageKey}) 19 if> 26 <if test="messageLan != null"> 27 and MESSAGE_LAN = #{messageLan} 28 if> 29 <if test="messageEnable != null"> 30 and MESSAGE_ENABLE = #{messageEnable} 31 if> 59 select>
1 <dependency> 2 <groupId>com.github.pagehelpergroupId> 3 <artifactId>pagehelperartifactId> 4 <version>4.1.0version> 5 dependency>
1 xml version="1.0" encoding="UTF-8" ?> 2 DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <properties resource="project.properties" /> 7 <settings> 8 <setting name="logPrefix" value="dao." /> 9 settings> 19 <plugins> 20 21 <plugin interceptor="com.github.pagehelper.PageHelper"> 22 <property name="dialect" value="postgresql"/> 27 28 <property name="pageSizeZero" value="true"/> 29 <property name="reasonable" value="true"/> 30 plugin> 31 plugins> 37 configuration>
1 Integer curPage = (Integer) paramInfo.get("curPage"); 2 Integer curRowNum = (Integer) paramInfo.get("curRowNum"); 3 4 PageInfopageResult; 5 try { 6 if (curPage == null || curRowNum == null) {//不分页,查询出所有 7 curRowNum = 0; 8 curPage = 0; 9 } 10 11 PageHelper.startPage(curPage, curRowNum); 12 13 //PageHelper.startPage(pageIndex,pageSize);//当前页码,每页大小 14 Listlist = sqlSessionTemplate.selectList(querySql, daoEntity, 15 new RowBounds((curPage-1)*curRowNum, curRowNum)); 16 pageResult = new PageInfo<>(list); 17 pageResult.setList(list); 18 } catch (Exception e) { 19 e.printStackTrace(); 20 paramInfo.put("status", Constants.EXECUTE_FAIL); 21 paramInfo.put("returnMsg", "查询出错,请检查SQL语句!"); 22 return paramInfo; 23 }
使用pageHelper之前,查询时的sql语句示例如下:
1 DEBUG dao.GlobalMessage.queryBatch - ==> Preparing: select * from t_global_message where 1 = 1 2 DEBUG dao.GlobalMessage.queryBatch - ==> Parameters:
使用pageHelper之后,输出的sql已经拼接了limit和offset:
1 DEBUG dao.GlobalMessage.queryBatch - ==> Preparing: select * from t_global_message where 1 = 1 limit ? offset ? 2 DEBUG dao.GlobalMessage.queryBatch - ==> Parameters: 10 0
当在程序中传递curPage或者curRowNum为null时,根据代码,curPage和curRowNum被置为了0,此时仍然使用pageHelper,sql语句没有拼接limit和offset,将所有数据都查询出来了:
1 DEBUG dao.GlobalMessage.queryBatch - ==> Preparing: select * from t_global_message where 1 = 1 2 DEBUG dao.GlobalMessage.queryBatch - ==> Parameters: