2010-04-14 32 views
18

Có cách nào để tôi có thể truy vấn được thực hiện iBatis không? Tôi muốn sử dụng lại truy vấn cho truy vấn UNION.iBatis được thực hiện sql

Ví dụ:

<sqlMap namespace="userSQLMap"> 
    <select id="getUser" resultClass="UserPackage.User"> 
     SELECT username, 
       password 
     FROM table 
     WHERE id=#value# 
    </select> 
</sqlMap> 

Và khi tôi thực hiện truy vấn thông qua

int id = 1 
List<User> userList = queryDAO.executeForObjectList("userSQLMap.getUser",id) 

Tôi muốn nhận SELECT username, password FROM table WHERE id=1

Có cách nào tôi có thể nhận được các truy vấn?

Cảm ơn.

Trả lời

2

Hầu hết các công cụ SQL cho phép bạn "đăng nhập" tất cả các truy vấn được thực thi (thường cùng với thông tin về thời gian truy vấn, số kết quả trả về và tương tự). Bạn có quyền truy cập vào nhật ký của công cụ và bạn có thể định cấu hình nhật ký của nó để nó sẽ ghi lại tất cả những gì bạn cần không?

+0

Đó có thể là có thể (đăng nhập truy vấn và sau đó đọc nó) nhưng cho rằng người dùng đang sử dụng hệ thống đồng thời, tôi sẽ không biết tôi cần truy vấn gì (truy vấn được thực hiện theo yêu cầu của người dùng). Tôi cần phải nhận được các truy vấn vì tôi sẽ được tái sử dụng nó cho một truy vấn UNION (với các giá trị) – qaxi

1

Bạn có thể sử dụng p6spy hoặc jdbcdslog cho điều đó.

+0

Cảm ơn jdbcdslog là chính xác những gì tôi đang tìm kiếm. –

10

Có thể hiển thị thông tin này.iBatis sử dụng một số khung công tác Ghi nhật ký bao gồm Log4J.
Để sử dụng Log4J tạo file log4j.properties trong path.You've lớp để đưa dòng tiếp theo trong file ví dụ:

log4j.logger.com.ibatis=DEBUG 
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG 
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG 
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG 
     
log4j.logger.com.ibatis=DEBUG 
log4j.logger.java.sql.Connection=DEBUG 
log4j.logger.java.sql.Statement=DEBUG 
log4j.logger.java.sql.PreparedStatement=DEBUG 
log4j.logger.java.sql.ResultSet=DEBUG 

Đối với khung đăng nhập khác và thông tin chi tiết xem this link

+0

Tuyệt vời !! Cảm ơn :) – agpt

+0

có vẻ như là một lỗi trong câu trả lời của bạn, trong tài liệu, thuộc tính 'log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = DEBUG' được đề cập, nhưng trong câu trả lời bạn đang sử dụng' log4j.logger.com.ibatis.SQL Map.engine.impl.SQL MapClientDelegate = DEBUG' không có vẻ là cấu hình log4j hợp lệ. – dhblah

9

Thêm phần này vào tệp log4j.xml của bạn và bạn có thể thấy đầu ra trên bàn điều khiển.

<logger name="java.sql" additivity="false"> 
    <level value="debug" /> 
    <appender-ref ref="console" /> 
</logger> 

Bạn sẽ thấy các tham số được truyền, truy vấn đang được thực thi và đầu ra của truy vấn.

