public
inte***ce
interceptor
@intercepts()
這裡我要攔截的是executor的query方法,先判斷有沒有pageparam型別的分頁引數,如果有的話先查詢符合條件的資料條數count,再獲取具體的資料list,將count和list封裝在page型別的物件裡面返回。
@intercepts()
public
class
easypage
implements
interceptor else
if (parameter instanceof pageparam || parameter instanceof hashmap) }}
// 判斷是否需要分頁,當引數不是預設值的時候就進行分頁
if (page != null && page.getindex() != 0 && page.getrows() != integer.max_value)
return
new page(list, index, rows, total);
}return invocation.proceed();
}
/**
* 獲取資料總條數
*@param parameter
*@param boundsql
*@return
*@throws sqlexception
*/public
int stringbuilder sqlbuilder = new stringbuilder();
connection connection;
preparedstatement countstmt = null;
resultset rs = null;
int count = 0;
try
logger.debug("==> preparing: {}", sqlbuilder.tostring());
logger.debug("<== total: {}", count);
} finally
} finally }}
return count;
}
上面只是幾個比較重要的方法,完整的**在github: 。**完成後,就可以獲取到分頁後的結果了。/**
* 根據資料庫型別設定引數,不需要在配置中設定資料庫型別,通過databasemetadata物件可以取到資料庫名稱
*@param ms
*@param boundsql
*@param offset
*@param pagekey
*@return
*@throws sqlexception
*/databasemetadata dbmd = ms.getconfiguration().getenvironment().getdatasource().getconnection().getmetadata();
string dbtype = dbmd.getdatabaseproductname();
string sql = boundsql.getsql();
if (dbtype != null)
}if (offset > 0) else
boundsql newboundsql = new boundsql(ms.getconfiguration(), sql, list, boundsql.getparameterobject());
return newboundsql;
}
mybatis分頁外掛程式
其實吧,這個分頁的封裝是我從mybatis實戰上抄的,然後又重構了下 形成了自己的。現在之所以會記錄一下,主要原因是出現了質變 對foreach的支援,而解決這個問題的過程中,我感覺,應該基本上使用上沒有多少侷限行了。下面說說實際的吧。基本的設計思路,是使用mybatis外掛程式,首先是下面這一串註...
mybatis 分頁外掛程式
pagehelper 是國內非常優秀的一款開源的mybatis分頁外掛程式 支援任何複雜的單錶 多表分頁。它支援基本主流與常用的資料庫,例如mysql oracle db2 sqlite hsqldb等。本文主要使用的是mysql和pagehelper方法的呼叫 使用pagehelper之後我們就可...
Mybatis分頁外掛程式 PageHelper
如果你也在用mybatis,建議嘗試該分頁外掛程式,這個一定是最方便使用的分頁外掛程式。該外掛程式目前支援oracle,mysql,mariadb,sqlite,hsqldb,postgresql六種資料庫分頁。第一步 在mybatis配置 xml中配置 外掛程式 1 2 3 4 5 6 7 第二步...