系统用的框架是Spring+ibatis,现在想记录下数据库操作日志即完整的SQL语句;
但是在Dao层怎么才能取回ibatis中的将参数已经组装好了的SQL语句呢,请大虾们指点下啊,最好是完整的代码方法,
谢谢!!

解决方案 »

  1.   

    1、在数据库侧打开监控模式
    2、用p6spy,会很慢
    3、直接观察CommonsLogging的输出,有预编译语句及传人的参数已足够
      

  2.   

    你好,用日志方式输出我只知道他输出在日志文件里,但是我是想取出SQL语句要记录下来的;
    ibatis中是有方法做到呢
      

  3.   

    运用装饰者模式即可先封装一下DataSource,如:
    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
      

  4.   

    有的话直接回答你,不用哪个这么麻烦什么数据库侧什么p6spy