參考來自:html
http://www.360doc.com/content/15/0728/15/12642656_487954693.shtmlgit
http://www.javashuo.com/article/p-uljowssh-bh.htmlgithub
http://www.hifreud.com/2015/03/06/mybatis-7-Pagination/spring
http://www.cnblogs.com/jcli/archive/2011/08/09/2132222.htmlsql
1 邏輯分頁 : 邏輯分頁指的是將數據庫中全部數據所有取出,而後經過Java代碼控制分頁邏輯。 2 物理分頁 : 物理分頁指的是在SQL查詢過程當中實現分頁,依託與不一樣的數據庫廠商,實現也會不一樣。
如今使用的是邏輯分頁,由於出現了性能問題,考慮將其變爲物理分頁。ps:項目中使用的ibatis的方式,具體的舊代碼後面會有 。數據庫
使用PageHelper也不會影響的部分,但爲了說明仍是列出來和mybatis相關的文件內容:json
1 <!-- MyBatis配置 --> 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 <!-- 自動掃描entity目錄, 省掉Configuration.xml裏的手工配置 --> 6 <property name="typeAliasesPackage" value="classpath:com/baosight/**/entity"/> 7 <!-- 顯式指定Mapper文件位置 --> 8 <property name="mapperLocations" value="classpath:sql/**/*.xml"/> 9 </bean>
來自BaseService.query()方法。主要邏輯是,當curPage=null或curRowNum=null時,不進行分頁。不然進行分頁處理。mybatis
重點代碼以下:app
1 Integer curPage = (Integer) paramInfo.get("curPage"); 2 Integer curRowNum = (Integer) paramInfo.get("curRowNum"); 3 4 List<JSONObject> resultArr = new ArrayList<JSONObject>(); 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 }
其餘參數相關的代碼以下:post
即這裏的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.pagehelper</groupId> 3 <artifactId>pagehelper</artifactId> 4 <version>4.1.0</version> 5 </dependency>
<plugins>標籤內的爲新增部分,即註冊PageHelper。
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 <!-- com.github.pagehelper爲PageHelper類所在包名 --> 21 <plugin interceptor="com.github.pagehelper.PageHelper"> 22 <property name="dialect" value="postgresql"/> 27 <!--設置爲true時,若是pagesize=0或者rowbounds.limit=0就會查詢出全部的結果--> 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 PageInfo<JSONObject> pageResult; 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 List<JSONObject> list = 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: