gpt4 book ai didi

java - 如何在 JDBI sql api 中打印 @SqlQuery 注释

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:01:54 26 4
gpt4 key购买 nike

我想知道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);

我没有得到预期的答案,所以我想看看实际进入 sql 查询的内容。那么有什么方法可以得到最终处理的查询吗?

最佳答案

你可以通过写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 中使用它。在您的情况下,请像这样使用此注释,

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

如果您使用自定义记录器进行记录,则使用 beforeExecution 方法。

关于java - 如何在 JDBI sql api 中打印 @SqlQuery 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23564383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com