没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
13页
一直以来ibatis的分页都是通过滚动ResultSet实现的,应该算是逻辑分页吧。逻辑分页虽然能很干净地独立于特定数据库,但效率在多数情况下不及特定数据库支持的物理分页,而hibernate的分页则是直接组装sql,充分利用了特定数据库的分页机制,效率相对较高。本文讲述的就是如何在不重新编译ibatis源码的前提下,为ibatis引入hibernate式的物理分页机制。
资源推荐
资源详情
资源评论
对 IBatis 分页的改进,使 ibatis 支持 hibernate 式的物理分页
关键字: ibatis spring 分页 物理分页
一直以来 ibatis 的分页都是通过滚动 ResultSet 实现的,应该算是逻辑分页吧。
逻辑分页虽然能很干净地独立于特定数据库,但效率在多数情况下不及特定数据
库支持的物理分页,而 hibernate 的分页则是直接组装 sql,充分利用了特定数
据库的分页机制,效率相对较高。本文讲述的就是如何在不重新编译 ibatis 源
码的前提下,为 ibatis 引入 hibernate 式的物理分页机制。
基本思路就是找到 ibatis 执行 sql 的地方,截获 sql 并重新组装 sql。通过分
析 ibatis 源码知道,最终负责执行 sql 的类是
com.ibatis.sqlmap.engine.execution.SqlExecutor,此类没有实现任何接口,
这多少有点遗憾,因为接口是相对稳定契约,非大的版本更新,接口一般是不会
变的,而类就相对易变一些,所以这里的代码只能保证对当前版本(2.1.7)的
ibatis 有效。下面是 SqlExecutor 执行查询的方法:
Java 代码
1. /**
2. * Long form of the method to execute a query
3. *
4. * @param request - the request scope
5. * @param conn - the database connection
6. * @param sql - the SQL statement to execute
7. * @param parameters - the parameters for the stat
ement
8. * @param skipResults - the number of results to
skip
9. * @param maxResults - the maximum number of resul
ts to return
10. * @param callback - the row handler for the quer
y
11. *
12. * @throws SQLException - if the query fails
13. */
14. public void executeQuery(RequestScope request, Connecti
on conn, String sql, Object[] parameters,
15. int skip
Results, int maxResults, RowHandlerCallback callback)
16. throws SQLException {
17. ErrorContext errorContext = request.getErrorContext()
;
18. errorContext.setActivity("executing query");
19. errorContext.setObjectId(sql);
20.
21. PreparedStatement ps = null;
22. ResultSet rs = null;
23.
24. try {
25. errorContext.setMoreInfo("Check the SQL Statemen
t (preparation failed).");
26.
27. Integer rsType = request.getStatement().getResul
tSetType();
28. if (rsType != null) {
29. ps = conn.prepareStatement(sql, rsType.intVa
lue(), ResultSet.CONCUR_READ_ONLY);
30. } else {
31. ps = conn.prepareStatement(sql);
32. }
33.
34. Integer fetchSize = request.getStatement().getFe
tchSize();
35. if (fetchSize != null) {
36. ps.setFetchSize(fetchSize.intValue());
37. }
38.
39. errorContext.setMoreInfo("Check the parameters (
set parameters failed).");
40. request.getParameterMap().setParameters(request, p
s, parameters);
41.
42. errorContext.setMoreInfo("Check the statement (q
uery failed).");
43.
44. ps.execute();
45. rs = getFirstResultSet(ps);
46.
47. if (rs != null) {
48. errorContext.setMoreInfo("Check the results
(failed to retrieve results).");
49. handleResults(request, rs, skipResults, maxR
esults, callback);
50. }
51.
52. // clear out remaining results
53. while (ps.getMoreResults());
54.
55. } finally {
56. try {
57. closeResultSet(rs);
58. } finally {
59. closeStatement(ps);
60. }
61. }
62.
63. }
其中 handleResults(request, rs, skipResults, maxResults, callback)一句
用于处理分页,其实此时查询已经执行完毕,可以不必关心 handleResults 方法,
但为清楚起见,下面来看看 handleResults 的实现:
Java 代码
1. private void handleResults(RequestScope request, ResultSet
rs, int skipResults, int maxResults, RowHandlerCallback
callback) throws SQLException {
2. try {
3. request.setResultSet(rs);
4. ResultMap resultMap = request.getResultMap();
5. if (resultMap != null) {
6. // Skip Results
7. if (rs.getType() != ResultSet.TYPE_FORWARD_O
NLY) {
8. if (skipResults > 0) {
9. rs.absolute(skipResults);
10. }
11. } else {
12. for (int i = 0; i < skipResults; i+
+) {
13. if (!rs.next()) {
14. break;
15. }
16. }
17. }
18.
19. // Get Results
20. int resultsFetched = 0;
剩余12页未读,继续阅读
资源评论
小小哭包
- 粉丝: 1900
- 资源: 3864
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功