Explain mybatis foreach collection example

In the process of SQL development, dynamic construction of in set condition query is a common usage. The foreach function is provided in mybatis. This function is more powerful. It allows you to specify a set, declare set items and index variables, which can be used in the element body. It also allows you to specify open and closed strings and place separators between iterations. This element is smart, and it doesn't accidentally attach extra delimiters.

The following is a demo example:

However, due to the use of this piece in the official documents, the description is relatively short, and the details are ignored (it may be a consistent problem in the open source project documents). Many students have encountered problems in using it. Especially in the foreach function, what is the use of the collection attribute and what precautions should be taken. Due to incomplete documents, this can only analyze the relevant requirements of each attribute through source code analysis.

The collection property is used to receive the input array or list interface implementation. However, for the name requirements, mybatis is still a little difficult to understand in the implementation, so we need to pay special attention to this point.

Let's start analyzing the source code (mybatis version 3.0.5 is used in the notes)

First find the entry where mybatis executes SQL configuration parsing

MapperMethod. Public object execute (object [] args) in Java class is the entry for execution

For the in set query, the application is the selectforlist or selctformap method.

However, no matter which method is called, the parameter object [] type passed in from the original JDK will be converted into an object through the getparam method. What does this method do? The analysis source code is as follows:

In the two places marked red in the figure above, I was surprised to find that the processing methods of one parameter and multiple parameters are different (most of the problems encountered by many students later come from this place). If the number of parameters is greater than one, it will be encapsulated into a map, and the key value will be used if the param annotation of mybatis is is used. Otherwise, the data sequence number will be used uniformly by default, starting from 1. Note this problem first and continue to analyze the code. Next, if it is a selectforlist operation (the corresponding methods will be applied to other operations), the public list selectlist (string statement, object parameter, rowboundaries, rowboundaries) method of defaultsqlsession will be called

Another discovery, see the source code as follows:

In the red part of the icon, the parameters are encapsulated again. Let's take a look at the code

Now it is clear that if the parameter type is list, it must be specified as list in the collection. If it is a data group, it must be specified as array in the collection property

Now the problem is clear. If it is a parameter, the value of collection depends on your parameter type.

If there are multiple values, they all start with numbers unless specified with the annotation param, so it is useless to specify any value in the collection. The following figure shows the result of debug.

According to the above analysis results, a solution is given below, hoping to help you.

When using this function, you should pay special attention to the following rules:

1. When there is only one query parameter

2. When multiple parameters are queried, such as findbyids (string name, long [] IDS), special attention should be paid. When passing parameters, the map method must be used, so that the name can be specified in the collection attribute

Here is an example

The complete example is as follows: for example, there is a query function, and the mapper interface file defines the following methods:

The SQL assembly method using in query is as follows:

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
分享
二维码
< <上一篇
下一篇>>