我在调试器中使用--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
答案 0 :(得分:2)
您可以在代码周围添加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());