Usage of mybatis passing multiple parameters for SQL query

PS: there are two ways to pass multiple parameters in ibatis3: one is to use Java Map, the other is to use JavaBeans.

When only to xxmapper When passing a parameter in an XML file, you can simply use "_parameter" to receive xxmapper The parameters passed in by Java and substituted into the query, for example:

(1) The xxxmapper.java file is defined as follows:

(2) In this case, you can use "_parameter" in the corresponding xxmapper.xml file to receive this parameter:

However, if in xxmapper If multiple parameters are passed in the java file, the above form cannot be used to receive parameters. At this time, there are two solutions to solve this problem:

A map < string, Object > collection is passed into the XML file, and then the parameters in the map collection can be used normally in the XML file.

Specific examples are as follows:

(1) The xxxmapper.java file is defined as follows:

List
findAll(Map

parms);

(2) Pass parameters to map in the specific implementation class defined above:

(3) At this time, the corresponding xxmapper.xml file uses "Java. Util. Map" to receive the map collection:

Note: the above example implements paging query data. We can find that using map to transfer parameters is not good, because it makes there only one map parameter in the interface, and other people don't know what parameters need to be transferred to the map when maintaining

2. Solve the problem by adding @ param annotation to the parameter:

(1) Add @ param annotation to the parameters in the method of xxxmapper.java file. The value in this annotation corresponds to the parameter name used in the XML file:

(2) At this time, the corresponding values in the @ param annotation can be used normally in the corresponding places in the xxxmapper.xml file:

Note: it should be noted that the parameters in the if condition judgment are written differently from those in the SQL statement, and the variables in the if judgment are not added #{}

Here are two methods for passing multiple parameters through mybatis

Map passes multiple parameters

Parametertype can be an alias or fully qualified name, map or Java util. Map, both are OK

Passing multiple parameters through a JavaBean

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