最近研读了MyBatis源代码,并且修改了几处代码,实现了数据库分页以及Object[]数据结果,源代码稍后放出,只因文章中不能直接携带图片之外的附件。
数据库分页不仅实现了数据库分页,而且还可以根据需求,自动返回SQL语句统计总行数。
查询结果可以返回List<Object[]> 结构的结果,兼容习惯数组操作。
map配置:
代码:
先贴上一段分页的运行日志(注意红色字为分页和统计关键日志):
INFO 09-20 23:36:40 (719) org.apache.ibatis.executor.statement.RoutingStatementHandler Pagination query prepare! (RoutingStatementHandler.java:81)
INFO 09-20 23:36:40 (906) org.apache.ibatis.executor.statement.RoutingStatementHandler Count PreparedStatement is created! (RoutingStatementHandler.java:100)
DEBUG 09-20 23:36:40 (906) java.sql.PreparedStatement ==> Executing: select * from ( select row_.*, rownum rownum_ from ( select * from student WHERE name like ? ) row_ ) where rownum_ <= 2+10 and rownum_ > 2 (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (906) java.sql.PreparedStatement ==> Parameters: %心%(String) (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1000) java.sql.ResultSet <== Columns: ID, NAME, MAJOR, BIRTH, SCORE, ROWNUM_ (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1015) java.sql.ResultSet <== Row: 97575FC004874789BBF35BF4950482DD, 心目, s, 2010-09-18 14:01:33.0, 12, 3 (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.ResultSet <== Row: D8D2B23C2CE1407CBBAD390791710972, 心目, s, 2010-09-18 14:02:56.0, 12, 4 (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.ResultSet <== Row: 4F9737FDC1F947D984A4B6645116C1A8, 心目, s, 2010-09-18 14:03:20.0, 12, 5 (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.ResultSet <== Row: F0C0384B4E93446DA5E970BED4D2A86F, 心目, s, 2010-09-18 14:04:31.0, 12, 6 (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.ResultSet <== Row: 7E20160DE3E645BCB5BB242AE93564B0, 心目, s, 2010-09-18 14:05:01.0, 12, 7 (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.ResultSet <== Row: 82EBB84B119D4C1EA6C47524381D9087, 心目, s, 2010-09-18 14:06:29.0, 12, 8 (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.ResultSet <== Row: 5A3C622E78EE4096981B82B7649562DD, 心目, s, 2010-09-18 14:07:24.0, 12, 9 (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.ResultSet <== Row: CDA81292ED86466F986F3CB990B8B01A, 心目, s, 2010-09-18 14:35:16.0, 12, 10 (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.ResultSet <== Row: D912787AC77B4D06B991944FA6CE08D5, 心目, s, 2010-09-18 14:35:48.0, 12, 11 (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.ResultSet <== Row: AED12300B24548199745EA7CDB3E604B, 心目, s, 2010-09-18 14:37:31.0, 12, 12 (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.PreparedStatement ==> Executing: select count(*) from student WHERE name like ? (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.PreparedStatement ==> Parameters: %心%(String) (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.ResultSet <== Columns: COUNT(*) (Slf4jImpl.java:28)
DEBUG 09-20 23:36:40 (1031) java.sql.ResultSet <== Row: 115 (Slf4jImpl.java:28)
总行数是:115