Preface
Most projects now 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 requires passing multiple parameters. Facing various complex business scenarios, passing parameters is also a kind of knowledge.
Below we summarize the following methods of multi-parameter transmission.
Method 1: Sequential parameter transfer method
The numbers in #{} represent the order in which you pass the parameters.
This method is not recommended to use, the SQL layer expression is not intuitive, and errors are prone to occur once the order is adjusted.
Method 2: @Param annotation method of passing parameters
The name in #{} corresponds to the name modified in the annotation @Param brackets.
This method is quite intuitive when there are not many parameters, so it is recommended to use it.
Method 3: Map parameter transfer method
The name in #{} corresponds to the key name in the Map.
This method is suitable for passing multiple parameters and the parameters are volatile and can be flexibly transmitted.
Method 4: Java Bean parameter transfer method
The name in #{} corresponds to the member attributes in the User class.
This method is very intuitive, but it requires building an entity class. It is not easy to expand. It requires adding attributes to use according to the situation.
Parameter passing method when using Mapper interface
When Mybatis uses the Mapper interface for programming, the underlying layer uses a dynamic proxy mechanism, which is called on the surface, but in fact it is through the corresponding method of SqlSession called by the dynamic proxy, such as selectOne(). Interested friends can check the getMapper() method of DefaultSqlSession, which will eventually obtain a MapperProxy object that proxys the Mapper interface. When the MapperProxy object calls the Mapper interface method, it will convert the passed parameters, and then use the converted parameters as the parameter to call the corresponding operation methods corresponding to SqlSession (such as selectOne, insert, etc.). The conversion process can be implemented with reference to the execute() method of MapperMethod. Simply put, the following rules:
1. If the passed-through is a single parameter and is not named with the @Param annotation, then the corresponding method of SqlSession is called directly with the single parameter as the real parameter.
2. If the passed is not a single parameter or contains parameters named with @Param annotation, the corresponding parameter will be converted into a Map for passing. The specific rules are as follows:
2.1. The corresponding parameters will be stored as keys in the target map 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 a parameter named with @Param annotation, it is stored in the target map with the name specified by @Param as the Key.
2.3. If the parameters are not named after @Param annotation, they are stored as keys in the target map in the form of 0, 1, and N in order. The first parameter is 0 and the Nth parameter is N.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.