Five MyBatis parameter passing
This article is the fifth section of the learning process, check this column for the whole process
Article Directory
MyBatis provides the ParamNameResolver class for parameter encapsulation
MyBatis parameter encapsulation:
single parameter
- POJO type
- Map collection
- Collection
- List
- Array
- other types
multiple parameters
1 multiple parameters
User select(@Param("username") String username,@Param("password") String password);
Encapsulated as a map collection
map has keys and values
The value is the value of the parameter (String username, String password)
Keys come in two formats:
map.put("arg0", parameter value 1)
map.put("param1", parameter value 1)
map.put("arg1", parameter value 2)
map.put("param2", parameter value 2)
If you don't use the @Param annotation in the future, you can use parameters such as arg0 param1 to get the value
But this method is not recommended, because the readability is poor after there are too many parameters
You can use the @Param annotation to replace the default arg key name in the Map collection
2 single parameter
POJO type: used directly, the attribute name is consistent with the parameter placeholder name
Map collection: used directly, the key name is consistent with the parameter placeholder name
Collection: Encapsulated as a Map collection, you can use the @Param annotation to replace the default arg key name in the Map collection
map.put(“arg0”,collection set);
map.put(“collection”,collection set);
List: You can use the @Param annotation to replace the default arg key name in the Map collection
map.put("arg0", list collection);
map.put(“collection”, list set);
map.put(“list”,list set);
Array: You can use the @Param annotation to replace the default arg key name in the Map collection
map.put("arg0", array);
map.put("array", array);
Other types: use directly. such as int
Summarize
In the future, the @Param annotation will be used to modify the default key name in the Map collection, and use the modified name to obtain the value, which is more readable