2016-08-02 15 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配置爲以開箱即用的方式登錄。

相關問題