我想知道jdbi sql api处理SQL查询究竟是什么用于调试目的.
我的接口类如下
我的接口类如下
public inteface myinteface{ @sqlQuery("select :c1 from tablename where cond = :cd") String returnMeValue(@Bind("c1") String c1,@Bind("cd") Integer cd); }
然后在另一个类中调用String result = myinterfaceclassobject.returnMeValue(“Name”,1);
解决方法
您可以通过编写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,Method method,Object arg) { return null; } } }
只需包含此注释并在sqlObject中使用它.在你的情况下使用这样的注释,
@LogsqlFactory public inteface myinteface{ @sqlQuery("select :c1 from tablename where cond = :cd") String returnMeValue(@Bind("c1") String c1,@Bind("cd") Integer cd); }