Reference: http://openwares.net/database/mybatis_parametertype.html
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.
Simple data types
Mapper Interface Method:
1 |
User selectByPrimaryKey(Integer id); |
SQL mapping:
123456 |
<
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:
12345678 |
<
select id
=
"selectByPrimaryKey" resultMap
=
"BaseResultMap" parameterType
=
"java.lang.Integer" >
select
<
include refid
=
"Base_Column_List" />
from tb_user
<
if test
=
"_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.
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:
SQL mapping:
123 |
< insert id = "Insert" parametertype = "User" usegeneratedkeys = keyproperty = "id" > &NBSP;&NBSP; insert into Tb_user (name, sex) &NBSP;&NBSP; values (#{name,jdbctype=char}, #{sex,jdbctype=char}) |
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:
1 |
< if test = "_parameter != null" > |
To test the properties of the user object:
1 |
< if test = "name != null" > |
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:
1 |
int updateByExample( @Param ( "user" ) User user, @Param ( "example" ) UserExample example); |
SQL mapping:
1234567 |
<
update id
=
"updateByExample" parameterType
=
"map" >
update tb_user
set id = #{user.id,jdbcType=INTEGER},
...
<
if test
=
"_parameter != null" >
<
include refid
=
"Update_By_Example_Where_Clause" />
</
if
>
|
Note here that the test passed in the map is empty, still using _parameter
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:
1 |
User selectUserInList(List<Interger> idlist); |
SQL Dynamic Statement Mapping:
123456789 |
<
select id
=
"selectUserInList" resultType
=
"User"
>
SELECT *
FROM USER
WHERE ID in
<
foreach item
=
"item" index
=
"index" collection
=
"list"
open
=
"(" separator
=
"," close
=
")"
>
#{item}
</
foreach
>
</
select
>
|
- 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:
1 |
List<User> selectByExample(UserExample example); |
SQL mapping File:
123 |
< where > &NBSP;&NBSP; < foreach collection = item Code class= "XML plain" >= separator = > &NBSP;&NBSP;&NBSP;&NBSP; < if test = "Criteria.valid" > |
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