加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

如安在JDBI sql api中打印@SqlQuery讲明

发布时间:2021-03-23 21:47:08 所属栏目:编程 来源:网络整理
导读:我想知道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);} 然后在另一个类中

我想知道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,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要领.

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读