日期:2014-05-16  浏览次数:20639 次

MyBatis 3.02 实现数据库分页,自动总行数统计及Object[]数据结果

最近研读了MyBatis源代码,并且修改了几处代码,实现了数据库分页以及Object[]数据结果,先放出编译包,源代码稍后放出。

数据库分页不仅实现了数据库分页,而且还可以根据需求,自动返回SQL语句统计总行数。

查询结果可以返回List<Object[]> 结构的结果,兼容习惯数组操作。

map配置:

?

<select id="findAll2" parameterType="list" resultType="object[]">  
    select * from student   
    <where>  
        <if test="array[0]">and name like #{array[0]}</if>  
        <if test="array[1]">and score = #{array[1]}</if>  
    </where>  
</select>  

?

代码:

StudentMaper maper = session.getMapper(StudentMaper.class);   
List<Object[]> list = maper.findAll2(new Object[] { "%心%", null }, new RowBounds(2, 10));

?先贴上一段分页的运行日志(注意红色字为分页和统计关键日志 ):

?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

?

1 楼