In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "MyBatis @ Param annotation how to achieve", the content is detailed, the steps are clear, and the details are handled properly. I hope this "MyBatis @ Param annotation how to achieve" article can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.
Let's start with the conclusion:
When there is only one input parameter and the @ Param annotation is not used, MyBatis will pass this parameter directly; when there is more than one input parameter, or if the @ Param annotation is used, MyBatis will encapsulate the parameter and pass it in Map. In this case, the key of Map can be classified as follows:
There will be key such as param1 and param2 in Map, and the order corresponds to the order of the input parameters. With or without @ Param annotations.
For the parameters of the @ Param annotation, the name given in the annotation is saved in Map as key
For parameters that are not annotated with @ Param, 1, 2, 3 are used in Map. Such numbers are used as key to save the input parameters in order.
Let's take a look at the source code.
First, determine whether the parameters of the @ Param annotation are useful in a method:
Private boolean hasNamedParams (Method method) {final Object [] [] paramAnnos = method.getParameterAnnotations (); for (Object [] paramAnno: paramAnnos) {for (Object aParamAnno: paramAnno) {if (aParamAnno instanceof Param) {return true;} return false;}
If the parameter of the @ Param annotation is used, take out the parameter name given in the annotation:
Private String getParamNameFromAnnotation (Method method, int I, String paramName) {final Object [] paramAnnos = method.getParameterAnnotations () [I]; / / get the note for (Object paramAnno: paramAnnos) {if (paramAnno instanceof Param) {paramName = ((Param) paramAnno). Value (); break;}} return paramName;}
Note the input parameter of the method. Method indicates the method, I represents the parameter, and paramName is the name of the parameter passed in. If the parameter is not annotated with @ Param, the passed paramName is returned.
The following method returns a TreeMap, whose key indicates the order of the parameters, for example, key=0 represents the zero parameter; value represents the name of the parameter, and if marked with @ Param, it is the name of the annotated parameter, otherwise it is equal to key, that is, the sequence number of the parameter is used as the name of the parameter.
Private SortedMap getParams (Method method, boolean hasNamedParameters) {final SortedMap params = new TreeMap (); final Class [] argTypes = method.getParameterTypes (); for (int I = 0; I < argTypes.length; iTunes +) {if (! RowBounds.class.isAssignableFrom (argTypes [I]) &! ResultHandler.class.isAssignableFrom (argTypes [I])) {String paramName = String.valueOf (params.size ()) / / parameter name. Default is the sequence number of the parameter if (hasNamedParameters) {/ / if @ Param annotation is used, get the parameter name paramName = getParamNameFromAnnotation (method, I, paramName) of the annotation; / / here paramName is passed as the parameter, indicating the default value} params.put (I, paramName);}} return params;}
HasNamedParameters only gives whether the method has a parameter annotated with @ Param from the dimension of the whole method. Even if its value is true, the @ Param annotation may not be used for a specific parameter, so the paramName passed in by calling getParamNameFromAnnotation is returned as the default value, that is, the sequence number of the parameter.
Finally, the parameters of the calling method are converted to those used internally by MyBatis:
Public Object convertArgsToSqlCommandParam (Object [] args) {final int paramCount = params.size (); if (args = = null | | paramCount = = 0) {return null;} else if (! hasNamedParameters & & paramCount = = 1) {return args [params.keySet (). Iterator (). Next (). IntValue ()];} else {final Map param = new ParamMap (); int I = 0 For (Map.Entry entry: params.entrySet ()) {param.put (entry.getValue (), args [entry.getKey (). IntValue ()]); / / issue # 71, add param names as param1, param2...but ensure backward compatibility final String genericParamName = "param" + String.valueOf (I + 1) If (! param.containsKey (genericParamName)) {param.put (genericParamName, args [entry.getKey ()]);} iTunes;} return param;}}
Where args is the input parameter of the Dao method, which has been converted into an array, which is actually the parameter passed in by the invoke method of the dynamic proxy.
This method first counts the input parameters, and the params used is the return value of the getParams method described earlier.
If (! hasNamedParameters & & paramCount = = 1)
The above condition judgment, that is, the method does not use the @ Param annotation and has only one parameter, then returns
Args [params.keySet () .iterator () .next () .intValue ()]
That is, it is returned directly as an Object.
If the above conditions are not met, first create a new Map as the return value:
Final Map param = new ParamMap ()
Then, set the key and value of map:
Param.put (entry.getValue (), args [entry.getKey () .intValue ()])
Then, for the sake of compatibility, do the following
Final String genericParamName = "param" + String.valueOf (I + 1); if (! param.containsKey (genericParamName)) {param.put (genericParamName, args [entry.getKey ()]);}
That is, set up key such as param1 and param2.
At this point, the required parameter object Object is built, which encapsulates multiple parameters passed in by Dao, and affects the type of the parameter object (whether it is map or not) depending on whether the parameter has the @ Param annotation.
After the parameter encapsulation is complete, the next step is to pass it to SqlSession.
After reading this, the article "how to implement MyBatis @ Param annotations" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.