SpringMVC + Mybatis bug调试 SQL正确,查数据库却返回NULL

傷城~ 2022-04-18 05:03 340阅读 0赞

今天碰到个bug,有点意思

背景是SpringMVC + Mybatis的一个项目,mapper文件里写了一条sql 大概相当于 select a from tableA where b = “123” 这样的级别

然后不管传进去的是什么 数据库里有没有 都会返回null


第一反应是sql语句写错了,比如把1和小写L弄混了之类的,传给sql的参数里有奇怪的空格等等

于是打开debug log 拿到传给sql的preparedStatement 和对应的参数

复制到console里自己查了一下,可以执行,返回结果也正确,说明不是sql的问题


既然不是sql的问题,那只好调试一下代码了

既然preparedStatement sql能够被打印出来,所以就不从业务逻辑加断点了,直接定位到PreparedStatement类 找到execute方法,上个断点开始单步

单步的时候发现了奇怪的现象,ide提示说源代码和class文件对应行不一致,单步调试时代码在不同行之间乱跳,并且对我正在监视的变量报了一个类中不存在该变量的错

所以怀疑是引用冲突了


接下来确认一下是不是引用了奇怪的类 ,用下面这个方法去定位一下PreparedStatement的位置

复制代码

  1. public static String where(final Class cls) {
  2. if (cls == null)throw new IllegalArgumentException("null input: cls");
  3. URL result = null;
  4. final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
  5. final ProtectionDomain pd = cls.getProtectionDomain();
  6. if (pd != null) {
  7. final CodeSource cs = pd.getCodeSource();
  8. if (cs != null) result = cs.getLocation();
  9. if (result != null) {
  10. if ("file".equals(result.getProtocol())) {
  11. try {
  12. if (result.toExternalForm().endsWith(".jar") ||
  13. result.toExternalForm().endsWith(".zip"))
  14. result = new URL("jar:".concat(result.toExternalForm())
  15. .concat("!/").concat(clsAsResource));
  16. else if (new File(result.getFile()).isDirectory())
  17. result = new URL(result, clsAsResource);
  18. }
  19. catch (MalformedURLException ignore) {}
  20. }
  21. }
  22. }
  23. if (result == null) {
  24. final ClassLoader clsLoader = cls.getClassLoader();
  25. result = clsLoader != null ?
  26. clsLoader.getResource(clsAsResource) :
  27. ClassLoader.getSystemResource(clsAsResource);
  28. }
  29. return result.toString();
  30. }

复制代码

在IDEA里 单步时按alt+F8 用where方法去查一下类,发现jvm没有加载我认为的msql-java-connector-5.1.63 而是加载了一个内部类库里的java-connector,定位进去看了一下 确实是写的有问题,原因找到了。


最后到maven依赖里,打开依赖树,找到了加载这个自定义connector的pom条目,配置了 ,然后重启项目,解决。

发表评论

表情:
评论列表 (有 0 条评论,340人围观)

还没有评论,来说两句吧...

相关阅读