mybatis parameter passing parameters, processing values

  1. A single parameter: mybatis not do special treatment

    1. Value way: Parameter Name} where # {parameter names need not consistent with the reference name of the method form, can use any parameter name to accept the arguments
    2. Examples: Method: update (Integer id) sql map file values ​​# {a}, this can be taken to a value
  2. A plurality of parameters: mybatis do special processing, that is packaged into a plurality of parameter map, key: param1 .... paramN, value: the value of the parameter passed, # {} is the specified key is acquired from the map of the value, i.e., # {param1}, # {param2} etc.

  3. A plurality of process parameters: the custom package key parameters

    1. Method get (@Param ( "id") Integer id, @ Param ( "name") String name) key using annotations specified value @Param
    2. Value: # {id}, # {name} # {taken from a predetermined value corresponding key}
  4. If multiple parameters of our business is just a logical data model (data model can only partially) can be directly passed pojo

    1. {#} Removed name attribute property value passed pojo
  5. If more than one parameter is not our business logic of our model, there is no corresponding pojo, not recommended for convenience, we can pass in map

    1. # {Key} corresponding map values ​​taken
  6. If more than one parameter is not our business logic of our model, but is often used, the recommended package into a vo (actually a simple javabean, all parameters package to a java class)

  7. If only one parameter, but the parameter type is a collection (collection, list, set) or array, will do special handling, package them into map

    1. If the parameter is a collection, then the map is a collection of key
    2. If the list is a collection, then the key is the list of map, values ​​# {list [0]}, remove list represents the first element
    3. If the array, so the key is Array map, values ​​# {array}
  8. $ # {} {} Values ​​and difference values

    1. # {} Are pre-compiled form, set the parameter to sql, the use of the preparedStatement, preventing sql injection
    2. $ {}, The value taken in directly fight the sql, security issues
    3. Local native jdbc not support placeholders can zhi value $ {}, such as sub-table, sorting
  9. # {} Richer usage

    1. Prescribed parameters of the rules a little: javaType, jdbcType, mode (stored procedures), numericScale,
      resultMap, typeHandler, jdbcTypeName, expression The (ready to support future functionality)

    2. the jdbcType: database type: typically needs to be provided under certain conditions:

      1. In our data is null when some of the database may not recognize mybatis default handling of the null. For example, Oracle (error);

      2. JdbcType OTHER: invalid type; because mybatis are all null mapping of native Jdbc OTHER type, oracle does not properly handle;

      3. Because the global configuration: jdbcTypeForNull = OTHER; oracle is not supported; two ways

        1. #{email,jdbcType=OTHER};

        2. jdbcTypeForNull = NULL

          <setting name="jdbcTypeForNull" value="NULL"/>

Guess you like

Origin www.cnblogs.com/syncmr/p/10955403.html
Recommended