3
import java.util.Properties; 
    import org.apache.ibatis.executor.Executor; 
    import org.apache.ibatis.mapping.BoundSql; 
    import org.apache.ibatis.mapping.MappedStatement; 
    import org.apache.ibatis.mapping.MappedStatement.Builder; 
    import org.apache.ibatis.mapping.SqlSource; 
    import org.apache.ibatis.plugin.Interceptor; 
    import org.apache.ibatis.plugin.Intercepts; 
    import org.apache.ibatis.plugin.Invocation; 
    import org.apache.ibatis.plugin.Plugin; 
    import org.apache.ibatis.plugin.Signature; 
    import org.apache.ibatis.session.ResultHandler; 
    import org.apache.ibatis.session.RowBounds; 

    import com.gm.common.orm.mybatis.dialect.Dialect; 
    import com.gm.common.utils.PropertiesHelper; 

    /** 
    * 为Mybatis提供基于方言(Dialect)的分页查询的插件 
    * 
    * 将拦截Executor.query()方法实现分页方言的插入. 
    * 
    * 配置文件内容: 
    * 
    * <pre> 
    * &lt;plugins> 
    * &lt;plugin interceptor="com.gm.common.orm.mybatis.plugin.OffsetLimitInterceptor"> 
    *  &lt;property name="dialectClass" value="com.gm.common.orm.mybatis.dialect.MySQLDialect"/> 
    * &lt;/plugin> 
    * &lt;/plugins> 
    * </pre> 
    */ 

    @Intercepts({@Signature(type=Executor.class,method="query",args={MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class})}) 
    public class OffsetLimitInterceptor implements Interceptor { 
     static int MAPPED_STATEMENT_INDEX = 0; 
     static int PARAMETER_INDEX = 1; 
     static int ROWBOUNDS_INDEX = 2; 
     static int RESULT_HANDLER_INDEX = 3; 

     Dialect dialect; 

     public Object intercept(Invocation invocation) throws Throwable { 
      processIntercept(invocation.getArgs()); 
      return invocation.proceed(); 
     } 

     void processIntercept(final Object[] queryArgs) { 
      // queryArgs = query(MappedStatement ms, Object parameter, RowBounds 
      // rowBounds, ResultHandler resultHandler) 
      MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX]; 
      Object parameter = queryArgs[PARAMETER_INDEX]; 
      final RowBounds rowBounds = (RowBounds) queryArgs[ROWBOUNDS_INDEX]; 
      int offset = rowBounds.getOffset(); 
      int limit = rowBounds.getLimit(); 

      if (dialect.supportsLimit() 
        && (offset != RowBounds.NO_ROW_OFFSET || limit != RowBounds.NO_ROW_LIMIT)) { 
       BoundSql boundSql = ms.getBoundSql(parameter); 
       String sql = boundSql.getSql().trim(); 
       if (dialect.supportsLimitOffset()) { 
        sql = dialect.getLimitString(sql, offset, limit); 
        offset = RowBounds.NO_ROW_OFFSET; 
       } else { 
        sql = dialect.getLimitString(sql, 0, limit); 
       } 
       limit = RowBounds.NO_ROW_LIMIT; 

       queryArgs[ROWBOUNDS_INDEX] = new RowBounds(offset, limit); 
       BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), 
         sql, boundSql.getParameterMappings(), boundSql 
           .getParameterObject()); 
       MappedStatement newMs = copyFromMappedStatement(ms, 
         new BoundSqlSqlSource(newBoundSql)); 
       queryArgs[MAPPED_STATEMENT_INDEX] = newMs; 
      } 
     } 

     // see: MapperBuilderAssistant 
     private MappedStatement copyFromMappedStatement(MappedStatement ms, 
       SqlSource newSqlSource) { 
      Builder builder = new MappedStatement.Builder(ms 
        .getConfiguration(), ms.getId(), newSqlSource, ms 
        .getSqlCommandType()); 

      builder.resource(ms.getResource()); 
      builder.fetchSize(ms.getFetchSize()); 
      builder.statementType(ms.getStatementType()); 
      builder.keyGenerator(ms.getKeyGenerator()); 
      builder.keyProperty(ms.getKeyProperty()); 

      // setStatementTimeout() 
      builder.timeout(ms.getTimeout()); 

      // setStatementResultMap() 
      builder.parameterMap(ms.getParameterMap()); 

      // setStatementResultMap() 
      builder.resultMaps(ms.getResultMaps()); 
      builder.resultSetType(ms.getResultSetType()); 

      // setStatementCache() 
      builder.cache(ms.getCache()); 
      builder.flushCacheRequired(ms.isFlushCacheRequired()); 
      builder.useCache(ms.isUseCache()); 

      return builder.build(); 
     } 

     public Object plugin(Object target) { 
      return Plugin.wrap(target, this); 
     } 

     public void setProperties(Properties properties) { 
      String dialectClass = new PropertiesHelper(properties) 
        .getRequiredString("dialectClass"); 
      try { 
       dialect = (Dialect) Class.forName(dialectClass) 
         .newInstance(); 
      } catch (Exception e) { 
       throw new RuntimeException(
         "cannot create dialect instance by dialectClass:" 
           + dialectClass, e); 
      } 
      System.out.println(OffsetLimitInterceptor.class.getSimpleName() 
        + ".dialect=" + dialectClass); 
     } 

     public static class BoundSqlSqlSource implements SqlSource { 
      BoundSql boundSql; 

      public BoundSqlSqlSource(BoundSql boundSql) { 
       this .boundSql = boundSql; 
      } 

      public BoundSql getBoundSql(Object parameterObject) { 
       return boundSql; 
      } 
     } 

    } 

tôi tham khảo: https://www.java2s.com/Open-Source/Java-Document-2/UnTagged/gmc/com/gm/common/orm/mybatis/plugin/OffsetLimitInterceptor.java.htm

6

Lấy đối tượng Configuration từ SqlSessionFactory của bạn, sau đó:

MappedStatement ms = configuration.getMappedStatement("MyMappedStatementId"); 
BoundSql boundSql = ms.getBoundSql(parameters); // pass in parameters for the SQL statement 
System.out.println("SQL" + boundSql.getSql()); 
+2

Sử dụng thông tin từ bài đăng này để gỡ lỗi selectList của DefaultSqlSession() trong đó MappedStatement là và sử dụng biểu thức: ms.getBoundSql (tham số) .getSql(); – jp093121

Các vấn đề liên quan