如何在JDBI sql api中打印@SqlQuery批注
发布时间:2021-03-19 04:44:52 所属栏目:MsSql教程 来源:网络整理
导读:我想知道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方法. (编辑:濮阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 中国移动公布积分奖励计划,爱国者智慧棒入选
- html中table表格的内容水平和垂直居中显示
- sql-server – SQL Server ROWLOCK over SELECT如果不存在I
- HTML表格标记教程(3):宽度和高度属性WIDTH、HEIGHT
- Linux统一遇障碍 UserLinux拟提高其可用度
- 分析:Longhorn会让微软更加“霸道”吗
- 数据库 – 为什么他们使用DBMS_STATS.GATHER_TABLE_STATS?
- .net – 如何在WPF DataGrid中定义自己的列?
- Adobe携手施乐制定JDF新标准 抵制大鳄微软
- 广角变焦镜头 佳能16-35mm f/2.8L III