2016-08-02 8 views

答えて

0

SqlCustomizerを記述してsqlを記録できます。

import org.skife.jdbi.v2.StatementContext; 
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizer; 
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizerFactory; 
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizingAnnotation; 
import org.skife.jdbi.v2.tweak.StatementCustomizer; 

import java.lang.annotation.*; 
import java.lang.reflect.Method; 
import java.sql.PreparedStatement; 
import java.sql.SQLException; 

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.TYPE) 
@SqlStatementCustomizingAnnotation(LogSqlFactory.Factory.class) 
public @interface LogSqlFactory { 

    static class Factory implements SqlStatementCustomizerFactory { 

     @Override 
     public SqlStatementCustomizer createForMethod(Annotation annotation, Class sqlObjectType, Method method) { 
      return null; 
     } 

     @Override 
     public SqlStatementCustomizer createForType(Annotation annotation, Class sqlObjectType) { 
      return q -> q.addStatementCustomizer(new StatementCustomizer() { 
       @Override 
       public void beforeExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException { 
        System.out.println(stmt.toString()); 
       } 

       @Override 
       public void afterExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException { } 

       @Override 
       public void cleanup(StatementContext ctx) throws SQLException { } 
      }); 
     } 

     @Override 
     public SqlStatementCustomizer createForParameter(Annotation annotation, Class sqlObjectType, Method method, Object arg) { 
      return null; 
     } 
    } 

} 

この注釈を含めてSqlObjectで使用してください。ロギング用のカスタムロガー、そしてbeforeExecutionメソッドを使用している場合は、あなたのケースでは、このよう

@LogSqlFactory 
public inteface myinteface{ 
@SqlQuery("select :c1 from tablename where cond = :cd") 
    String returnMeValue(@Bind("c1") String c1, @Bind("cd") Integer cd); 
} 

をこのアノテーションを使用します。

+0

私のためには機能しませんでした –

0

DropwizardのDBIFactoryを使用していますか?この方法でログに記録するようにJDBIを設定します。

関連する問題