当前位置: 首页 > 知识库问答 >
问题:

如何显示来自Spring和MyBatis的SQL错误?

华睿识
2023-03-14

我使用--debug在调试器中运行了以下代码,但它不会打印出任何SQL错误。它会在异常情况下静默失败并从servlet请求返回。

    System.out.println("sourceMapper = " + sourceMapper);
    Source source = sourceMapper.findByHost(host);
    System.out.println("source = " + source);

这是输出

2018-05-11 13:47:58.080  INFO 29392 --- [nio-8080-exec-2] c.s.shorturl.apis.ShortUrlApiController  : Method name: doUrlRequest() user agent is = curl/7.59.0
sourceMapper = org.apache.ibatis.binding.MapperProxy@30a1e904
2018-05-11 13:47:58.359  INFO 29392 --- [nio-8080-exec-2] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2018-05-11 13:47:58.489  INFO 29392 --- [nio-8080-exec-2] o.s.jdbc.support.SQLErrorCodesFactory    : SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
2018-05-11 13:47:58.541 DEBUG 29392 --- [nio-8080-exec-2] o.s.b.w.f.OrderedRequestContextFilter    : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@645ec595

如何打印遇到的 SQL 错误?或者找出它失败的原因以及异常是什么?

MyBatis 3.4.5, MyBatis Spring 1.3.1, MyBatis Spring Boot Autoconfigure 1.3.1, MyBatis Spring Boot Starter 1.3.1, Spring Boot 1.5.6

共有1个答案

仲孙华奥
2023-03-14

您可以在代码周围添加try/catch并记录捕获的异常:

try {
    System.out.println("sourceMapper = " + sourceMapper);
    Source source = sourceMapper.findByHost(host);
    System.out.println("source = " + source);
} catch (Exception e) {
    // log using slf4j's Logger
    logger.error("An exception caught", e);
    // or print it to standard output
    e.printStackTrace();
}

应该在某个地方有一个Logger实例(可能在同一个类中):

private final Logger logger = LoggerFactory.getLogger(this.getClass());
 类似资料:
  • 本文向大家介绍Spring Boot集成Mybatis中如何显示日志的实现,包括了Spring Boot集成Mybatis中如何显示日志的实现的使用技巧和注意事项,需要的朋友参考一下 在Spring Boot集成Mybatis的项目中,如果出现SQL语句执行问题,我们需要进行排查。此时就需要打印对应的SQL语句,那么该如何操作才能正常打印出对应的SQL语句呢? 其实操作非常简单,在applicat

  • 我想在JTable中显示MySQL中的数据,但只显示了表中的最后一行。请帮帮我。我知道我有一个问题,因为jt=newjtable(数据,列)每次都为每行创建一个新表(删除之前的),但我找不到正确的选项。

  • org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: The 'JSON_EXTRACT(all_info, '$' property of null is not a List or Array. 关键是这个sql 用navi

  • 问题内容: 我正在使用JDBC在我的Java项目中连接到MySQL。我已经在mySQL表中实现了一些检查约束。 我有一个表单,用户可以在其中输入一些值并将其提交到MySQL表。如果违反约束,则Java会给出SQLException。 我的 目的 是显示有关表中违反 哪种 条件的弹出消息。 如何在Java中检索此信息? 我尝试了一个记录器,但它仅在控制台中显示冗长的错误消息。 问题答案: 您可以查看

  • 问题内容: 我的包含。 如何在明细栏中显示此列表的数据? 问题答案: 这是工作示例。 该样本的关键点: 使用的_THIS表达; 使用列表(JR:列表) 在组分详细频带 用于生成报告的代码段: JavaBean代码: jrxml文件:

  • 在中是否存在防止SQL注入的好方法? 我将此方法用于。实际上,我在查询中使用用户给出的字符串参数。 有什么想法吗?