Summary of four methods of passing multiple parameters in mybatis

preface

Now most projects use mybatis, but some companies use hibernate. The biggest feature of using mybatis is that SQL needs to be written by itself, and writing SQL needs to pass multiple parameters. In the face of various complex business scenarios, passing parameters is also a kind of knowledge.

The following several methods of multi parameter transfer are summarized.

Method 1: sequential parameter transfer method

#The numbers in {} represent the order in which you pass in parameters.

This method is not recommended, the SQL layer expression is not intuitive, and it is easy to make mistakes once the order is adjusted.

Method 2: @ param annotation parameter passing method

#The name in {} corresponds to the name modified in the @ param bracket of the annotation.

This method is relatively intuitive in the case of few parameters, and is recommended.

Method 3: map parameter transfer method

#The name in {} corresponds to the key name in map.

This method is suitable for transmitting multiple parameters, and the parameters are changeable and can be transmitted flexibly.

Method 4: Java Bean parameter passing method

#The name in {} corresponds to the member attribute in the user class.

This method is very intuitive, but it needs to build an entity class. It is not easy to expand. It needs to add attributes. It depends on the situation.

How to pass parameters when using mapper interface

When mybatis uses mapper interface for programming, in fact, the underlying layer adopts dynamic proxy mechanism. On the surface, it is the mapper interface called, but in fact, it is the corresponding method of sqlsession called through dynamic proxy, such as selectone (). Interested friends can view the getmapper () method of defaultsqlsession, It will eventually get a mapperproxy object that proxies the mapper interface. Mapperproxy object will convert the passed parameters when calling mapper interface method, and then call the corresponding operation methods (such as selectone, insert, etc.) of sqlsession with the converted parameters as input parameters. The conversion process can be implemented by referring to the execute() method of mappermethod. Simply put, the following rules apply:

1. If the passed parameter is a single parameter and is not named with the @ param annotation, the corresponding method of sqlsession will be called directly with the single parameter as the real parameter.

2. If the passed parameter is not a single parameter or contains a parameter named with the @ param annotation, the corresponding parameter will be converted into a map for transmission. The specific rules are as follows:

2.1. The corresponding parameters will be stored in the target map as keys in the form of Param1, param2 and paramn in order. The first parameter is Param1 and the nth parameter is paramn.

2.2 if the parameter is named after the @ param annotation, the name specified by @ param is stored as a key in the target map.

2.3. If the parameter is not named with the @ param annotation, it is stored in the target map as a key in the form of 0, 1 and N in sequence. The first parameter is 0 and the nth parameter is n.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

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