Analysis on the causes of exceptions caused by improper use of mybatis foreach tag

Exception generation scenario and exception information

Last week, due to the use of in the mapper interface method of mybatis, we implemented map The generic class of the entry interface, and the corresponding SQL statement of this method also uses the foreach tag, resulting in an exception. The following is the exception information:

Because I don't know much about mybatis, it took me a long time to debug and find the specific cause of the exception.

Next, I will reproduce the exception and analyze the cause of the exception.

Abnormal recurrence

In order to reproduce the above exception, a demo is written. Relevant codes are as follows:

Database table structure:

KeyValue. Java parameter class:

Dao class goodsroomnight30daysmapper java

mybatis-config. XML file:

GoodsRoomnight30daysMapper. Main contents of XML file:

The above is the main code to reproduce this exception. The complete code can be viewed in GitHub: https://github.com/misterzhou/java-demo/tree/master/test-spring/spring-server

Reproduce the abnormal test code goodsroomnight30daystest java:

Sell a pass. Don't look down first and think about the causes of the abnormality (students who are proficient in using foreach tags should be able to see the clue).

Anomaly finding process and anomaly analysis

In the project, because the parameter class and return result class of Dao method often contain a key and the value corresponding to the key, in order to avoid repeatedly defining the class, I define a class that implements map The keyValue generic class of the entry interface. See the previous section for details.

GoodsRoomnight30daysMapper. The insertbatch () method uses this generic class. After running, the exception information mentioned at the beginning of this article is thrown.

When you see the exception information, you focus on whether mybatis does not support generics well enough. I asked my colleague (@ Shengnan), who tried on his machine and found nothing abnormal. That's strange. After a closer look at the code, I find that the difference is that my keyValue generic class implements map Entry interface. At this time, I don't know the description of foreach tag on the official website of mybatis:

Any iteratable object (such as list, collection, etc.) and any dictionary or array object can be passed to foreach as collection parameters. When using an iteratable object or array, index is the number of current iterations, and the value of item is the element obtained in this iteration. When using a dictionary (or a collection of map.entry objects), index is the key and item is the value.

The next step is to see the specific causes of exceptions through debug. So we first implemented map with Debug the keyValue class code of the entry interface. You can see from the exception log that the exception is in defaultsqlsession Java: if line 200 is thrown, set the breakpoint to defaultsqlsession Java: Line 197, and then execute step by step down. When executing to foreachsqlnode Java: 73 line, finally full of enlightenment. Let's take a look at the debug call chain diagram:

Let's look at the specific code foreachsqlnode Java: 73 (this class is the node class of foreach tag object):

At this time, the specific exception reason is obvious. The class of the O object here is the keyValue class, because the keyValue class implements map Entry interface, so o instance map If the entry is true, mybatis assigns the key value to the index attribute of foreach and the value value to the item attribute. Here, the integer object with the value of 5 is assigned to the item attribute. So goodsroomnight30daysmapper The object corresponding to the item attribute of the select tag with ID insertbatch in the XML does not have an item Key and item Value attribute, which is the final cause of the exception.

The above is the analysis of the causes of exceptions caused by improper use of mybatis foreach tag introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>