详细demo:参照http://www.kusoft.net
我的数据库是采用mssql2000
采用分页必定数据量比较大:
按照ibatis的自带的是缓存分页.如果一大的肯定内存吃不消. 但是可以采用queryForList,却得不到总记录数码相.
要得到总记录数,因此我采用物理游标进行分页.
功能:需要得到结果的记录数,显示每页几行
(1)定义一个接口Pager:
Java代码
- public interface Pager {
- //总共几行
- public int getRows();
- //存入一页的数据
- public List<Object> getList();
- public void setList(List<Object> list);
- public void setRows(int rows);
- }
(2)Pager实现类PagerImpl
Java代码
- public class PagerImpl implements Pager {
- private List<Object> list = null;
- private int rows = 0;
- public List<Object> getList() {
- return list;
- }
- public void setList(List<Object> list) {
- this.list = list;
- }
- public int getRows() {
- return rows;
- }
- public void setRows(int rows) {
- this.rows = rows;
- }
- }
(3)扩展ibatis自带接口 SqlMapExecutor
Java代码
- //skip 起启行,max每页几行
- Pager queryForPager(String id, Object parameterObject, int skip, int max) throws SQLException;
- Pager queryForPager(String id, Object parameterObject, int skip) throws SQLException;
- //根据条件查出所有结果
- Pager queryForPager(String id, Object parameterObject) throws SQLException;
(4)扩展ibatis接口 SqlMapExecutor的实现类有两个SqlMapClientImpl,SqlMapSessionImpl
Java代码
- SqlMapClientImpl的扩展如下
- public Pager queryForPager(String id, Object paramObject, int skip, int max)
- throws SQLException {
- if (max <= 0)
- return getLocalSqlMapSession().queryForPager(id, paramObject, skip);
- else
- return getLocalSqlMapSession().queryForPager(id, paramObject, skip,
- max);
- }
- public Pager queryForPager(String id, Object parameterObject, int skip)
- throws SQLException {
- return getLocalSqlMapSession().queryForPager(id, parameterObject, skip,
- SqlExecutor.NO_MAXIMUM_RESULTS);
- }
- public Pager queryForPager(String id, Object parameterObject)
- throws SQLException {
- return getLocalSqlMapSession().queryForPager(id, parameterObject,
- SqlExecutor.NO_SKIPPED_RESULTS, SqlExecutor.NO_MAXIMUM_RESULTS);
- }
(5)最终修改SqlExecutor获取总记录数
SqlExecutor:439行
Java代码
- if (resultMap != null) {
- // Skip Results
- if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {
- rs.beforeFirst();
- if (skipResults > 0) {
- rs.absolute(skipResults);
- }
- } else {
- for (int i = 0; i < skipResults; i++) {
- if (!rs.next()) {
- return;
- }
- }
- }
- // Get Results
- int resultsFetched = 0;
- while ((maxResults == SqlExecutor.NO_MAXIMUM_RESULTS || resultsFetched < maxResults)
- && rs.next()) {
- Object[] columnValues = resultMap
- .resolveSubMap(request, rs).getResults(request, rs);
- callback.handleResultObject(request, columnValues, rs);
- resultsFetched++;
- }
- if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {
- rs.last();
- request.setRows(rs.getRow());
- } else {
- int total = skipResults + resultsFetched;
- while(rs.next()){
- total++;
- }
- request.setRows(total);
- }
- }
这样就可以了
(6)如何调用
6.1创建一个方法
Java代码
- public static Pager pager(GroupVO vo,int offset,int limit)throws SQLException{
- return sqlMapper.queryForPager("list",vo,offset);
- }
Java代码
- public static void main(String[] args){
- try {
- SimpleExample se = new SimpleExample();
- GroupVO vo = new GroupVO();
- Pager pager = se.pager(vo,0,10);
- //vo.setGroup_no("100");
- //vo.setGroup_name("fengjian");
- //vo.setGroup_desc("thanks");
- System.out.println("totalsize = "+pager.getRows());
- List list = pager.getList();
- if(list!=null)
- for(int i=0; i<list.size(); i++){
- GroupVO v = (GroupVO)list.get(i);
- System.out.println("group no = " + v.getGroup_no());
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }