您的位置:首页 > 其它

2017.12.14 Mybatis物理分页插件PageHelper的使用(一)

2018-01-04 16:43 411 查看
参考来自:

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.物理分页和逻辑分页

1 逻辑分页 : 逻辑分页指的是将数据库中所有数据全部取出,然后通过Java代码控制分页逻辑。
2 物理分页 : 物理分页指的是在SQL查询过程中实现分页,依托与不同的数据库厂商,实现也会不同。


 

2.需求

现在使用的是逻辑分页,因为出现了性能问题,考虑将其变为物理分页。ps:项目中使用的ibatis的方式,具体的旧代码后面会有 。

 

3.实际使用

3.0 Spring中mybatis的配置

使用PageHelper也不会影响的部分,但为了说明还是列出来和mybatis相关的文件内容:

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>


 

3.1 旧代码

来自BaseService.query()方法。主要逻辑是,当curPage=null或curRowNum=null时,不进行分页。否则进行分页处理。

重点代码如下:

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         }


 

其他参数相关的代码如下:

即这里的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>


 

3.2 新代码

(1)pom.xml

1        <dependency>
2             <groupId>com.github.pagehelper</groupId>
3             <artifactId>pagehelper</artifactId>
4             <version>4.1.0</version>
5         </dependency>


 

(2)mybatis的配置文件SqlMapConfig.xml
<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>


 

(3)代码变动

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         }


 

4.效果比对

使用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:


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: