MyBatis Incoming parameters and ParameterType

Source: Internet
Author: User
Tags map data structure

The SELECT, INSERT, update, and delete elements in the MyBatis mapper file have a ParameterType property that is used for the parameter types that the corresponding Mapper interface method accepts.

The types of parameters that can be accepted are primitive types and complex types.

The Mapper interface method generally takes a parameter, and you can bind multiple parameters to a map as input parameters by using the @param annotation.

      1. Simple data types

Mapper Interface Method:

User Selectbyprimarykey (Integer ID);

SQL mapping:

 <  select  id  = "Selectbyprimarykey"   Resultmap  = "Baseresultmap"   ParameterType  = "Java.lang.Integer"  >   select  <  include  refid  = "Base_column_list"  />   from base.tb_user where id = #{id,jdbctype=integer}  </ select  >  

For a simple data type, the SQL mapping statement directly #{the variable name} in this way, the "variable name" can be arbitrary. The Mapper interface method passes over the value, as to what its name is actually not test and need not know.
and Java reflection can only get the type of the method parameter, it is impossible to know the name of the method parameter.

For example above, using #{id} to refer to is just more intuitive, using other names to refer to the same. So when you pass a parameter in the IF element, you have to use _parameter to refer to the parameter. Like this:

<SelectID= "Selectbyprimarykey"Resultmap= "Baseresultmap"ParameterType= "Java.lang.Integer" >Select<includerefID= "Base_column_list" />From Tb_user<ifTest= "_parameter! = 0">WHERE id = #{id,jdbctype=integer}</if></Select>

If you use the ID in test conditions, you will be prompted with an error because the parameter does not actually have a name, only a value or reference, and can only be referenced using _parameter.

      1. Object type

In the case of a Java complex object type, the attribute name of the object can be directly referenced in the SQL mapping statement, where the property name is the actual real name and is not arbitrarily specified.
Mapper Interface Method:

int Insert (user user);

SQL mapping:

<id= "Insert"  parametertype= "User"  usegeneratedkeys = "true" Keyproperty = "id" >   INSERT INTO tb_user (name, sex)  values (#{name,jdbctype=char}, #{sex,jdbctype=char})</  Insert>

Although you can explicitly refer to the property name of an object, if you want to test the incoming user parameter in the IF element, you still use _parameter to refer to the actual argument passed in, because the name of the user object passed in is not testable. If you test the properties of an object, you can simply refer to the property name.

To test the user object:

<test= "_parameter! = null">

To test the properties of the user object:

<test= "Name! = NULL">
      1. Map type

Incoming map type, directly through #{keyname} can refer to the value corresponding to the key. Multiple parameter values that use the @param annotation are also assembled into a map data structure, and there is no difference in passing the map directly.

Mapper Interface:

int Updatebyexample (@Param ("user") user user, @Param ("example") userexample example);

SQL mapping:

<UpdateID= "Updatebyexample"ParameterType= "Map" >update tb_user Set id = #{user.id,jdbctype=integer}, ...<ifTest= "_parameter! = null" >    <includerefID= "Update_by_example_where_clause" />  </if></Update>

Note here that the test passed in the map is empty, still using _parameter

      1. Collection type

You can pass a List of instance or an Array to MyBatis as a parameter object. When you do, MyBatis'll automatically wrap it in a Map, and key it by name. List instances would be keyed to the name "list" and array instances would be keyed to the name "array".

You can pass an object of list or array type as a parameter, MyBatis automatically wraps the list or array object into a Map object, and the list type Object uses list as the key name, and the array object uses array as the key name.

A collection type is typically used to construct an in condition that uses a foreach element in a SQL map file to traverse a list or array element.

Mapper Interface:

User selectuserinlist (list<interger> idlist);

SQL Dynamic Statement Mapping:

<SelectID= "Selectuserinlist"Resulttype= "User">SELECT * from USER WHERE ID in<foreachItem= "Item"Index= "Index"Collection= "List"Open="("Separator=","Close=")">#{item}</foreach></Select>
      1. Collection properties in the object type

For a single pass-through list or array, when mapped in a SQL mapping file, it can only be referenced by a list or array. However, if the object type has a property of type list or array, then in the foreach element of the SQL mapping file, you can use the property name directly to refer to it.
Mapper Interface:

List<User> selectbyexample (userexample example);

SQL mapping File:

<>  <collection= "Oredcriteria"  Item  = "Criteria"  separator= "or">    <  Test = "Criteria.valid" > </ where >

Here, Userexample has a property called Oredcriteria, whose type is list, so you can refer to the list directly using the property name Oredcriteria in the foreach element.

Item= "Criteria" means using the criteria name to refer to each list or array element in each collection

MyBatis Incoming parameters and ParameterType

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.