Five MyBatis parameter passing

Five MyBatis parameter passing

This article is the fifth section of the learning process, check this column for the whole process

column directory

MyBatis provides the ParamNameResolver class for parameter encapsulation

MyBatis parameter encapsulation:

  • single parameter

    1. POJO type
    2. Map collection
    3. Collection
    4. List
    5. Array
    6. 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

  1. POJO type: used directly, the attribute name is consistent with the parameter placeholder name

  2. Map collection: used directly, the key name is consistent with the parameter placeholder name

  3. 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);

  4. 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);

  5. 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);

  6. 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

Guess you like

Origin blog.csdn.net/qq_45842943/article/details/126356958