轉帖|其它|編輯:郝浩|2010-10-28 15:16:36.000|閱讀 2622 次
概述: ibatis 是一個 O/R Mapping 解決方案, ibatis 最大的特點就是小巧,上手很快。如果你不需要太多復雜的功能, ibatis 是能滿足你的要求又足夠靈活的最簡單的解決方案。ibatis 以SQL開發的工作量和數據庫移植性上的讓步,為系統 設計提供了更大的自由空間。本文主要介紹ibatis在debug時打印完整的SQL語句,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
這些年做的項目中,對持久層的選型更多的是基于ibatis,而且自己涂鴉的系統開發框架數據層操作也是居于ibatis實現的,不為別的,就因為ibatis使用的是純jdbc操作。
時下流行凡是都“原始回歸”,“如果能把ibatis進行更好地理解和擴展,也不乏為一個優秀的杜撰框架”,基于這樣的想法,好長時間里,我一直是鐘情于ibatis的源碼學習,自然從中也學到了很多有用的東西,設計模式、算法分析、構建思想和一些優秀的API實體類工具等。
好,先來幾句贊美ibatis的浮夸。
ibatis 是一個 O/R Mapping 解決方案, ibatis 最大的特點就是小巧,上手很快。如果你不需要太多復雜的功能, ibatis 是能滿足你的要求又足夠靈活的最簡單的解決方案。ibatis 以SQL開發的工作量和數據庫移植性上的讓步,為系統 設計提供了更大的自由空間。
ibatis 底層采用純JDBC操作,要求操作人員直接編寫sql進行操作,光從這點看,ibatis本身就大大地提高數據層操作的透明度。
這里我主要解決的就是一個常見的問題,即使用ibatis時需要在控制臺或日志文件中打印其當前執行的完整sql,而不是常見的參數 問號 ? 語句。(這里主要基于ibatis2.0以上版本)。
步驟:
1.網上下一個源碼文件(沒有可找我,我發一份給你,我的QQ群:91377268)
2. 最終API執行流程,這里以insert為例。 具體順序如下圖:
3. 最終根源找到,即修改com.ibatis.sqlmap.engine.execution.SqlExecutor即可。
在代碼66行處有如下方法
public int executeUpdate(StatementScope statementScope, Connection conn, String sql, Object[] parameters) throws SQLException {
;
}
insert最終執行的就是這個方法,恩,就是它了,改吧
4. 在類體中引入日志打印工具
private static final Log log = LogFactory.getLog(SqlExecutor.class);
5. 添加debug下執行sql打印信息
在ps.execute(); 執行前添加如下:
if (log.isDebugEnabled()) {
int count = ps.getParameterMetaData().getParameterCount();
for (int i = 0; i < count; i++) {
sql = sql.replaceFirst("\\?", parameters[i].getClass().getName().equals("java.lang.String") ? "'"+parameters[i].toString()+"'" : parameters[i].toString());
+"'" : parameters[i].toString());
}
log.debug("===當前執行SQL為===" + sql + ".");
}
還是貼一下完整的方法吧:
public int executeUpdate(StatementScope statementScope, Connection conn, String sql, Object[] parameters) throws SQLException {
ErrorContext errorContext = statementScope.getErrorContext();
errorContext.setActivity("executing update");
errorContext.setObjectId(sql);
PreparedStatement ps = null;
setupResultObjectFactory(statementScope);
int rows = 0;
try {
errorContext.setMoreInfo("Check the SQL Statement (preparation failed).");
ps = prepareStatement(statementScope.getSession(), conn, sql);
setStatementTimeout(statementScope.getStatement(), ps);
errorContext.setMoreInfo("Check the parameters (set parameters failed).");
statementScope.getParameterMap().setParameters(statementScope, ps, parameters);
errorContext.setMoreInfo("Check the statement (update failed).");
//打印調試信息 start
if (log.isDebugEnabled()) {
int count = ps.getParameterMetaData().getParameterCount();
for (int i = 0; i < count; i++) {
sql = sql.replaceFirst("\\?", parameters[i].getClass().getName().equals("java.lang.String") ? "'"+parameters[i].toString()
+"'" : parameters[i].toString());
}
log.debug("===當前執行SQL為===" + sql + ".");
}
//打印調試信息 end
ps.execute();
rows = ps.getUpdateCount();
} finally {
closeStatement(statementScope.getSession(), ps);
}
return rows;
}
主要的工作就是這樣,下面開啟commons-logging\log4j的debug模式測試一下吧。其他方式的API方法修改也類似了。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載