系统用的框架是Spring+ibatis,现在想记录下数据库操作日志即完整的SQL语句;
但是在Dao层怎么才能取回ibatis中的将参数已经组装好了的SQL语句呢,请大虾们指点下啊,最好是完整的代码方法,
谢谢!!
但是在Dao层怎么才能取回ibatis中的将参数已经组装好了的SQL语句呢,请大虾们指点下啊,最好是完整的代码方法,
谢谢!!
解决方案 »
- java 读取大量文件
- 求算法,上一个说的不是很详细。急!各位大侠帮忙!
- jquery.validate.js中传参,为什么request.getParameter(),得到总是上次输入的
- 急急急!HibernateTemplate的update锁数据表问题
- spring2.5 mvc中的主题theme使用的问题
- ejb-modeler-layout.xml的作用是什么?
- 关于Eclipse运行时的问题?
- 如何在ejb里面实现一个返回Collection的finder方法
- Hibernate連接數據源成功之後, 發生錯誤??
- 在j2ee环境下,如何处理多用户并发活动,是不是用事务来处理,请那位大侠详细说明一下??
- struts2 package 问题
- 求用JSP编写把图片存到数据库,再把图片从数据库读取的程序。
2、用p6spy,会很慢
3、直接观察CommonsLogging的输出,有预编译语句及传人的参数已足够
ibatis中是有方法做到呢
import java.util.Map;import javax.sql.DataSource;import com.ibatis.common.jdbc.DbcpConfiguration;
import com.ibatis.sqlmap.engine.datasource.DataSourceFactory;public class MyDbcpDataSourceFactory implements DataSourceFactory {
private DataSource dataSource; @SuppressWarnings("unchecked")
public void initialize(Map map) {
DbcpConfiguration dbcp = new DbcpConfiguration(map);
dataSource = new MyDataSource(dbcp.getDataSource());
} public DataSource getDataSource() {
return dataSource;
}
}再提供一个MyDataSource:
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.sunyard.sunflow.db.wrapper.ConnectionWrapper;
public class MyDataSource implements DataSource {
private DataSource dataSource = null; public MyDataSource(DataSource dataSource) {
if (dataSource == null) {
throw new RuntimeException("数据库连接池为null");
}
this.dataSource = dataSource;
} public Connection getConnection() throws SQLException {
Connection conn = dataSource.getConnection();
conn = new ConnectionWrapper(conn);
return conn;
}
//TODO other method here
}再就是那个ConnectionWrapper,这里是真正做事的地方:import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.Map;public class ConnectionWrapper implements Connection {
protected Connection conn; public ConnectionWrapper(Connection conn) {
this.conn = conn;
} public void clearWarnings() throws SQLException {
conn.clearWarnings();
} public void close() throws SQLException {
conn.close();
} public void commit() throws SQLException {
conn.commit();
} public Statement createStatement() throws SQLException {
return new StatementWrapper(conn.createStatement());
} public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return new StatementWrapper(conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability));
} public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
return new StatementWrapper(conn.createStatement(resultSetType, resultSetConcurrency));
} public boolean getAutoCommit() throws SQLException {
return conn.getAutoCommit();
} public String getCatalog() throws SQLException {
return conn.getCatalog();
} public int getHoldability() throws SQLException {
return conn.getHoldability();
} public DatabaseMetaData getMetaData() throws SQLException {
return conn.getMetaData();
} public int getTransactionIsolation() throws SQLException {
return conn.getTransactionIsolation();
} public Map<String, Class<?>> getTypeMap() throws SQLException {
return conn.getTypeMap();
} public SQLWarning getWarnings() throws SQLException {
return conn.getWarnings();
} public boolean isClosed() throws SQLException {
return conn.isClosed();
} public boolean isReadOnly() throws SQLException {
return conn.isReadOnly();
} public String nativeSQL(String sql) throws SQLException {
//这里操作sql
return conn.nativeSQL(sql);
} public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
//这里操作sql
return conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
} public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
//这里操作sql
return conn.prepareCall(sql, resultSetType, resultSetConcurrency);
} public CallableStatement prepareCall(String sql) throws SQLException {
//这里操作sql
return conn.prepareCall(sql);
} public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
//这里操作sql
return conn.prepareStatement(sql,resultSetType,resultSetConcurrency,resultSetHoldability);
} public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
//这里操作sql
return conn.prepareStatement(sql,resultSetType,resultSetConcurrency);
} public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
//这里操作sql
return conn.prepareStatement(sql,autoGeneratedKeys);
} public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
//这里操作sql
return conn.prepareStatement(sql,columnIndexes);
} public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
//这里操作sql
return conn.prepareStatement(sql,columnNames);
} public PreparedStatement prepareStatement(String sql) throws SQLException {
//这里操作sql
return conn.prepareStatement(sql);
}
//TODO OTHER METHOD HERE
}因为Statement也可能直接execute sql,所以对Statement也包装一下成为StatementWrapper,方式类似,这里就能获取到所有的sql