4 ways to pass multiple parameters in Mybatis

Most projects now use Mybatis, but some companies also use Hibernate. The biggest feature of using Mybatis is that you need to write sql yourself, and writing sql requires passing multiple parameters. Facing various complex business scenarios, passing parameters is also a kind of knowledge. Here is a summary of the following methods of multi-parameter passing:

Method 1: Sequential parameter passing method (not recommended)

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

This method is not recommended because the SQL layer expression is not intuitive and is prone to errors once the order is adjusted.

Method 2: @Param annotation parameter passing method (recommended)

The name in #{} corresponds to the name modified in the annotation @Param brackets.

This method is relatively intuitive when there are not many parameters and is recommended.

When there is only one parameter in the interface (and @Param() is not used), the response parameter type parameterType needs to be added to the xml; if there are multiple parameters (@Param() is used for each parameter), no Will read the parameter type parameterType and directly obtain the value in the parameter.

Method 3: Map parameter transfer method (recommended)

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 passed flexibly.

Method 4: Java Bean parameter passing method (recommended)

The names in #{} correspond to the member attributes in the User class. This method is very intuitive, but it requires building an entity class, which is not easy to extend. You need to add attributes and use it according to the situation.

If you reprint, please indicate the source: Open Source Byte   https://sourcebyte.vip/article/337.html

Guess you like

Origin blog.csdn.net/qq_35634154/article/details/132839277