如何在JDBI sql api中打印@SqlQuery注释 [英] How to print the @SqlQuery annotation in JDBI sql api

查看:889
本文介绍了如何在JDBI sql api中打印@SqlQuery注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道jdbi sql api处理sql查询究竟是为了调试目的。
我的接口类如下:

I want to know what exactly sql query is processed by jdbi sql api for debugging purposes. My interface class is following

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);

and later called in another class as String result = myinterfaceclassobject.returnMeValue("Name",1);

我没有得到预期的答案所以我想看看究竟是什么去了sql查询。那么有没有任何方法可以获得最终处理的查询?

I am not getting expected answer so I want to see what actually going to the sql query. So is there any method to get the final processed query?

推荐答案

您可以通过编写SqlCustomizer来记录sql。

You can log the sql by writing SqlCustomizer.

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中使用它。在你的情况下使用这样的注释,

Just include this annotation and use this in SqlObject. In your case use this annotation like this,

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

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

If you use custom loggers for logging, then beforeExecution method.

这篇关于如何在JDBI sql api中打印@SqlQuery注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